海康相机型号:DS-2CD2512F-IS
参考连接http://blog.csdn.net/wanghuiqi2008/article/details/31404571
先上效果图
其中,在连接时遇到了X64与X86兼容问题:
海康SDK在X64(配置时用的时X64),程序运行时采用的X64,但opencv2.4.9配置时又采用的时X86,所以在使用X64的时候,会出现opencv找不到lib文件,所有在程序中添加了以下代码:
#pragma comment( lib, "opencv_highgui249d.lib") #pragma comment( lib, "opencv_core249d.lib") #pragma comment( lib, "opencv_imgproc249d.lib")
主要程序如下:
1 #include <cstdio> 2 #include <iostream> 3 #include <ctime> 4 #include <Windows.h> 5 #include "HCNetSDK.h" 6 #include "highgui.h" 7 #include "cv.h" 8 9 #pragma comment( lib, "opencv_highgui249d.lib") 10 #pragma comment( lib, "opencv_core249d.lib") 11 #pragma comment( lib, "opencv_imgproc249d.lib") 12 using namespace cv; 13 using namespace std; 14 15 16 //typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)(); 17 //PROCGETCONSOLEWINDOW GetConsoleWindow; 18 19 int main(int argc, char * argv[]) 20 { 21 //--------------------------------------- 22 // 初始化 23 NET_DVR_Init(); 24 //设置连接时间与重连时间 25 NET_DVR_SetConnectTime(2000, 1); 26 NET_DVR_SetReconnect(10000, true); 27 28 //--------------------------------------- 29 //获取控制台窗口句柄 30 //HMODULE hKernel32 = GetModuleHandle((LPCWSTR)"kernel32"); 31 //GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow"); 32 33 34 //--------------------------------------- 35 // 注册设备 36 LONG lUserID; 37 NET_DVR_DEVICEINFO_V30 struDeviceInfo; 38 lUserID = NET_DVR_Login_V30("192.168.199.3", 8000, "admin", "guoji123", &struDeviceInfo); 39 if (lUserID < 0) 40 { 41 printf("Login error, %d\n", NET_DVR_GetLastError()); 42 NET_DVR_Cleanup(); 43 return -1; 44 } 45 46 47 //--------------------------------------- 48 //cvNamedWindow("camera",CV_WINDOW_AUTOSIZE); 49 IplImage* frame; 50 //定义JPEG图像质量 51 LPNET_DVR_JPEGPARA JpegPara = new NET_DVR_JPEGPARA; 52 JpegPara->wPicQuality = 0; 53 JpegPara->wPicSize = 9; 54 55 char * Jpeg = new char[200*1024]; 56 DWORD len = 200*1024; 57 LPDWORD Ret = 0; 58 59 if(!NET_DVR_SetCapturePictureMode(BMP_MODE)) 60 { 61 cout<<"Set Capture Picture Mode error!"<<endl; 62 cout<<"The error code is "<<NET_DVR_GetLastError()<<endl; 63 } 64 65 //bool capture = NET_DVR_CaptureJPEGPicture(lUserID,1,JpegPara,"1111"); 66 vector<char>data(200*1024); 67 while(1) 68 { 69 bool capture = NET_DVR_CaptureJPEGPicture_NEW(lUserID,1,JpegPara,Jpeg,len,Ret); 70 if(!capture) 71 { 72 printf("Error: NET_DVR_CaptureJPEGPicture_NEW = %d", NET_DVR_GetLastError()); 73 return -1; 74 } 75 76 for(int i=0;i<200*1024;i++) 77 data[i] = Jpeg[i]; 78 79 Mat img = imdecode(Mat(data),1); 80 imshow("camera",img); 81 waitKey(1); 82 83 } 84 85 //FILE * fp = fopen("3.jpg","wb"); 86 //fwrite(Jpeg,1,123*1024,fp); 87 //fclose(fp); 88 89 return 0; 90 }