1. 程式人生 > >opencv讀取海康威視攝像頭

opencv讀取海康威視攝像頭

海康威視的網路攝像頭使用:
開發工具:vs2013 C++ 海康SDK

下載海康威視SDK:網址:http://www.hikvision.com/cn/download_61.html
• 1:對照自己的開發平臺可以下載裝置網路SDK_Win32裝置網路SDK_Win64,解壓到本地檔案下:
• 2:VS配置
• Opencv配置。參考址 http://blog.csdn.net/poem_qianmo/article/details/19809337
• 建議自己建立一個專案屬性表這樣方便使用
• SDK庫檔案和標頭檔案配置
• 解壓SDK可以得到標頭檔案和庫檔案
• 在VC++目錄中新增opencv的標頭檔案包含路徑和SDK中的標頭檔案(修改“標頭檔案”為“include”)
這裡寫圖片描述


新增庫檔案:既要新增庫檔案lib也要把lib下的HCNetSDKCom檔案包含進去
這裡寫圖片描述
再連結器——輸入——新增依賴項,把opencv的lib和SDK中的lib新增進去(包含HCNetSDKCom的lib)
opencv_ml249d.lib
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
GdiPlus.lib
HCCore.lib
HCNetSDK.LIB
PlayCtrl.lib
HCAlarm.lib
HCGeneralCfgMgr.lib
HCPreview.lib
配置完成後就可以新增CPP檔案。

#include <cstdlib>
#include <cstring>
#include <iostream>
#include "Windows.h"
#include "HCNetSDK.h"
#include "plaympeg4.h"
#include <opencv2\opencv.hpp>
#include <time.h>

using namespace std;
using namespace cv;

LONG nPort = -1;

volatile int gbHandling = 3;

//解碼回撥 視訊為YUV資料(YV12),音訊為PCM資料
void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2) { if (gbHandling) { gbHandling--; return; } long lFrameType = pFrameInfo->nType; if (lFrameType == T_YV12) { Mat pImg(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3); Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, pBuf); cvtColor(src, pImg, CV_YUV2BGR_YV12); // Sleep(-1); imshow("IPCamera", pImg); waitKey(1); } gbHandling = 3; } ///實時流回調 void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser) { switch (dwDataType) { case NET_DVR_SYSHEAD: //系統頭 if (!PlayM4_GetPort(&nPort)) //獲取播放庫未使用的通道號 { break; } //m_iPort = lPort; //第一次回撥的是系統頭,將獲取的播放庫port號賦值給全域性port,下次回撥資料時即使用此port號播放 if (dwBufSize > 0) { if (!PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME)) //設定實時流播放模式 { break; } if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 10 * 1024 * 1024)) //開啟流介面 { break; } if (!PlayM4_Play(nPort, NULL)) //播放開始 { break; } if (!PlayM4_SetDecCallBack(nPort, DecCBFun)) { break; } } break; case NET_DVR_STREAMDATA: //碼流資料 if (dwBufSize > 0 && nPort != -1) { if (!PlayM4_InputData(nPort, pBuffer, dwBufSize)) { cout << "error" << PlayM4_GetLastError(nPort) << endl; break; } } break; default: //其他資料 if (dwBufSize > 0 && nPort != -1) { if (!PlayM4_InputData(nPort, pBuffer, dwBufSize)) { break; } } break; } } void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser) { char tempbuf[256] = { 0 }; switch (dwType) { case EXCEPTION_RECONNECT: //預覽時重連 printf("----------reconnect--------%d\n", time(NULL)); break; default: break; } } void main() { //--------------------------------------- // 初始化 NET_DVR_Init(); //設定連線時間與重連時間 NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); //--------------------------------------- // 註冊裝置 LONG lUserID; NET_DVR_DEVICEINFO_V30 struDeviceInfo; lUserID = NET_DVR_Login_V30("169.254.42.127", 8000, "admin", "keylab88", &struDeviceInfo); if (lUserID < 0) { printf("Login error, %d\n", NET_DVR_GetLastError()); NET_DVR_Cleanup(); return; } //--------------------------------------- //設定異常訊息回撥函式 NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL); //--------------------------------------- //啟動預覽並設定回撥資料流 LONG lRealPlayHandle; cvNamedWindow("Mywindow", 0); cvNamedWindow("IPCamera", 0); HWND h = (HWND)cvGetWindowHandle("Mywindow"); if (h == 0) { cout << "視窗建立失敗" << endl; } NET_DVR_PREVIEWINFO struPlayInfo = { 0 }; struPlayInfo.hPlayWnd = h; //需要SDK解碼時控制代碼設為有效值,僅取流不解碼時可設為空 struPlayInfo.lChannel = 1; //預覽通道號 struPlayInfo.dwStreamType = 0; //0-主碼流,1-子碼流,2-碼流3,3-碼流4,以此類推 struPlayInfo.dwLinkMode = 0; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, fRealDataCallBack, NULL); if (lRealPlayHandle < 0) { printf("NET_DVR_RealPlay_V40 error\n"); printf("%d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return; } waitKey(); Sleep(-1); //--------------------------------------- //關閉預覽 NET_DVR_StopRealPlay(lRealPlayHandle); //登出使用者 NET_DVR_Logout(lUserID); //釋放SDK資源 NET_DVR_Cleanup(); return; }

這裡寫圖片描述