海康相机型号: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")
主要程序如下:
#include <cstdio>
#include <iostream>
#include <ctime>
#include <Windows.h>
#include "HCNetSDK.h"
#include "highgui.h"
#include "cv.h" #pragma comment( lib, "opencv_highgui249d.lib")
#pragma comment( lib, "opencv_core249d.lib")
#pragma comment( lib, "opencv_imgproc249d.lib")
using namespace cv;
using namespace std; //typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
//PROCGETCONSOLEWINDOW GetConsoleWindow; int main(int argc, char * argv[])
{
//---------------------------------------
// 初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(, );
NET_DVR_SetReconnect(, true); //---------------------------------------
//获取控制台窗口句柄
//HMODULE hKernel32 = GetModuleHandle((LPCWSTR)"kernel32");
//GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow"); //---------------------------------------
// 注册设备
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30("192.168.199.3", , "admin", "guoji123", &struDeviceInfo);
if (lUserID < )
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return -;
} //---------------------------------------
//cvNamedWindow("camera",CV_WINDOW_AUTOSIZE);
IplImage* frame;
//定义JPEG图像质量
LPNET_DVR_JPEGPARA JpegPara = new NET_DVR_JPEGPARA;
JpegPara->wPicQuality = ;
JpegPara->wPicSize = ; char * Jpeg = new char[*];
DWORD len = *;
LPDWORD Ret = ; if(!NET_DVR_SetCapturePictureMode(BMP_MODE))
{
cout<<"Set Capture Picture Mode error!"<<endl;
cout<<"The error code is "<<NET_DVR_GetLastError()<<endl;
} //bool capture = NET_DVR_CaptureJPEGPicture(lUserID,1,JpegPara,"1111");
vector<char>data(*);
while()
{
bool capture = NET_DVR_CaptureJPEGPicture_NEW(lUserID,,JpegPara,Jpeg,len,Ret);
if(!capture)
{
printf("Error: NET_DVR_CaptureJPEGPicture_NEW = %d", NET_DVR_GetLastError());
return -;
} for(int i=;i<*;i++)
data[i] = Jpeg[i]; Mat img = imdecode(Mat(data),);
imshow("camera",img);
waitKey(); } //FILE * fp = fopen("3.jpg","wb");
//fwrite(Jpeg,1,123*1024,fp);
//fclose(fp); return ;
}