对雄迈摄像头二次开发,因为雄迈给的资料比较混乱,没有找到可用的sdk手册,所以一开始想用OpenCV+qt。但是在Windows环境下,opencv摄像头读取卡顿严重,而且opencv只可以处理视频,不能处理音频,音频处理还需要ffmpeg。
后面发现SheBeiWangLuoSDK这个文件夹里面的东西可以参考,而且做出来效果也可以。所以就用雄迈的sdk进行二次开发。
准备工作
- 头文件(.h):netsdk.h
- 静态库(.lib):netsdk.lib
- 动态库:netsdk.dll,StreamReader.dll,playctrl.dll
说明:
- 主要使用netsdk。
- 使用时注意库32位和64位的区别(虽然他们的名字是一样的)。
- 录屏后没有录入声音,一般是因为没有playctrl.dll
#include <QMainWindow>
一定要在#include "netsdk.h"
前使用,不然会报错
初始化
// SDK初始化
bool iResult = H264_DVR_Init((fDisConnect)DisConnectBackCallFunc,(DWORD)this);
if(iResult == true){
cout<<"SDK INIT OK!"<<endl;
}else{
ccout << "SDK Init error;error number is " <<H264_DVR_GetLastError();
}
一开始这一部分我还不太会其中的断线回调部分,网上海康的二次开发比较多,所以后面我参考了海康的断线回调部分
//断线回调
void CALLBACK MainWindow::DisConnectBackCallFunc(LONG lLoginID, char *pchDVRIP, LONG nDVRPort, DWORD dwUser)
{
printf("Call DisConnectFunc\n");
cout<<"lLoginID:"<<lLoginID<<endl;
if(NULL != pchDVRIP)
{
cout<<"pchDVRIP:"<<pchDVRIP<<endl;
}
cout<<"nDVRPort:"<<nDVRPort<<endl;
cout<<"dwUser:"<<dwUser<<endl;
printf("\n");
}
摄像头登录
// 登录
H264_DVR_DEVICEINFO OutDev;
int nError = 0;
LoginHandle = 0;
H264_DVR_SetConnectTime(3000, 1);
char DVRIPtext[] = "192.168.0.100";
char* DVRIP= DVRIPtext;
char UserNametext[] = "admin";
char* UserName = UserNametext;
char Passwordtext[] = "";
char* Password = Passwordtext;
LoginHandle = H264_DVR_Login(DVRIP, 34567, UserName, Password,&OutDev,&nError);
输入参数:IP、端口号、用户名、密码
输出参数:摄像头的登录句柄
为了防止内存溢出,也可以先定义char,然后再使用strcpy()复制ip等
摄像头画面开启和关闭
我看大华和海康摄像头的二次开发,视频播放都是需要定义HWND
void MainWindow::on_Open_triggered()
{
//获取Label句柄
HWND myhWnd = (HWND)ui->myVideo->winId();
cout<<"hWnd"<<myhWnd<<endl;
// 播放句柄
H264_DVR_CLIENTINFO playstru;
playstru.nChannel = 0;//1的时候就报错-11202
playstru.nStream = 0;
playstru.nMode = 0;
playstru.hWnd = myhWnd;
//实时监视句柄
watchHandle = 0;
watchHandle = H264_DVR_RealPlay(LoginHandle,&playstru);
if(watchHandle <= 0){
cout<<"real play error; error number is "<<H264_DVR_SUB_CONNECT_ERROR<<endl;
}else{
cout<<"real play succuss; play id is "<<watchHandle<<endl;
}
}
关闭摄像头画面:
void MainWindow::on_Stop_triggered()
{
H264_DVR_StopRealPlay(watchHandle,NULL);
}
摄像头声音打开和关闭
void MainWindow::on_OpenSound_triggered()
{
// 打开音频
H264_DVR_OpenSound(watchHandle);
}
void MainWindow::on_StopSound_triggered()
{
// 关闭音频
H264_DVR_CloseSound(watchHandle);
}
摄像头截屏
注意路径应该为“\”或“/”,不可以为“\”
另外这个需要注意的是,在32位里使用H264_DVR_CatchPic()函数就可以,但在64位里需要使用H264_DVR_LocalCatchPic()函数
void MainWindow::on_CatchPic_triggered()
{
int nChannel = 0;
char* fileName = "D:\\study\\QtTestXM\\guTset1.bmp";
int nType = 0;//0:bmp 1:jpg
bool mytest = H264_DVR_CatchPic(watchHandle,nChannel,fileName,nType);
cout<<"pic======="<<mytest<<endl;
}
摄像头录像开启和关闭
录像开启:
void MainWindow::on_StartRecord_triggered()
{
// 回放没有声音是因为没有playctrl.dll
char* fileName = "D:\\study\\QtTestXM\\guVideo.avi";
long type=2;//0:h264 2:avi
H264_DVR_StartLocalRecord(watchHandle, fileName, type);
}
录像关闭:
void MainWindow::on_StopRecord_triggered()
{
H264_DVR_StopLocalRecord(watchHandle);
}
摄像头系统时间校准
void MainWindow::on_SetTime_triggered()
{
// 获取系统系统时间
QDate curDate=QDate::currentDate();
QTime curTime=QTime::currentTime();
// 设置相机时间
SDK_SYSTEM_TIME pSysTime;
pSysTime.year = curDate.year();
pSysTime.month = curDate.month();
pSysTime.day = curDate.day();
pSysTime.wday = curDate.dayOfWeek();
pSysTime.hour = curTime.hour();
pSysTime.minute = curTime.minute();
pSysTime.second = curTime.second();
H264_DVR_SetSystemDateTime(LoginHandle,&pSysTime);
}
摄像头颜色(亮度、饱和度)调节
以亮度增加为例:
void MainWindow::on_BrightnessUp_triggered()
{
DWORD nRegionNum = 0;
// 亮度,对比度,饱和度,色度
LONG pBrightness;
LONG pContrast;
LONG pSaturation;
LONG pHue;
H264_DVR_LocalGetColor(watchHandle,nRegionNum,&pBrightness,&pContrast,&pSaturation,&pHue);//获取现有颜色信息
LONG nBrightness = pBrightness*1.1;
LONG nContrast = pContrast;
LONG nSaturation = pSaturation;
LONG nHue = pHue;
H264_DVR_LocalSetColor(watchHandle,nRegionNum,nBrightness,nContrast,nSaturation,nHue);//设置新的颜色信息
}
一些参考
在考虑opencv开发,和sdk开发时,参考过的一些资料:
- OpenCV应用(4)雄迈相机网络取图
https://blog.csdn.net/a83025273/article/details/101955760 - C#基于雄迈Sdk的对画面视频监控源码
https://download.csdn.net/download/woofilian/9711418 - 海康威视的qt,用了callback函数
https://github.com/geometryl/qt_hikvision/blob/master/qt_hikvision/qt_hikvision/qt_hikvision.cpp - 对网络摄像头的授时
https://www.pianshen.com/article/37151633466/ - 私有协议、onvif协议
https://blog.csdn.net/benkaoya/article/details/72453403
搜“onvif协议授时”有一些可以参考的
https://blog.csdn.net/benkaoya/article/details/72486511