参考链接:https://blog.csdn.net/o_ha_yo_yepeng/article/details/79825648
1.在海康官网下载sadp和ivms-4200测试并配置相机ip ,用户名和密码等
2.在海康官网下载相应版本sdk
3.新建一个c++工程
4.工程目录下建两个文件夹存放HCSDK的include和lib文件夹,OPENCV
将 \OPENCV\opencv\build\x64\vc15\lib 复制到目标文件夹
将 OPENCV\opencv\build\include 复制到目标文件夹
将OPENCV\opencv\build\x64\vc15\bin 复制到目标文件夹
OpenCV+VS2017配置:
5.打开解决方案的属性管理器,视图->其他窗口->属性管理器。
6.根据Debug或Release版本配置属性表或新建属性表
7.C/C++常规选项附加包含目录添加:D:\Tools\Platform_Test\HK_SDK\include
8.链接器输入选项添加:
D:\Tools\Platform_Test\HK_SDK\lib\HCNetSDKCom\HCAlarm.lib
D:\Tools\Platform_Test\HK_SDK\lib\HCNetSDKCom\HCPreview.lib
D:\Tools\Platform_Test\HK_SDK\lib\HCNetSDKCom\HCGeneralCfgMgr.lib
D:\Tools\Platform_Test\HK_SDK\lib\GdiPlus.lib
D:\Tools\Platform_Test\HK_SDK\lib\HCCore.lib
D:\Tools\Platform_Test\HK_SDK\lib\PlayCtrl.lib
D:\Tools\Platform_Test\HK_SDK\lib\HCNetSDK.lib
opencv_world455d.lib
9.V/C++目录 库目录添加 D:\Tools\OPENCV\opencv\build\x64\vc15\lib
包含目录添加 D:\Tools\OPENCV\opencv\build\include D:\Tools\OPENCV\opencv\build\include\opencv2
10.编写代码
HK_Camera.h
#pragma once #include <HCNetSDK.h> #include <plaympeg4.h> #include <PlayM4.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace std; class HK_Camera { public: HK_Camera(); ~HK_Camera(); public: bool Init(); //初始化 bool Login(char* sIpAddress, char* sUserName, char* sPassword, WORD wPort);//登录 void ShowImg(bool bSaveVideo = false, string sfileName = ""); private: LONG lUserID; };
HK_Camera.cpp
//#include "stdafx.h" #include "HK_Camera.h" #include <iostream> #include <opencv2\imgproc\imgproc.hpp> //全局变量 LONG g_nPort; cv::Mat g_BGRImage; cv::VideoWriter g_VideoWriter; //数据解码回调函数 //功能:将YV_12格式的视频数据流转码为可供opencv处理的BGR类型的图片数据,并实时显示 void CALLBACK DecCBFun(long nPort, char* pBuf, long nSize, FRAME_INFO* pFrameInfo, long nUser, long nReserved2) { if (pFrameInfo->nType == T_YV12) { //cout << "Frame type is T_YV12" << endl; if (g_BGRImage.empty()) { g_BGRImage.create(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3); } cv::Mat YUVImage(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, (unsigned char*)pBuf); cv::cvtColor(YUVImage, g_BGRImage, cv::COLOR_YUV2BGR_YV12); //cv::resize(g_BGRImage, g_BGRImage, cv::Size(g_BGRImage.cols / 2, g_BGRImage.rows / 2)); cv::imshow("RGBImage", g_BGRImage); if (g_VideoWriter.isOpened()) { g_VideoWriter.write(g_BGRImage); } cv::waitKey(10); YUVImage.~Mat(); } } void fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* pUser) { if (dwDataType == NET_DVR_STREAMDATA)//码流数据 { if (dwBufSize > 0 && g_nPort != -1) { if (PlayM4_InputData(g_nPort, pBuffer, dwBufSize)) { //cout << "Success input data " << endl; } else { cout << "Fail input data " << endl; } } } } HK_Camera::HK_Camera() { } HK_Camera::~HK_Camera() { if (g_VideoWriter.isOpened()) { g_VideoWriter.release(); } } //初始化 bool HK_Camera::Init() { if (NET_DVR_Init()) { return true; } else { return false; } } //登录 bool HK_Camera::Login(char* sIpAddress, char* sUserName, char* sPassword, WORD wPort) { NET_DVR_DEVICEINFO_V30 devInfo; this->lUserID = NET_DVR_Login_V30(sIpAddress, wPort, sUserName, sPassword, &devInfo); if (this->lUserID < 0) { cout << "Login Failed!" << endl; return false; } else { cout << "Login Success!" << endl; return true; } } //显示图片 void HK_Camera::ShowImg(bool bSaveVideo, string sfileName) { if (bSaveVideo == true) { double fps = 25.0; g_VideoWriter.open(sfileName, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, cv::Size(1920, 1080), true); } if (PlayM4_GetPort(&g_nPort)) //获取播放库通道号 { if (PlayM4_SetStreamOpenMode(g_nPort, STREAME_REALTIME)) //设置流模式 { if (PlayM4_OpenStream(g_nPort, NULL, 0, 1024 * 1024)) // 打开流 { if (PlayM4_SetDecCallBackExMend(g_nPort, DecCBFun, NULL, 0, NULL)) { if (PlayM4_Play(g_nPort, NULL)) { cout << "Success to set play mode" << endl; } else { cout << "Fail to set play mode" << endl; } } else { cout << "Fail to set dec callback " << endl; } } else { cout << "Fail to open stream" << endl; } } else { cout << "Fail to set stream open mode" << endl; } } else { cout << "Fail to get port" << endl; } //启动预览并设置回调数据流 NET_DVR_CLIENTINFO struClientInfo; struClientInfo.lChannel = 1; // Channel Number 设备通道 struClientInfo.hPlayWnd = NULL;//窗口为空,设备SDK不解码只取流 struClientInfo.lLinkMode = 0;//0:TCP方式,1:UDP方式,2:多播方式,3 - RTP方式,4-RTP/RTSP,5-RSTP/HTTP struClientInfo.sMultiCastIP = NULL; ;//多播组地址 if (NET_DVR_RealPlay_V30(this->lUserID, &struClientInfo, fRealDataCallBack, NULL, TRUE)) { cv::namedWindow("RgbImg"); } }
main.cpp
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <direct.h> #include "HK_Camera.h" #include <iostream> #include <opencv2/core/utils/logger.hpp> using namespace std; int main(int argc, char* argv[]) { //cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_SILENT);//不再输出日志 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);//只输出错误日志 HK_Camera camera; char sIpAddress[] = "192.168.0.64"; char sUserID[] = "admin"; char sPassword[] = "okwy1234"; WORD wPort = 8000; if (camera.Init()) { cout << "Init Success" << endl; if (camera.Login(sIpAddress, sUserID, sPassword, wPort)) { cout << "Login Success" << endl; camera.ShowImg(true, "test.avi"); } else { cout << "Login Fail" << endl; } } else { cout << "Init Fail" << endl; } system("pause"); return 0; }
11.代码运行前将 海康sdkD:\Tools\CH-HCNetSDKV6.1.6.45_build20210302_win64\库文件中所有文件 复制到 D:\Tools\test\HC_OpenCV_Test\x64\Debug 或 D:\Tools\test\HC_OpenCV_Test\x64\Release
BUG:
提示bug:E1696无法打开源文件 "PlayM4.h",解决办法:
到\CH-HCNetSDK(Windows64)V5.3.1.22_build20170909\Demo示例\2- MFC分功能示例\2- 实时流回调解码获取YUV\文件夹下找到PlayM4.h文件,添加到D:\Tools\Platform_Test\HK_SDK\include 文件夹下