python调用海康SDK存在几个问题,一是海康官方没有python技术支持,二是海康SDK涉及到的参数传递特
别多,因此不建议直接用python调用海康SDK,最好是直接修改海康的C++demo,然后编译成可执行程序或者
动态库供python调用,下面的登录接口是python直接调用海康的SDK库进行登录的
海康的库主要放在lib下,在使用海康的库函数的时候,因为不知道该函数是在哪个库里面,所以我直接采用了
遍历库的办法去执行程序
import os import ctypes #获取所有的库文件到一个列表 path = "/home/caobin/chike/chike/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/" so_list = [] def add_so(path,so_list): files = os.listdir(path) for file in files: if not os.path.isdir(path+file): if file.endswith(".so"): so_list.append(path+file) else: add_so(path+file+"/",so_list) add_so(path,so_list) for lib in so_list: print(lib) lUserID = 0 m_lAlarmHandle = 0 def callCpp(func_name,*args): for so_lib in so_list: # print(so_lib) try: lib = ctypes.cdll.LoadLibrary(so_lib) try: value = eval("lib.%s"%func_name)(*args) print("调用的库:"+so_lib) print("执行成功,返回值:"+str(value)) return value except: continue except: print("库文件载入失败:"+so_lib) continue print("没有找到接口!") return False #定义结构体 class LPNET_DVR_DEVICEINFO_V30(ctypes.Structure): _fields_ = [ ("sSerialNumber", ctypes.c_byte * 48), ("byAlarmInPortNum", ctypes.c_byte), ("byAlarmOutPortNum", ctypes.c_byte), ("byDiskNum", ctypes.c_byte), ("byDVRType", ctypes.c_byte), ("byChanNum", ctypes.c_byte), ("byStartChan", ctypes.c_byte), ("byAudioChanNum", ctypes.c_byte), ("byIPChanNum", ctypes.c_byte), ("byZeroChanNum", ctypes.c_byte), ("byMainProto", ctypes.c_byte), ("bySubProto", ctypes.c_byte), ("bySupport", ctypes.c_byte), ("bySupport1", ctypes.c_byte), ("bySupport2", ctypes.c_byte), ("wDevType", ctypes.c_uint16), ("bySupport3", ctypes.c_byte), ("byMultiStreamProto", ctypes.c_byte), ("byStartDChan", ctypes.c_byte), ("byStartDTalkChan", ctypes.c_byte), ("byHighDChanNum", ctypes.c_byte), ("bySupport4", ctypes.c_byte), ("byLanguageType", ctypes.c_byte), ("byVoiceInChanNum", ctypes.c_byte), ("byStartVoiceInChanNo", ctypes.c_byte), ("byRes3", ctypes.c_byte * 2), ("byMirrorChanNum", ctypes.c_byte), ("wStartMirrorChanNo", ctypes.c_uint16), ("byRes2", ctypes.c_byte * 2)] #用户注册设备 def NET_DVR_Login_V30(sDVRIP = "192.168.1.63",wDVRPort = 8000,sUserName = "admin",sPassword = "guoji123"): init_res = callCpp("NET_DVR_Init")#SDK初始化 if init_res: print("SDK初始化成功") else: error_info = callCpp("NET_DVR_GetLastError") print("SDK初始化错误:" + str(error_info)) return False set_overtime = callCpp("NET_DVR_SetConnectTime",5000,4)#设置超时 if set_overtime: print("设置超时时间成功") else: error_info = callCpp("NET_DVR_GetLastError") print("设置超时错误信息:" + str(error_info)) return False #用户注册设备 #c++传递进去的是byte型数据,需要转成byte型传进去,否则会乱码 sDVRIP = bytes(sDVRIP,"ascii") sUserName = bytes(sUserName,"ascii") sPassword = bytes(sPassword,"ascii") DeviceInfo = LPNET_DVR_DEVICEINFO_V30() DeviceInfoRef = ctypes.byref(DeviceInfo) lUserID = callCpp("NET_DVR_Login_V30",sDVRIP,wDVRPort,sUserName,sPassword,DeviceInfoRef) print("登录结果:"+str(lUserID)) if lUserID == -1: error_info = callCpp("NET_DVR_GetLastError") print("登录错误信息:" + str(error_info)) return error_info else: return lUserID NET_DVR_Login_V30()