python海康威視sdkリアルタイムプレビュー

17492 ワード

http://www.genshuixue.com/i-cxy/p/12205516
 
CDLL("./libhpr.so", mode=ctypes.RTLD_GLOBAL)

CDLL("./libhlog.so", mode=ctypes.RTLD_GLOBAL)

lib = cdll.LoadLibrary("./libHcsSdk.so")





#coding=utf-8
import threading
from ctypes import *
dll = CDLL("./PlatformSDK.dll")
import sys
from PyQt5.QtWidgets import QApplication,QWidget

def aaa(a):
pStr = c_char_p( )
pStr.value = a
return pStr

def getStreamCallback(handle,iStreamType,data,size,pUser):
print(str(handle),iStreamType,size)

def getPreviewMsgCallback(handle, pUser,nMsg):
print(str(handle))
class PLAT_DEVICE_INFO_V20(Structure):
_fields_ = [('iId', c_int),
('iControlUnitId', c_int),
('iDevType', c_int),
('szName', c_char*64),
('szIndexCode', c_char*64),
('iRes1', c_int)]

def __str__(self):
return u'{0},{1}'.format(self.iId, self.iControlUnitId);

class PLAT_GET_INFO_RSP(Structure):
_fields_ = [('iRtnNum', c_int),
('iTotal', c_int)]
def __str__(self):
return u'{0},{1}'.format(self.iRtnNum, self.iTotal)
class PLAT_GET_INFO_REQ(Structure):
_fields_ = [('iBeginPos', c_int),
('iGetNum', c_int)]


class PLAT_CHANNEL_INFO_V20(Structure):
_fields_ = [('iChannelId', c_int), #  (    、   、IO )Id
('iChannelNo', c_int), #          
('iRegionId', c_int), #    Id
('iDeviceId', c_int), #     Id
('iState', c_int), #        PLAT_STATUS_*
('szChannelName', c_char * 256),#    
('szChannelIndex', c_char * 64),#     
('szDeviceIndex', c_char * 64),#       
('szStreamUrl', c_char*128)]#  URL
def __str__(self):
return u'{0},{1}'.format(self.iState, self.iDeviceId)
def getDevice():
pLAT_GET_INFO_RSP = PLAT_GET_INFO_RSP()
pLAT_DEVICE_INFO_V20 = PLAT_DEVICE_INFO_V20()

ret = dll.Plat_VSS_GetVssDevInfo_V20(g_iLogHandle, None, None, byref(pLAT_GET_INFO_RSP))
print("Plat_VSS_GetVssDevInfo_V20", ret)
print(pLAT_GET_INFO_RSP.__str__())
# print(dll.Plat_GetLastError())
ret = dll.Plat_VSS_GetVssDevInfo_V20(g_iLogHandle, addressof(pLAT_DEVICE_INFO_V20), addressof(pLAT_GET_INFO_RSP),
addressof(pLAT_GET_INFO_RSP))
print("Plat_VSS_GetVssDevInfo_V20", ret)
print(pLAT_DEVICE_INFO_V20.__str__())
print(str(pLAT_DEVICE_INFO_V20.szName, encoding="utf-8"))
print(str(pLAT_DEVICE_INFO_V20.szIndexCode, encoding="utf-8"))

def getCamere():
pLAT_GET_INFO_RSP = PLAT_GET_INFO_RSP()
p_GetCameraInfo_V20 = PLAT_CHANNEL_INFO_V20()
req=PLAT_GET_INFO_REQ()
ret = dll.Plat_VSS_GetCameraInfo_V20(g_iLogHandle, None, None, byref(pLAT_GET_INFO_RSP))
print("Plat_VSS_GetCameraInfo_V20", ret)
print(pLAT_GET_INFO_RSP.__str__())
for i in range(pLAT_GET_INFO_RSP.iTotal):
req.iBeginPos=i
req.iGetNum=1
# print(dll.Plat_GetLastError())
ret = dll.Plat_VSS_GetCameraInfo_V20(g_iLogHandle, byref(p_GetCameraInfo_V20),
byref(req),
addressof(pLAT_GET_INFO_RSP))
print("Plat_VSS_GetCameraInfo_V20", ret)
print(p_GetCameraInfo_V20.__str__())
print(str(p_GetCameraInfo_V20.szChannelName, encoding="utf-8"))
print(str(p_GetCameraInfo_V20.szChannelIndex, encoding="utf-8"))

