opencv实时获取海康摄像头PTZ数据(角度俯仰角变焦)并显示图像

效果:

 代码:

#include"stdafx.h"
#include <cstdio>
#include <iostream>
#include <ctime>
#include <Windows.h>
#include "HCNetSDK.h"
#include "highgui.h"
#include "cv.h"
#include <opencv2\opencv.hpp>
#include <iomanip>
using namespace cv;
using namespace std;

//转换函数
DWORD HexToDecMa(DWORD wHex)
{
	return (wHex / 4096) * 1000 + ((wHex % 4096) / 256) * 100 + ((wHex % 256) / 16) * 10 + (wHex % 16);
}

int main(int argc, char * argv[])
{
	//---------------------------------------
	// 初始化
	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("XXX.XXX.X.X", 8000, "ADMIN", "ADMIN", &struDeviceInfo);
	if (lUserID < 0)
	{
		printf("Login error, %d\n", NET_DVR_GetLastError());
		NET_DVR_Cleanup();
		return -1;
	}
	//---------------------------------------
	//cvNamedWindow("camera",CV_WINDOW_AUTOSIZE);
	IplImage* frame;
	//定义JPEG图像质量
	LPNET_DVR_JPEGPARA JpegPara = new NET_DVR_JPEGPARA;
	JpegPara->wPicQuality = 0;
	JpegPara->wPicSize = 9;
	char * Jpeg = new char[200 * 1024];
	DWORD len = 200 * 1024;
	LPDWORD Ret = 0;

	if (!NET_DVR_SetCapturePictureMode(BMP_MODE))
	{
		cout << "Set Capture Picture Mode error!" << endl;
		cout << "The error code is " << NET_DVR_GetLastError() << endl;
	}
	vector<char>data(200 * 1024);
	//读取PTZ
	NET_DVR_PTZPOS m_ptzPosCurrent;
	DWORD dwtmp;
	while (1)
	{
		bool capture = NET_DVR_CaptureJPEGPicture_NEW(lUserID, 0, JpegPara, Jpeg, len, Ret);
		if (!capture)
		{
			printf("Error: NET_DVR_CaptureJPEGPicture_NEW = %d", NET_DVR_GetLastError());
			return -1;
		}
		//获取当前PTZ状态
		bool a = NET_DVR_GetDVRConfig(0, NET_DVR_GET_PTZPOS, 0, &m_ptzPosCurrent, sizeof(NET_DVR_PTZPOS), &dwtmp);
		//打印PTZ
		int m_iPara1 = HexToDecMa(m_ptzPosCurrent.wPanPos);
		int m_iPara2 = HexToDecMa(m_ptzPosCurrent.wTiltPos);
		int m_iPara3 = HexToDecMa(m_ptzPosCurrent.wZoomPos);
		cout << "P" << m_iPara1 / 10 + 1 << endl;
		cout << "T" << m_iPara2 / 10 + 1 << endl;
		cout << "Z" << m_iPara3 / 10 << endl;
		for (int i = 0; i < 200 * 1024; i++)
			data[i] = Jpeg[i];

		Mat img = imdecode(Mat(data), 1);
		imshow("camera", img);
		waitKey(1);
	}
	return 0;
}

 需要自己配置一些sdk的东西。自己去海康官网下载一下就好,记得注意一下通道号

发布了25 篇原创文章 · 获赞 28 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/maitianpt/article/details/85340727