看了好长时间博客,都是用swing 编译c++ 自己搞得 各种问题成功率也不高,于是抱着试一试的态度找海康要一下python的sdk 结果还真有话不多说上代码和库。
# -*- coding: utf-8 -*-
主函数入口文件
__author__ = 'Dean'from ctypes import *
import platform, os, time
from HCNetSDK_header import *
from playctrl import *import tkinter as tk
import win32api
import win32con
import pywintypes
from win32gui import * # 设备信息申明
DEV_IP = "1"
DEV_PORT = 8000
DEV_USER_NAME = 'admin'
DEV_PASSWORD = 'haikang123'
WINDOWS_FLAG = True# HCNetSDK库文件路径
HCNETSDK_DLL_PATH = r'./HCNetSDK.dll'
HCNETSDK_DLL_PATH_LINUX = r'./libhcnetsdk.so'def GetPlatform():
sysstr = platform.system()
if sysstr !="Windows":
global WINDOWS_FLAG
WINDOWS_FLAG = False
def LoadHCNetSDK():
加载HCNetSDK库
if not WINDOWS_FLAG:
Objdll = cdll.LoadLibrary(HCNETSDK_DLL_PATH_LINUX)
else:
Objdll = WinDLL(HCNETSDK_DLL_PATH)
return Objdlldef InitHCNetSDK(Objdll):
初始化HCNetSDK库
if not WINDOWS_FLAG:
sdk_path = NET_DVR_LOCAL_SDK_PATH()
sdk_path.sPath = os.path.dirname(HCNETSDK_DLL_PATH).encode('utf-8')
Objdll.NET_DVR_SetSDKInitCfg(2, byref(sdk_path)) # 初始化DLL
Objdll.NET_DVR_Init()
# 设置日志路径
if not WINDOWS_FLAG:
Objdll.NET_DVR_SetLogToFile(3, r'/home/sdklog', True)
else:
Objdll.NET_DVR_SetLogToFile(3, r'C:\\SdkLog', True)
# 设置设备超时时间
Objdll.NET_DVR_SetConnectTime(int(2000), 1)def LoginDev(Objdll):
device_info = NET_DVR_DEVICEINFO_V30()
print(DEV_IP)
print(DEV_PORT)
print(DEV_USER_NAME)
print(DEV_PASSWORD) lUserId = Objdll.NET_DVR_Login_V30(bytes(DEV_IP, 'utf-8'), DEV_PORT, bytes(DEV_USER_NAME, 'utf-8') \
, bytes(DEV_PASSWORD, 'utf-8'), byref(device_info)) return (lUserId, device_info)
def RealDataCallBack_V30(lPlayHandle, dwDataType, pBuffer, dwBufSize, pUser):
码流回调函数
if dwDataType == NET_DVR_SYSHEAD:
# 设置流播放模式
Playctrldll.PlayM4_SetStreamOpenMode(PLAYCTRL_PORT, 0)
if Playctrldll.PlayM4_OpenStream(PLAYCTRL_PORT, pBuffer, dwBufSize, 1024*1000):
global FuncDisplayCB
FuncDisplayCB = DISPLAYCBFUN(DisplayCBFun)
Playctrldll.PlayM4_SetDisplayCallBack(PLAYCTRL_PORT, FuncDisplayCB)
if Playctrldll.PlayM4_Play(PLAYCTRL_PORT, None):
print('播放库播放成功')
else:
print('播放库播放成功')
else:
print('播放库播放成功')
elif dwDataType == NET_DVR_STREAMDATA:
Playctrldll.PlayM4_InputData(PLAYCTRL_PORT, pBuffer, dwBufSize)
else:
print('播放库播放成功'+dwBufSize) def OpenPreview(Objdll, lUserId, callbackFun):
''' window = tk.Tk() # 创建窗口
window.title("this is a test") # 窗口标题
window.geometry('500x400') # 窗口大小,小写字母x
k = tk.Label(window,
text='this is a window created by tkinter', # 文本
bg='green', # 字体的背景颜色
font=('Arial', 12), # 字体和大小
width=30, height=2 # 字体所占的宽度和高度
k.pack() # 固定 hWindow = pywintypes.HANDLE(int(window.frame(), 16))
print(hWindow)
hdc = GetDC(hWindow) preview_info = NET_DVR_PREVIEWINFO()
preview_info.hPlayWnd = hdc
preview_info.lChannel = 1 #通道号
preview_info.dwStreamType = 0 #主码流
preview_info.dwLinkMode = 0 #TCP
preview_info.bBlocked = 1 #阻塞取流 lRealPlayHandle = Objdll.NET_DVR_RealPlay_V40(lUserId, byref(preview_info)\
, callbackFun, None);
window.mainloop() # 结束(不停循环刷新)
return lRealPlayHandledef InputData(fileMp4, Playctrldll):
while True:
pFileData = fileMp4.read(4096)
if pFileData is None:
break if not Playctrldll.PlayM4_InputData(PLAYCTRL_PORT, pFileData, len(pFileData)):
breakif __name__ == '__main__':
# 获取系统平台
GetPlatform() (sdkPath, tempfilename) = os.path.split(HCNETSDK_DLL_PATH)
Playctrldll = LoadPlayctrlSDK(sdkPath, WINDOWS_FLAG)
if not Playctrl_Getport(Playctrldll):
print('获取播放库通道号失败')
exit()
# 加载HCNetSDK库
Objdll = LoadHCNetSDK() # 初始化HCNetSDK库
InitHCNetSDK(Objdll)
# 登录设备
(lUserId, device_info) = LoginDev(Objdll)
if lUserId < 0:
print('Login device fail, error code is:%d' % Objdll.NET_DVR_GetLastError())
# 释放资源
Objdll.NET_DVR_Cleanup()
#exit() # 定义码流回调函数
funcRealDataCallBack_V30 = REALDATACALLBACK(RealDataCallBack_V30)
# 开启预览
lRealPlayHandle = OpenPreview(Objdll, lUserId, funcRealDataCallBack_V30)
if lRealPlayHandle < 0:
print('Open preview fail, error code is:%d' % Objdll.NET_DVR_GetLastError()) # 登出设备
Objdll.NET_DVR_Logout(lUserId)
# 释放资源
Objdll.NET_DVR_Cleanup()
#exit() time.sleep(10)
Playctrldll.PlayM4_Stop(PLAYCTRL_PORT)
Playctrldll.PlayM4_CloseFile(PLAYCTRL_PORT)
Playctrldll.PlayM4_FreePort(PLAYCTRL_PORT)
PLAYCTRL_PORT = c_long(-1) # 开始云台控制
lRet = Objdll.NET_DVR_PTZControl(lRealPlayHandle, ZOOM_IN, 0)
if lRet == 0:
print('Start ptz control fail, error code is:%d' % Objdll.NET_DVR_GetLastError())
else:
print('Start ptz control success')
# 停止云台控制
lRet = Objdll.NET_DVR_PTZControl(lRealPlayHandle, ZOOM_IN, 1)
if lRet == 0:
print('Stop ptz control fail, error code is:%d' % Objdll.NET_DVR_GetLastError())
else:
print('Stop ptz control success') # 关闭预览
Objdll.NET_DVR_StopRealPlay(lRealPlayHandle)
# 登出设备
Objdll.NET_DVR_Logout(lUserId) # 释放资源
Objdll.NET_DVR_Cleanup()