# -*- 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 Objdll

def 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 lRealPlayHandle

def InputData(fileMp4, Playctrldll):
while True:
pFileData = fileMp4.read(4096)
if pFileData is None:
break

if not Playctrldll.PlayM4_InputData(PLAYCTRL_PORT, pFileData, len(pFileData)):
break

if __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()

获取直播rtmp地址备注: 1.萤石开发手册:https://open.ys7.com/doc/zh/uikit/wechat_miniapp.html微信公众平台:在里注册小程序必须选用企业账号(需要审核)3.可以采用复核资质快速注册小程序(无需任何费用和审核)...... 在dhnet sdk .h 中 // 错误类型代号,对应CLIENT_GetLastError接口的返回值 #define _EC(x) (0x80000000|x) #define NET_NOERROR 0 // 没有错误 #define NET_ERROR -1 / 要 调用 摄像机视频,需要使用 SDK 提供的API。需要注意的是,上述代码中的`HCNet SDK `是 SDK Python 封装,需要先安装。此外,还需要在 摄像机的管理页面中开启 SDK 接入功能,并配置好IP地址、端口号、用户名和密码等信息。print(' SDK 初始化成功')print(' SDK 初始化失败')print('登录失败')print('登录成功') `export default function Player(e) { this.Player = { // clsid: "4E8893A4-8723-427A-81EA-72480BAB4501", application: "npPlugin SDK -plugin", <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schem @ApiOperation(value = "设置 摄像头PTZ参数", notes = "设置 摄像头PTZ参数") @Log(action = "设置 摄像头PTZ参数", tag = "/setHKCameraPTZValue") @WebMapping("/setHKCameraPTZValue") public JSONObject setHKCameraPTZvalue(String ip,String username,String password,int i.