先下载海康SDK
我下载的win64版本: 海康SDK官网下载
解压后得到库文件和头文件
头文件
库文件
新建QWidget项目,选择64位的kits工具,在pro目录新建include文件夹和libs文件夹
进入解压好的海康库文件所在目录,将全部文件复制到libs里
进入解压好的海康头文件所在目录,将全部文件复制到include里
在.pro文件里加入以下代码:
INCLUDEPATH += \-L. $$PWD\include# "-L. $$PWD" 表示pro所在目录LIBS += \-L. $$PWD/libs/HCCore.lib \-L. $$PWD/libs/HCNetSDK.lib \-L. $$PWD/libs/PlayCtrl.lib \-L. $$PWD/libs/HCNetSDKCom\HCAlarm.lib \-L. $$PWD/libs/HCNetSDKCom\HCGeneralCfgMgr.lib \-L. $$PWD/libs/HCNetSDKCom\HCPreview.lib
遇到乱码问题需要将HCNetSDK.h文件保存为带bom的utf8格式
头文件:
#ifndef WIDGET_H#define WIDGET_H#pragma execution_character_set("utf-8")#include <QWidget>#include "HCNetSDK.h"class QLabel;class Widget : public QWidget{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();long play(HWND hWnd,NET_DVR_PREVIEWINFO struPlayInfo);bool initSDK();bool uninitSDK();public slots:void OnClickedLogin();void OnClickedLogout();void OnClickedPreview();void OnClickedStopPreview();void OnClickedCapture();private:long userID = -1;long previewID = -1;QLabel* label = nullptr;};#endif // WIDGET_H
源文件:
#include "widget.h"#include <QDebug>#include <iostream>#include <QHBoxLayout>#include <QVBoxLayout>#include <windows.h>#include <QMessageBox>#include <QPushButton>#include <QLabel>#include <QDateTime>Widget::Widget(QWidget *parent) :QWidget(parent){setMinimumSize(800,600);label = new QLabel("text");auto LoginButton = new QPushButton("登录",this);auto LogoutButton= new QPushButton("注销",this);auto PreviewButton= new QPushButton("实时画面",this);auto StopPreviewButton= new QPushButton("停止实时画面",this);auto CaptureButton= new QPushButton("抓图",this);connect(LoginButton,&QPushButton::clicked,this,&Widget::OnClickedLogin);connect(LogoutButton,&QPushButton::clicked,this,&Widget::OnClickedLogout);connect(PreviewButton,&QPushButton::clicked,this,&Widget::OnClickedPreview);connect(StopPreviewButton,&QPushButton::clicked,this,&Widget::OnClickedStopPreview);connect(CaptureButton,&QPushButton::clicked,this,&Widget::OnClickedCapture);QHBoxLayout* hbox = new QHBoxLayout();hbox->addWidget(LoginButton);hbox->addWidget(LogoutButton);hbox->addWidget(PreviewButton);hbox->addWidget(StopPreviewButton);hbox->addWidget(CaptureButton);QVBoxLayout* mainBox = new QVBoxLayout(this);mainBox->addWidget(label,1);mainBox->addLayout(hbox,0);if(initSDK())qDebug()<<"SDK init Success!";}Widget::~Widget(){if(uninitSDK())qDebug()<<"SDK uninit Success!";}long Widget::play(HWND hWnd, NET_DVR_PREVIEWINFO struPlayInfo){struPlayInfo={0};struPlayInfo.hPlayWnd = hWnd;//需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空struPlayInfo.lChannel = 33;//预览通道号struPlayInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推struPlayInfo.dwLinkMode = 0;//0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTPstruPlayInfo.bBlocked = 1;//0- 非阻塞取流,1- 阻塞取流long IRealPlayHandle = NET_DVR_RealPlay_V40(userID,&struPlayInfo,NULL,NULL);if(IRealPlayHandle <0){qDebug() << "NET_DVR_RealPlay_V40 error;error number " <<NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_RealPlay_V40 error;error number "+QString::number(NET_DVR_GetLastError()));NET_DVR_Logout(userID);userID=-1;return -1;}return IRealPlayHandle;}bool Widget::initSDK(){bool isok = NET_DVR_Init();if(isok == false){qDebug()<< "NET_DVR_Init error;error number is " <<NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_Init error;error number is "+QString::number(NET_DVR_GetLastError()));return isok;}//设置连接时间与重连时间NET_DVR_SetConnectTime(2000, 1);NET_DVR_SetReconnect(10000, true);return isok;}bool Widget::uninitSDK(){bool isok = NET_DVR_Cleanup();if(isok == false){qDebug()<< "NET_DVR_Cleanup error;error number is " <<NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_Cleanup error;error number "+QString::number(NET_DVR_GetLastError()));return isok;}return isok;}void Widget::OnClickedLogin(){NET_DVR_USER_LOGIN_INFO struLoginInfo = {};struLoginInfo.bUseAsynLogin = 0; //同步登录方式strcpy_s(struLoginInfo.sDeviceAddress, "192.168.1.143"); //设备IP地址struLoginInfo.wPort = 8000; //设备服务端口strcpy_s(struLoginInfo.sUserName, "admin"); //设备登录用户名strcpy_s(struLoginInfo.sPassword, "12345678"); //设备登录密码//设备信息, 输出参数NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {};userID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);if(userID<0){qDebug() << "NET_DVR_Login_V40 error;" << "error number is " << NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_Login_V40 error;error number "+QString::number(NET_DVR_GetLastError()));return;}qDebug()<<"Login Success,userID:" << userID;NET_DVR_IPPARACFG_V40 ipcfg ={};DWORD bytesReturned = 0;ipcfg.dwSize = sizeof(NET_DVR_IPPARACFG_V40);int iGroupNO = 0;bool resCode = NET_DVR_GetDVRConfig(userID, NET_DVR_GET_IPPARACFG_V40, iGroupNO, &ipcfg, sizeof(NET_DVR_IPPARACFG_V40), &bytesReturned);if (!resCode){DWORD code = NET_DVR_GetLastError();std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;NET_DVR_Logout(userID);NET_DVR_Cleanup();return ;}std::wcout << "设备组 " << ipcfg.dwGroupNum << " 数字通道个数 " << ipcfg.dwDChanNum << " 起始通道 " << ipcfg.dwStartDChan << std::endl << std::endl;for (unsigned i = 0; i < ipcfg.dwDChanNum; i++){NET_DVR_PICCFG_V30 channelInfo;bytesReturned = 0;channelInfo.dwSize = sizeof(NET_DVR_PICCFG_V30);unsigned channelNum = i + ipcfg.dwStartDChan;NET_DVR_GetDVRConfig(userID, NET_DVR_GET_PICCFG_V30, channelNum, &channelInfo, sizeof(NET_DVR_PICCFG_V30), &bytesReturned);std::wcout <<u8"通道号 "<< channelNum << "\t通道名称 " << channelInfo.sChanName;std::wcout << "\t用户名 " << ipcfg.struIPDevInfo[i].sUserName << "\t密码 " << ipcfg.struIPDevInfo[i].sPassword;std::wcout << "\t设备ID " << (size_t)ipcfg.struIPDevInfo[i].szDeviceID;std::wcout << "\tip地址 " << ipcfg.struIPDevInfo[i].struIP.sIpV4 << "\t端口 " << ipcfg.struIPDevInfo[i].wDVRPort << std::endl;}}void Widget::OnClickedLogout(){if(userID!=-1){if(NET_DVR_Logout(userID)==false){qDebug() << "NET_DVR_Logout error;" << "error number is " << NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_Logout error;error number "+QString::number(NET_DVR_GetLastError()));return;}userID=-1;qDebug() <<"Logout Success!";}}void Widget::OnClickedPreview(){if(userID==-1){qDebug() <<"Need Login!";QMessageBox::warning(this,"error","Need Login!");return;}HWND hWnd = (HWND)label->winId();NET_DVR_PREVIEWINFO struPlayInfo;previewID = play(hWnd,struPlayInfo);if(previewID==-1){return;}qDebug()<<"Preview Success!" << userID;}void Widget::OnClickedStopPreview(){if(previewID!=-1){if(NET_DVR_StopRealPlay(previewID)==false){qDebug() << "NET_DVR_StopRealPlay error;" << "error number is " << NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_StopRealPlay error;error number "+QString::number(NET_DVR_GetLastError()));return;}previewID=-1;qDebug() <<"Stop Preview Success!";}}void Widget::OnClickedCapture(){if(userID!=-1){NET_DVR_JPEGPARA IpJpegPara2;IpJpegPara2.wPicQuality=0;IpJpegPara2.wPicSize=0xff;QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyy-MM-dd_hh-mm-ss_zzz");current_date="C:\\Picture\\"+current_date+".jpg";char* fileName;QByteArray ba = current_date.toLatin1(); // mustfileName=ba.data();if(NET_DVR_CaptureJPEGPicture(userID,1,&IpJpegPara2,fileName)==false){qDebug() << "NET_DVR_CaptureJPEGPicture error;" << "error number is " << NET_DVR_GetLastError();QMessageBox::warning(this,"error","NET_DVR_CaptureJPEGPicture error;error number "+QString::number(NET_DVR_GetLastError()));return;}qDebug() <<"Capture Success!";}}