编译运行参考:ubuntu20.04下开发海康威视网络摄像头sdk(一)运行示例程序
然后是控制摄像头转动:
初始化和左转
我直接在这个截图的函数中修改的:
参考博客:https://blog.csdn.net/MKraul/article/details/106315701
我改的代码 CapPicture.cpp
/*
* Copyright(C) 2010,Hikvision Digital Technology Co., Ltd
*
* File name CapPicture.cpp
* Discription
* Version 1.0
* Author panyd
* Create Date 2010_3_25
* Modification History
*/
#include <stdio.h>
#include <iostream>
#include "CapPicture.h"
#include "public.h"
#include <string.h>
#include <unistd.h>
#include <vector>
using namespace std;
//相机的角度参数是十六进制,需要和十进制来回切换
int HexToDecMa(int wHex)//十六进制转十进制
{
return (wHex / 4096) * 1000 + ((wHex % 4096) / 256) * 100 + ((wHex % 256) / 16) * 10 + (wHex % 16);
}
int DEC2HEX_doc(int x)//十进制转十六进制
{
return (x / 1000) * 4096 + ((x % 1000) / 100) * 256 + ((x % 100) / 10) * 16 + x % 10;
}
int Demo_Capture()
{
NET_DVR_Init(); //初始化SDK
long lUserID;
//login
NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};//用户注册设备
struLoginInfo.bUseAsynLogin = false; //bUseAsynLogin为0时登录为同步模式 为1时为异步登陆
struLoginInfo.wPort = 8000;
memcpy(struLoginInfo.sDeviceAddress, "10.102.23.130", NET_DVR_DEV_ADDRESS_MAX_LEN);
memcpy(struLoginInfo.sUserName, "admin", NAME_LEN);
memcpy(struLoginInfo.sPassword, "xxxxxx", NAME_LEN);
//NET_DVR_Login_V40 该接口返回-1表示登录失败,其他值表示返回的用户ID值。用户ID具有唯一性,后续对设备的操作都需要通过此ID实现
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
if (lUserID < 0) //登陆失败
{
printf("pyd1---Login error, %d\n", NET_DVR_GetLastError());
return HPR_ERROR;
}
//截图功能
// NET_DVR_JPEGPARA strPicPara = {0};
// strPicPara.wPicQuality = 2;
// strPicPara.wPicSize = 0;
// int iRet;
// iRet = NET_DVR_CaptureJPEGPicture(lUserID, struDeviceInfoV40.struDeviceV30.byStartChan, &strPicPara, "./ssss.jpeg");
// if (!iRet)
// {
// printf("pyd1---NET_DVR_CaptureJPEGPicture error, %d\n", NET_DVR_GetLastError());
// return HPR_ERROR;
// }
//截图完成
//读取相机信息
NET_DVR_PTZPOS m_ptzPosCurrent;
DWORD dwtmp;
//获取位姿信息
bool a = NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZPOS, 0, &m_ptzPosCurrent, sizeof(NET_DVR_PTZPOS), &dwtmp);
//wPanPos P参数(水平参数)
//wTiltPos T参数(垂直参数)
//wZoomPos Z参数(变焦参数)
//HexToDecMa 十六进制转换十进制
int m_iPara1 = HexToDecMa(m_ptzPosCurrent.wPanPos);
int m_iPara2 = HexToDecMa(m_ptzPosCurrent.wTiltPos);
int m_iPara3 = HexToDecMa(m_ptzPosCurrent.wZoomPos);
vector<int> TempPosture(3);//三个参数代表含义如下cout所示
//NET_DVR_PTZPOS 里面获取的参数是实际显示值的10倍 所以要除以10 具体参考开发手册里面的解释
TempPosture[0] = m_iPara1 / 10 ;
TempPosture[1] = m_iPara2 / 10 ;
TempPosture[2] = m_iPara3 / 10 ;
cout << "##################当前参数#####################"<< endl;
cout << "P水平方向 " << TempPosture[0] << " ##" << endl;
cout << "T仰角 " << TempPosture[1] << " ##" << endl;
cout << "Z焦距 " << TempPosture[2] << " ##" << endl;
cout << "###############################################"<< endl;
//开始控制摄像头转动
printf("下面控制摄像头转动!\n");
// //初始化操作 水平角度为0度 仰角45度
// //定义要初始化的参数
// vector<int> TargetPosture(3);
// TargetPosture[0]=(0);
// TargetPosture[1]=(45);
// TargetPosture[2]=(1);
// //将参数转化为十六进制 记得再乘以10
// m_ptzPosCurrent.wPanPos = DEC2HEX_doc(TargetPosture[0]*10);
// m_ptzPosCurrent.wTiltPos = DEC2HEX_doc(TargetPosture[1]*10);
// m_ptzPosCurrent.wZoomPos = DEC2HEX_doc(TargetPosture[2]*10);
// // 开始转动
// bool b=NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_PTZPOS, 0, &m_ptzPosCurrent, sizeof(NET_DVR_PTZPOS));
// sleep(6);
// TempPosture[0] = TargetPosture[0];
// TempPosture[1] = TargetPosture[1];
// TempPosture[2] = TargetPosture[2];
// cout << "##################初始化后参数#####################"<< endl;
// cout << "P水平方向 " << TempPosture[0] << " ##" << endl;
// cout << "T仰角 " << TempPosture[1] << " ##" << endl;
// cout << "Z焦距 " << TempPosture[2] << " ##" << endl;
// cout << "###############################################"<< endl;
//下面是使相机在本来的基础上左转
//设置想要变化的参数值
vector<int> Changevalue(3);
Changevalue[0]=(45);//degree水平加30度
Changevalue[1]=(0);//degree俯仰加0度
Changevalue[2]= (0);//焦距加0
//将变化值更新
vector<int> TargetPosture(3);
TargetPosture[0]=TempPosture[0]+Changevalue[0];
TargetPosture[1]=TempPosture[1]+Changevalue[1];
TargetPosture[2]=TempPosture[2]+Changevalue[2];
//将变化后的参数转化为相机自带的十六进制 记得再乘以10
m_ptzPosCurrent.wPanPos = DEC2HEX_doc(TargetPosture[0]*10);
m_ptzPosCurrent.wTiltPos = DEC2HEX_doc(TargetPosture[1]*10);
m_ptzPosCurrent.wZoomPos = DEC2HEX_doc(TargetPosture[2]*10);
//将参数传入函数,相机开始调整位置
bool c=NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_PTZPOS, 0, &m_ptzPosCurrent, sizeof(NET_DVR_PTZPOS));
sleep(4);
//更新此时的位置参数 打印出来展示
TempPosture[0] = TargetPosture[0];
TempPosture[1] = TargetPosture[1];
TempPosture[2] = TargetPosture[2];
printf("摄像头转动完成!");
cout << "####################调整之后#######################"<< endl;
cout << "P水平方向 " << TempPosture[0] << " #" << endl;
cout << "T仰角 " << TempPosture[1] << " #" << endl;
cout << "Z焦距 " << TempPosture[2] << " #" << endl;
cout << "###############################################"<< endl;
//logout
NET_DVR_Logout_V30(lUserID);//注销设备
NET_DVR_Cleanup();//释放SDK资源
return HPR_OK;
}