概述
材料:opencv2.4.7,vs2010,海康威视网络摄像头,海康SDK(win64)
一. OpenCV2.4.7+vs2010配置
1、设置环境变量:
我的电脑--->右键--->属性--->高级--->环境变量--->在用户变量里,新建path和OPENCV两个项
(根据自己的OpenCV下载路径进行配置,我下载在了E:OpenCV2中)
步骤1:在path中添加 E:OpenCV2opencvbuildx86vc10bin,和 E:OpenCV2opencvbuildx64vc11 就是添加dll文件
步骤2:添加OPENCV的环境变量E:OpenCV2opencvbuild
2、打开一个Opencv的工程,点菜单目录的“视图”--->属性管理器--->双击项目名称
步骤3:在配置属性--->VC++目录--->包含目录文件中添加
E:OpenCV2opencvbuildinclude
E:OpenCV2opencvbuildincludeopencv
E:OpenCV2opencvbuildincludeopencv2
E:OpenCV2opencvbuildincludeopencv2highgui
E:OpenCV2opencvbuildincludeopencv2imgproc
E:OpenCV2opencvbuildincludeopencv2core
步骤4::在配置属性--->VC++目录--->库目录中添加
E:OpenCV2opencvbuildx86vc11lib
步骤5 添加lib库文件, 在配置debug里--->项目属性--->链接器--->输入--->附加依赖库 里加上
opencv_calib3d247d.lib
opencv_contrib247d.lib
opencv_core247d.lib
opencv_features2d247d.lib
opencv_flann247d.lib
opencv_gpu247d.lib
opencv_highgui247d.lib
opencv_imgproc247d.lib
opencv_legacy247d.lib
opencv_ml247d.lib
opencv_nonfree247d.lib
opencv_objdetect247d.lib
opencv_photo247d.lib
opencv_stitching247d.lib
opencv_ts247d.lib
opencv_video247d.lib
opencv_videostab247d.lib
步骤6:在配置release里--->项目属性--->链接器--->输入--->附加依赖库 里加上
opencv_calib3d247.lib
opencv_contrib247.lib
opencv_core247.lib
opencv_features2d247.lib
opencv_flann247.lib
opencv_gpu247.lib
opencv_highgui247.lib
opencv_imgproc247.lib
opencv_legacy247.lib
opencv_ml247.lib
opencv_nonfree247.lib
opencv_objdetect247.lib
opencv_photo247.lib
opencv_stitching247.lib
opencv_ts247.lib
opencv_video247.lib
opencv_videostab247.lib
opencv测试实例:
//开始简单的实例:
#include <core/core.hpp> //注意路径
#include <highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat image;
image = imread("H:\Image\lena.jpg"); // Read the file,argv[1];注意是双斜杠!
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
二. SDK+vs2010环境配置(预览)
首先,将网络摄像头接上电源,并通过网线接入电脑。在官网下载SADP,安装好后名字叫设备网络搜索,打开设备网路搜索点击刷新,观察设备是否在线(要保证你的设备ip与电脑ip在同一网段,即前三位相同,若已根据网上教程设置好设备的ip,账户名和密码,则要改正电脑ip与设备ip处于同一网段,相关教程自行百度)。
(win10)更改电脑ip步骤:网络和Internet设置->更改适配器选项->双击除了WiFi的另一个 (我的这里面叫以太网)->属性->双击Internet协议版本4(TCP/IPv4)->点击使用下面的IP地址(s),即可更改ip(注意:ip前三位和默认网关前三位于摄像机ip前三位相同,子网掩码设置为默认值
然后双击app中设备的ip名,进入网站(一般使用IE)进行登陆,注意此时要断开电脑的wifi。输入账户和密码登陆后,即可观察实时预览情况。
在VS2010中进行海康威视网络摄像头二次开发:
1.vs2010中新建工程
2.配置属性页 注:平台改为(x64)(因为我的SDK是win64版本,并下载于 E:HIKSDKx64 文件夹中)
(1)VC++目录->包含目录->添加OpenCV的相关文件(见一),添加海康SDK中头文件路径(win64)
E:HIKSDKx64CH-HCNetSDKV6.0.2.35_build20190411_Win64头文件
(2)VC++目录->库目录->添加OpenCV的相关文件(见一),添加海康SDK中库文件路径(win64)
E:HIKSDKx64CH-HCNetSDKV6.0.2.35_build20190411_Win64库文件
(3)链接器->输入->附加依赖项->添加OpenCV的相关依赖项,添加海康SDK中附加依赖项
HCNetSDK.lib
GdiPlus.lib
HCAlarm.lib
HCCore.lib
HCGeneralCfgMgr.lib
HCPreview.lib
PlayCtrl.lib
(4)运行程序会出现缺少相关dll文件情况,在文件夹x64的debug文件夹中添加以下文件(这个debug中原本含有.exe文件)
HCNetSDKCom文件夹(不能更改名字)
AudioRender.dll
GdiPlus.dll
HCCore.dll
HCNetSDK.dll
PlayCtrl.dll
SuperRender.dll
注:程序中加入#include "plaympeg4.h" 预览前加入语句 PlayM4_GetLastError(0); 要不然会出现64号问题
预览+抓图程序如下
// hkproject02.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "Windows.h"
#include "HCNetSDK.h"
#include "plaympeg4.h"
#include <time.h>
#include <string>
using namespace std;
//typedef HWND(WINAPI *PROCGETCONSOLEWINDOW)();
//PROCGETCONSOLEWINDOW GetConsoleWindow;
void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
char tempbuf[256] = { 0 };
switch (dwType)
{
case EXCEPTION_RECONNECT: //预览时重连
printf("----------reconnect--------%dn", time(NULL));
break;
default:
break;
}
}
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle((LPWSTR)pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
wsprintf((LPWSTR)pszNewWindowTitle, (LPCWSTR)"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle((LPCWSTR)pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound = FindWindow(NULL, (LPCWSTR)pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle((LPCWSTR)pszOldWindowTitle);
return(hwndFound);
}
void main() {
//---------------------------------------
//初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//---------------------------------------
// 获取控制台窗口句柄
//HMODULE hKernel32 = GetModuleHandle("kernel32");
//GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32, "GetConsoleWindow");
//---------------------------------------
// 注册设备
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30("192.168.0.65", 8000, "admin", "aaaa123456", &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %dn", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
//---------------------------------------
//注册消息异常回调函数
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
PlayM4_GetLastError(0);
//---------------------------------------
//启动预览并设置回调数据流
LONG lRealPlayHandle;
HWND hWnd = GetConsoleWindow(); //获取窗口句柄
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = hWnd; //需要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
struPlayInfo.bBlocked = 1; //0- 非阻塞取流,1- 阻塞取流
lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, NULL, NULL);
//判断出的什么错误
DWORD s;
s = NET_DVR_GetLastError();
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V40 errorn");
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
int o = -1;
for (int i = 0; i < 10; i++)
{
string a = "E:\c++\HKsdk\hkproject02\out\";
char c = '1' + i;
a.push_back(c);
a = a + ".bmp";
//抓图
o = NET_DVR_CapturePicture(lRealPlayHandle, const_cast<char*>(a.c_str()));
Sleep(1000);
}
Sleep(10000);
//关闭预览
NET_DVR_StopRealPlay(lRealPlayHandle);
//注销用户
NET_DVR_Logout(lUserID);
//释放SDK 资源
NET_DVR_Cleanup();
return;
}
最后
以上就是高大故事为你收集整理的opencv+海康威视网络摄像头实时预览+抓图(环境配置加程序)一. OpenCV2.4.7+vs2010配置二. SDK+vs2010环境配置(预览)的全部内容,希望文章能够帮你解决opencv+海康威视网络摄像头实时预览+抓图(环境配置加程序)一. OpenCV2.4.7+vs2010配置二. SDK+vs2010环境配置(预览)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复