print(str(p_GetCameraInfo_V20.szStreamUrl, encoding="utf-8"))

if __name__ == '__main__':
a = dll.Plat_Init()
print(a)
g_iLogHandle = dll.Plat_LoginCMS_V20(aaa(b"117.119.97.27"), 85, aaa(b'admin'), aaa(b'hik12345+'), 0, 61618)
print(g_iLogHandle)
if g_iLogHandle != 0:
# print(dll.Plat_GetLastError())
pass
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 300)
w.move(300, 300)
# print("type",c_int(w.winId())
puiHandle = c_int(11)
d = b"f0d979f821174c8b841d9cff6bcc61dd"

CMPFUNC = CFUNCTYPE(c_void_p, c_long, c_int, c_char_p, c_int, c_void_p)
m_callback = CMPFUNC(getStreamCallback)

# void __stdcall CPreview::CB_PreviewMsgCallback(long handle, void * pUser, int nMsg) {}

CMPFUNC_VM = CFUNCTYPE(c_void_p, c_long, c_void_p, c_int)
pm_callback = CMPFUNC_VM(getPreviewMsgCallback)

print("_callback")
# ret = play_dll.Plat_VSS_PlayVideo_V20(g_iLogHandle, str2pstr(d), c_int(w.winId()), byref(puiHandle), m_callback,
# pm_callback, None)

dll.Plat_VSS_PlayVideo_V20(g_iLogHandle, aaa(d), c_int(w.winId()), byref(puiHandle), m_callback,
pm_callback, None)
getCamere()
w.setWindowTitle('Hello world')
w.show()
sys.exit(app.exec_())
 
Windows 64では、pythonを通じて海康SDKを呼び出して、登録、プレビュー、スナップショット、光学変倍、カメラの活性化、エリアフォーカス、エリア露出機能を実現します。
 python       :

import numpy as np
import os
import ctypes
 
 
#             
path = "lib/win64/"
def file_name(file_dir):
    pathss=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
          pathss.append(path+file)
    return pathss
 
dll_list=file_name(path)
 
lUserID = 0
lChannel=1
def callCpp(func_name,*args):
    for HK_dll in dll_list:
        try:
            lib = ctypes.cdll.LoadLibrary(HK_dll)
            try:
                value = eval("lib.%s"%func_name)(*args)
                # print("    :"+HK_dll)
                # print("    ,   :"+str(value))
                return value
            except:
                continue
        except:
            # print("       :"+HK_dll)
            continue
    # print("      !")
    return False
 
# region   
#       
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)]
 
#          ,    IP,  、  
def NET_DVR_Login_V30(sDVRIP = "192.168.1.65",wDVRPort = 8000,sUserName = "admin",sPassword = "guoji123"):
    init_res = callCpp("NET_DVR_Init")#SDK   
    if init_res:
        print("SDK     ")
        error_info = callCpp("NET_DVR_GetLastError")
    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")
    print( "      ")
    DeviceInfo = LPNET_DVR_DEVICEINFO_V30()
    print(DeviceInfo)
    lUserID = callCpp("NET_DVR_Login_V30",sDVRIP,wDVRPort,sUserName,sPassword,ctypes.byref(DeviceInfo))
    print("    ,  ID:"+str(lUserID))
    if lUserID == -1:
        error_info = callCpp("NET_DVR_GetLastError")
        print("      :" + str(error_info))
        return error_info
    else:
        return lUserID
# endregion
 
# region   
#       
class NET_DVR_PREVIEWINFO(ctypes.Structure):
    _fields_ = [
        ("lChannel", ctypes.c_long),
        ("lLinkMode", ctypes.c_long),
        ("hPlayWnd", ctypes.c_void_p),
        ("sMultiCastIP", ctypes.c_char_p),
        ("byProtoType", ctypes.c_byte),
        ("byRes", ctypes.c_byte * 3)]
#     
def Preview():
    lpPreviewInfo=NET_DVR_PREVIEWINFO()
    # hPlayWnd           handle,        BMP  
    lpPreviewInfo.hPlayWnd=None
    lpPreviewInfo.lChannel=1
    lpPreviewInfo.dwLinkMode=0
    lpPreviewInfo.sMultiCastIP=None
    m_lRealHandle=callCpp("NET_DVR_RealPlay_V30",lUserID,ctypes.byref(lpPreviewInfo),None,None,True)
    if(m_lRealHandle<0):
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :" + str(error_info))
    else:
        print("    ")
    return m_lRealHandle
