python 调用海康SDK 抓图
时间: 2023-06-29 15:09:31 浏览: 320
基于python的海康摄像头的SDK抓取单张图像
你可以使用海康SDK提供的HCNetSDK.dll库文件来调用它的抓图功能。以下是一个简单的示例代码,可以帮助你开始:
```python
import sys
import os
from ctypes import *
# 加载HCNetSDK.dll
dll_path = os.path.join(os.getcwd(), "HCNetSDK.dll")
hcnetsdk = windll.LoadLibrary(dll_path)
# 登录设备
def login_device(ip, port, username, password):
device_info = hcnetsdk.NET_DVR_DEVICEINFO_V30()
lUserID = hcnetsdk.NET_DVR_Login_V30(ip.encode('utf-8'), port, username.encode('utf-8'), password.encode('utf-8'), byref(device_info))
if lUserID < 0:
print("登录失败,错误码:", hcnetsdk.NET_DVR_GetLastError())
return -1
else:
print("登录成功,用户ID:", lUserID)
return lUserID
# 抓图
def capture_picture(lUserID):
pic_path = "snapshot.jpg"
pic_size = 1024 * 1024
pic_buffer = create_string_buffer(pic_size)
ret = hcnetsdk.NET_DVR_CaptureJPEGPicture(lUserID, 1, byref(pic_buffer), pic_size, byref(c_long(0)))
if ret > 0:
with open(pic_path, "wb") as f:
f.write(pic_buffer.raw[:ret])
print("抓图成功,图片已保存到:", pic_path)
else:
print("抓图失败,错误码:", hcnetsdk.NET_DVR_GetLastError())
# 退出登录
def logout_device(lUserID):
hcnetsdk.NET_DVR_Logout(lUserID)
if __name__ == '__main__':
ip = "192.168.1.64" # 设备IP地址
port = 8000 # 设备端口号
username = "admin" # 登录用户名
password = "12345" # 登录密码
lUserID = login_device(ip, port, username, password)
if lUserID < 0:
sys.exit()
capture_picture(lUserID)
logout_device(lUserID)
```
需要注意的是,此示例仅适用于海康SDK V5.3以上版本。在使用该示例前,你需要安装海康SDK并且根据你的Python版本安装相应的ctypes模块。
阅读全文