#include <iostream>
#include <thread>
#include "HCNetSDK.h"
#pragma comment(lib,"GdiPlus.lib")
#pragma comment(lib,"HCCore.lib")
#pragma comment(lib,"HCNetSDK.lib")
#pragma comment(lib,"PlayCtrl.lib")
int main(int argc, char **argv)
{
NET_DVR_Init();
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
struLoginInfo.bUseAsynLogin = 0; //同步登录方式
::strcpy(struLoginInfo.sDeviceAddress, "10.10.1.9"); //设备IP地址
struLoginInfo.wPort = 8000; //设备服务端口
::strcpy(struLoginInfo.sUserName, "admin"); //设备登录用户名
::strcpy(struLoginInfo.sPassword, "123ABCabc"); //设备登录密码
//设备信息, 输出参数
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
int userId = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
if (userId == -1)
{
DWORD code = NET_DVR_GetLastError();
std::cout << "NET_DVR_Login_V40 failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
NET_DVR_Logout(userId);
//释放SDK资源
NET_DVR_Cleanup();
::getchar();
return -1;
}
std::cout << "login success user " << userId << std::endl;
DWORD bytesReturned = 0;
NET_DVR_DEVICECFG devCfg;
devCfg.dwSize = sizeof(NET_DVR_DEVICECFG);
bool resCode = NET_DVR_GetDVRConfig(userId, NET_DVR_GET_DEVICECFG, -1, &devCfg, sizeof(NET_DVR_DEVICECFG), &bytesReturned);
if (!resCode)
{
DWORD code = NET_DVR_GetLastError();
std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
NET_DVR_Logout(userId);
//释放SDK资源
NET_DVR_Cleanup();
::getchar();
return -1;
}
std::cout << "设备名: " << devCfg.sDVRName << " 序列号: " << devCfg.sSerialNumber << " 设备ID号: " << devCfg.dwDVRID << " 设备类型: " << (int)devCfg.byDVRType << std::endl;
NET_DVR_IPPARACFG_V40 ipcfg;
bytesReturned = 0;
ipcfg.dwSize = sizeof(NET_DVR_IPPARACFG_V40);
int iGroupNO = 0;
resCode = NET_DVR_GetDVRConfig(userId, NET_DVR_GET_IPPARACFG_V40, iGroupNO, &ipcfg, sizeof(NET_DVR_IPPARACFG_V40), &bytesReturned);
if (!resCode)
{
DWORD code = NET_DVR_GetLastError();
std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
NET_DVR_Logout(userId);
//释放SDK资源
NET_DVR_Cleanup();
::getchar();
return -1;
}
std::cout << "设备组 " << ipcfg.dwGroupNum << " 数字通道个数 " << ipcfg.dwDChanNum << " 起始通道 " << ipcfg.dwStartDChan << std::endl;
for (int i = 0; i < ipcfg.dwDChanNum; i++)
{
switch (ipcfg.struStreamMode[i].byGetStreamType)
{
case 0:
if (ipcfg.struStreamMode[i].uGetStream.struChanInfo.byEnable)
{
int byIPID = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPID;
int byIPIDHigh = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPIDHigh;
int iDevInfoIndex = byIPIDHigh * 256 + byIPID - 1 - iGroupNO * 64;
printf("通道号%d 在线 摄像头取流, IP: %s\n", i + 1, ipcfg.struIPDevInfo[iDevInfoIndex].struIP.sIpV4);
}
else {
int byIPID = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPID;
int byIPIDHigh = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPIDHigh;
int iDevInfoIndex = byIPIDHigh * 256 + byIPID - 1 - iGroupNO * 64;
std::cout << "IP: " << ipcfg.struIPDevInfo[iDevInfoIndex].struIP.sIpV4 << " 离线" << std::endl;
}
break;
case 1:
if (ipcfg.struStreamMode[i].uGetStream.struPUStream.struStreamMediaSvrCfg.byValid)
{
printf("IP channel %d connected with the IP device by stream server.\n", i + 1);
printf("IP of stream server: %s, IP of IP device: %s\n", ipcfg.struStreamMode[i].uGetStream.\
struPUStream.struStreamMediaSvrCfg.struDevIP.sIpV4, ipcfg.struStreamMode[i].uGetStream.\
struPUStream.struDevChanInfo.struIP.sIpV4);
}
break;
}
NET_DVR_RECORD_CHECK_COND recordChk;
NET_DVR_STREAM_INFO sInfo;
::memset(sInfo.byID, 0, STREAM_ID_LEN);
sInfo.dwChannel = i + 1;
sInfo.dwSize = sizeof(NET_DVR_STREAM_INFO);
NET_DVR_TIME_EX startT, endT;
startT.wYear = 2020;
startT.byMonth = 7;
startT.byDay = 1;
startT.byHour = 0;
startT.byMinute = 0;
startT.bySecond = 0;
endT.wYear = 2020;
endT.byMonth = 7;
endT.byDay = 1;
endT.byHour = 23;
endT.byMinute = 59;
endT.bySecond = 59;
recordChk.dwSize = sizeof(NET_DVR_RECORD_CHECK_COND);
recordChk.struStreamInfo = sInfo;
recordChk.byCheckType = 0;
recordChk.struBeginTime = startT;
recordChk.struEndTime = endT;
LONG recordHandle = NET_DVR_StartRemoteConfig(userId, NET_DVR_RECORD_CHECK, &recordChk, sizeof(NET_DVR_RECORD_CHECK_COND), [](DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData) {
switch (dwType)
{
case NET_SDK_CALLBACK_TYPE_STATUS:
std::cout << "status len " << dwBufLen << std::endl;
break;
case NET_SDK_CALLBACK_TYPE_PROGRESS:
std::cout << "progress len " << dwBufLen << std::endl;
break;
case NET_SDK_CALLBACK_TYPE_DATA:
std::cout << "data len " << dwBufLen << std::endl;
break;
default:
break;
}
}, nullptr);
NET_DVR_RECORD_SEGMENT_RET recordData;
recordData.dwSize = sizeof(NET_DVR_RECORD_SEGMENT_RET);
LONG code = 1000;
while (code == 1000)
{
code = NET_DVR_GetNextRemoteConfig(recordHandle, &recordData, sizeof(NET_DVR_RECORD_SEGMENT_RET));
while (code == 1001)
{
code = NET_DVR_GetNextRemoteConfig(recordHandle, &recordData, sizeof(NET_DVR_RECORD_SEGMENT_RET));
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "录像大小 " << recordData.dwRecordTotalSize << std::endl;
recordData.dwRecordTotalSize = 0;
}
std::cout << "用户名 " << ipcfg.struIPDevInfo[i].sUserName << " 密码 " << ipcfg.struIPDevInfo[i].sPassword << " 设备ID " << (int)ipcfg.struIPDevInfo[i].szDeviceID << " ip地址 " << ipcfg.struIPDevInfo[i].struIP.sIpV4 << " 端口 " << ipcfg.struIPDevInfo[i].wDVRPort << " 设备类型: " << (int)ipcfg.struIPDevInfo[i].byCameraType << std::endl << std::endl;
}
::getchar();
NET_DVR_Logout(userId);
//释放SDK资源
NET_DVR_Cleanup();
return 0;
}
海康NVR SDK获取所有摄像头ip及配置
最新推荐文章于 2025-03-28 16:53:34 发布