vs配置opencv环境 opencv绘制海康相机

it2024-03-11  80

首先我们打开vs 创建一个控制台应用 去上一层目录 然后选择×64 debug 讲海康相机以及opencv的dll文件复制进去 分别在opencv的这个目录下 以下是调用的代码

// HIK.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <HCNetSDK.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/types_c.h> #include <opencv2/highgui/highgui_c.h> #include <opencv2/opencv.hpp> #include <PlayM4.h> //#include <afx.h> using namespace std; using namespace cv; LONG nPort = -1; HWND hPlayWnd = NULL; FILE *Videofile = NULL; FILE *Audiofile = NULL; char filename[100]; int iPicNum = 0;//Set channel NO. // 解码回调 视频为YUV数据(YV12),音频为PCM数据 void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2) { long lFrameType = pFrameInfo->nType; if (lFrameType == T_AUDIO16) { // TRACE("Audio nStamp:%d\n", pFrameInfo->nStamp); OutputDebugString("test_DecCb_Write Audio16 \n"); if (Audiofile == NULL) { sprintf(filename, "AudionPCM.pcm", iPicNum); Audiofile = fopen(filename, "wb"); } fwrite(pBuf, nSize, 1, Audiofile); } else if (lFrameType == T_YV12) { int w = pFrameInfo->nWidth; int h = pFrameInfo->nHeight; Mat yuv = Mat(h*1.5,w,CV_8UC1,pBuf); Mat rgb = Mat(h, w, CV_8UC3);; cvtColor(yuv, rgb, CV_YUV2RGB_I420); namedWindow("rgb", 0); imshow("rgb", rgb); waitKey(5); //TRACE("Video nStamp:%d\n", pFrameInfo->nStamp); /*OutputDebugString("test_DecCb_Write YUV \n"); if (Videofile == NULL) { sprintf(filename, "VideoYV12.yuv", iPicNum); Videofile = fopen(filename, "wb"); } fwrite(pBuf, nSize, 1, Videofile);*/ } else { } } //void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser) //{ // switch (dwDataType){ // case NET_DVR_SYSHEAD: // cout << "1" << endl; // break; // case NET_DVR_STREAMDATA: // cout << dwDataType << endl; // break; // default: // break; // } //} void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser) { DWORD dRet = 0; BOOL inData = FALSE; switch (dwDataType) { case NET_DVR_SYSHEAD: if (!PlayM4_GetPort(&nPort)) { break; } if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024)) { dRet = PlayM4_GetLastError(nPort); break; } //设置解码回调函数 只解码不显示 // if (!PlayM4_SetDecCallBack(nPort,DecCBFun)) // { // dRet=PlayM4_GetLastError(nPort); // break; // } //设置解码回调函数 解码且显示 if (!PlayM4_SetDecCallBackEx(nPort, DecCBFun, NULL, NULL)) { dRet = PlayM4_GetLastError(nPort); break; } //打开视频解码 if (!PlayM4_Play(nPort, hPlayWnd)) { dRet = PlayM4_GetLastError(nPort); break; } //打开音频解码, 需要码流是复合流 if (!PlayM4_PlaySound(nPort)) { dRet = PlayM4_GetLastError(nPort); break; } break; case NET_DVR_STREAMDATA: inData = PlayM4_InputData(nPort, pBuffer, dwBufSize); //cout << dwDataType << endl; while (!inData) { Sleep(10); inData = PlayM4_InputData(nPort, pBuffer, dwBufSize); OutputDebugString("PlayM4_InputData failed \n"); } break; default: inData = PlayM4_InputData(nPort, pBuffer, dwBufSize); while (!inData) { Sleep(10); inData = PlayM4_InputData(nPort, pBuffer, dwBufSize); OutputDebugString("PlayM4_InputData failed \n"); } break; } } int main() { LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo = NULL; char ip[16], userName[64], passWord[64]; NET_DVR_Init(); //HK SDK初始化 NET_DVR_SetConnectTime(); strcpy_s(ip, 16, "192.168.1.112"); strcpy_s(userName, 64, "admin"); strcpy_s(passWord, 64, "jk123456"); LONG userId = NET_DVR_Login_V30(ip,8000,userName,passWord,lpDeviceInfo); if (userId < 0) { cout << "Login Failed" << endl; } else { cout << "Login success, userId:" << userId << endl; } NET_DVR_CLIENTINFO ClientInfo; ClientInfo.lChannel = 1; //Channel number 设备通道号 ClientInfo.hPlayWnd = NULL; //窗口为空,设备SDK不解码只取流 ClientInfo.lLinkMode = 0; //Main Stream ClientInfo.sMultiCastIP = NULL; LONG llRealHandle = NET_DVR_RealPlay_V30(userId, &ClientInfo, fRealDataCallBack,NULL,TRUE); getchar(); if (llRealHandle < 0) { cout << "Failed to Start RealPlay" << endl; return 0; } else { cout << "Start RealPlay,playHnd =" <<llRealHandle << endl; } if (NET_DVR_Logout(userId)) { cout << "Logout success" << endl; } else { cout << "Logout failed" << endl; } NET_DVR_Cleanup(); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
最新回复(0)