# endregion
 
 
# # region   
# # BMP       hPlayWnd       none
# def Get_BMPPicture():
#     sBmpPicFileName = bytes("pytest.bmp", "ascii")
#     if(callCpp("NET_DVR_CapturePicture",m_lRealHandle,sBmpPicFileName)==False):
#         error_info = callCpp("NET_DVR_GetLastError")
#         print("    :" + str(error_info))
#     else:
#         print("    ")
#
#        
class NET_DVR_JPEGPARA(ctypes.Structure):
    _fields_ = [
        ("wPicSize", ctypes.c_ushort),
        ("wPicQuality", ctypes.c_ushort)]
 
# jpeg  hPlayWnd      none,           
def Get_JPEGpicture():
    sJpegPicFileName = bytes("pytest.jpg", "ascii")
    lpJpegPara=NET_DVR_JPEGPARA()
    lpJpegPara.wPicSize=0
    lpJpegPara.wPicQuality=0
    if (callCpp("NET_DVR_CaptureJPEGPicture", lUserID, lChannel, ctypes.byref(lpJpegPara), sJpegPicFileName)== False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :" + str(error_info))
    else:
        print("    ")
# endregion
 
#         
 
#        
class NET_DVR_FOCUSMODE_CFG(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("byFocusMode", ctypes.c_byte),
        ("byAutoFocusMode", ctypes.c_byte),
        ("wMinFocusDistance", ctypes.c_uint16),
        ("byZoomSpeedLevel", ctypes.c_byte),
        ("byFocusSpeedLevel", ctypes.c_byte),
        ("byOpticalZoom", ctypes.c_byte),
        ("byDigtitalZoom", ctypes.c_byte),
        ("fOpticalZoomLevel", ctypes.c_float),
        ("dwFocusPos", ctypes.c_uint32),
        ("byFocusDefinitionDisplay", ctypes.c_byte),
        ("byFocusSensitivity", ctypes.c_byte),
        ("byRes1", ctypes.c_byte*2),
        ("dwRelativeFocusPos", ctypes.c_uint32),
        ("byRes", ctypes.c_byte * 48)]
 
#        
def get_CamZoom():
    m_struFocusModeCfg = NET_DVR_FOCUSMODE_CFG()
    m_struFocusModeCfg.byRes
    dwReturned = ctypes.c_uint16(0)
    print(callCpp("NET_DVR_GetDVRConfig"))
    if (callCpp("NET_DVR_GetDVRConfig", lUserID, 3305, lChannel, ctypes.byref(m_struFocusModeCfg), 76,
                ctypes.byref(dwReturned)) == False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("        :" + str(error_info))
        ctypes.ARRAY()
    else:
        print("        ")
    return m_struFocusModeCfg.fOpticalZoomLevel
 
 
#        
def Change_CamZoom(zoomScale):
    m_struFocusModeCfg=NET_DVR_FOCUSMODE_CFG()
    dwReturned=ctypes.c_uint16(0)
    print(callCpp("NET_DVR_GetDVRConfig"))
    if (callCpp("NET_DVR_GetDVRConfig", lUserID, 3305, lChannel,ctypes.byref(m_struFocusModeCfg),76, ctypes.byref(dwReturned))==False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("        :" + str(error_info))
    else:
        print("        ")
        print("       :"+str(m_struFocusModeCfg.fOpticalZoomLevel))
        m_struFocusModeCfg.fOpticalZoomLevel=zoomScale
        if (callCpp("NET_DVR_SetDVRConfig", lUserID, 3306, lChannel, ctypes.byref(m_struFocusModeCfg),76)==False):
            error_info = callCpp("NET_DVR_GetLastError")
            print("        :" + str(error_info))
        else:
            print("        ;       :"+str(m_struFocusModeCfg.fOpticalZoomLevel))
 
 
 
 
#            
class NET_DVR_XML_CONFIG_INPUT(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("lpRequestUrl", ctypes.c_void_p),
        ("dwRequestUrlLen", ctypes.c_uint32),
        ("lpInBuffer", ctypes.c_void_p),
        ("dwInBufferSize", ctypes.c_uint32),
        ("dwRecvTimeOut", ctypes.c_uint32),
        ("byForceEncrpt", ctypes.c_byte),
        ("byRes", ctypes.c_byte*31),]
 
#            
class NET_DVR_XML_CONFIG_OUTPUT(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("lpOutBuffer", ctypes.c_void_p),
        ("dwOutBufferSize", ctypes.c_uint32),
        ("dwReturnedXMLSize", ctypes.c_uint32),
        ("lpStatusBuffer", ctypes.c_void_p),
        ("dwStatusSize", ctypes.c_uint32),
        ("byRes", ctypes.c_byte*31)]
 
 
#             :             (startX,startY,endX,endY)
# flag=1      ,flag!=1      
def RegionalCorrection(startX,startY,endX,endY,flag=1):
    # #      
    if(flag==1):
        choise="regionalFocus"
    else:
        choise = "regionalExposure"
    inUrl = "PUT /ISAPI/Image/channels/1/" + choise
    inPutBuffer = "" + str(startX) + "" + str(startY) + "" + str(endX) + "" + str(endY) + "" + choise + ">"
 
    szUrl = (ctypes.c_char * 256)()
    struInput = NET_DVR_XML_CONFIG_INPUT()
    struOuput = NET_DVR_XML_CONFIG_OUTPUT()
    struInput.dwSize=ctypes.sizeof(struInput)
    struOuput.dwSize=ctypes.sizeof(struOuput)
    dwBufferLen = 1024 * 1024
    pBuffer = (ctypes.c_char * dwBufferLen)()
#_____________________________________________put________________________________________________________
    csCommand = bytes(inUrl, "ascii")
    ctypes.memmove(szUrl, csCommand, len(csCommand))
    struInput.lpRequestUrl = ctypes.cast(szUrl,ctypes.c_void_p)
    struInput.dwRequestUrlLen = len(szUrl)
 
    m_csInputParam= bytes(inPutBuffer, "ascii")
    dwInBufferLen = 1024 * 1024
    pInBuffer=(ctypes.c_byte * dwInBufferLen)()
    ctypes.memmove(pInBuffer, m_csInputParam, len(m_csInputParam))
    struInput.lpInBuffer = ctypes.cast(pInBuffer,ctypes.c_void_p)
    struInput.dwInBufferSize = len(m_csInputParam)
 
    struOuput.lpStatusBuffer = ctypes.cast(pBuffer,ctypes.c_void_p)
    struOuput.dwStatusSize = dwBufferLen
 
    if (callCpp("NET_DVR_STDXMLConfig", lUserID, ctypes.byref(struInput), ctypes.byref(struOuput))):
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :" + str(error_info))
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :    "+ str(error_info))
 
 
#           
class NET_DVR_ACTIVATECFG(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("sPassword",ctypes.c_byte*16),
        ("byRes",  ctypes.c_byte*108)]
 
 
#      
def OnActivateDevice():
    init_res = callCpp("NET_DVR_Init")#SDK   
    if init_res:
        print("SDK     ")
        error_info = callCpp("NET_DVR_GetLastError")
    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
 
    szLan=(ctypes.c_char * 256)()
    pwd=bytes('guoji123', "ascii")#        
    DevAddr=bytes('192.168.1.64', "ascii") #      IP  
    struActivateCfg=NET_DVR_ACTIVATECFG()
    struActivateCfg.dwSize=ctypes.sizeof(struActivateCfg)
    ctypes.memmove(struActivateCfg.sPassword, pwd, len(pwd))
    if(callCpp("NET_DVR_ActivateDevice",DevAddr,8000,ctypes.byref(struActivateCfg))):
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :" + str(error_info))
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("    :" + str(error_info))
 
 
 
# OnActivateDevice()#      
#     print(ctypes.sizeof(NET_DVR_FOCUSMODE_CFG))
lUserID=NET_DVR_Login_V30()
    
m_lRealHandle=Preview()
# Get_JPEGpicture()
# Change_CamZoom(1)
 
#             :             (startX,startY,endX,endY)
# flag=1      ,flag!=1      
 
最新のDemo住所:https://download.csdn.net/download/weixin_40851278/1086000