使用大华SDK获取NVR通道配置信息(含通道名称)
一:出发点
在一些视频监控项目中经常需要对挂载在NVR下的摄像头进行配置。
由于这些配置信息在NVR里已经存在,如果能将NVR中的通道配置信息导出,会大大减少前端的配置工作。
网上的通过海康SDK查询NVR通道信息的帖子很多,但是对大华SDK的帖子缺很少,我在查阅了大华SDK的文档后使用C++实现了对通道名称、用户名、设备IP地址的读取。
现在把实现方式贴出来,希望对有需要的人提供些参考。
二:使用方法
1.CLIENT_GetNewDevConfig函数
![](https://img2020.cnblogs.com/blog/2607547/202110/2607547-20211029104646867-1925797073.png)
2.使用CFG_CMD_REMOTEDEVICE参数获取网络连接信息,使用CFG_CMD_CHANNELTITLE参数获取通道名称。
3.由于CLIENT_GetNewDevConfig函数返回的是JSON格式的数据,因此需要对JSON数据进行解析处理。
三.主要代码:
#include <iostream> #include <Windows.h> #include "dhnetsdk.h" #include "dhplay.h" #include "dhconfigsdk.h" #include "json/json.h" void __stdcall DisConnectFunc(LLONG lLoginID, char* pchDVRIP, LONG nDVRPort, LDWORD dwUser) { printf("Device disconnect, IP=%s, Port=%d\n", pchDVRIP, nDVRPort); } void __stdcall SubDisConnectFunc(EM_INTERFACE_TYPE emInterfaceType, BOOL bOnline, LLONG lOperateHandle, LLONG lLoginID, LDWORD dwUser) { switch (emInterfaceType) { case DH_INTERFACE_REALPLAY: printf("实时监视接口: Short connect is %d\n", bOnline); break; case DH_INTERFACE_PREVIEW: printf("多画面预览接口: Short connect is %d\n", bOnline); break; case DH_INTERFACE_PLAYBACK: printf("回放接口: Short connect is %d\n", bOnline); break; case DH_INTERFACE_DOWNLOAD: printf("下载接口: Short connect is %d\n", bOnline); break; default: break; } } std::string Utf8ToGbk(const char* src_str) { int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0); wchar_t* wszGBK = new wchar_t[len + 1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char* szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL); std::string strTemp(szGBK); if (wszGBK) delete[] wszGBK; if (szGBK) delete[] szGBK; return strTemp; } int main() { //初始化 CLIENT_Init(DisConnectFunc, 0); CLIENT_SetSubconnCallBack(SubDisConnectFunc, 0); NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY stInparam; memset(&stInparam, 0, sizeof(stInparam)); stInparam.dwSize = sizeof(stInparam); strncpy(stInparam.szIP, "192.168.31.242", sizeof(stInparam.szIP) - 1); strncpy(stInparam.szUserName, "admin", sizeof(stInparam.szUserName) - 1); strncpy(stInparam.szPassword, "111111", sizeof(stInparam.szPassword) - 1); stInparam.nPort = 37777; stInparam.emSpecCap = EM_LOGIN_SPEC_CAP_TCP; NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY stOutparam; memset(&stOutparam, 0, sizeof(stOutparam)); stOutparam.dwSize = sizeof(stOutparam); LLONG lLoginHandle = NULL; LLONG lRealHandle = NULL; LLONG lSearch = NULL; //注册用户到设备 lLoginHandle = CLIENT_LoginWithHighLevelSecurity(&stInparam, &stOutparam); DWORD dwError = CLIENT_GetLastError() & (0x7fffffff); Json::Value return_root; Json::Reader reader; Json::Value value; Json::FastWriter return_writer; int nerror = 0; char* szOutBuffer = new char[32 * 1024]; memset(szOutBuffer, 0, 32 * 1024); //获取网络信息 BOOL r1 = CLIENT_GetNewDevConfig(lLoginHandle, (char*)CFG_CMD_REMOTEDEVICE, 2, szOutBuffer, 32 * 1024, &nerror); int b = CLIENT_GetLastError() & (0x7fffffff); reader.parse(szOutBuffer, value); Json::Value devs = value["params"]["table"]; Json::Value::Members member = devs.getMemberNames(); int i = 0; for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); ++iter) { if (devs[*iter]["Enable"].asBool()) { Json::Value item; item["Channel"] = ++i; item["Ip"] = devs[*iter]["Address"]; return_root.append(item); } } //获取通道名称 memset(szOutBuffer, 0, 32 * 1024); BOOL r2 = CLIENT_GetNewDevConfig(lLoginHandle, (char*)CFG_CMD_CHANNELTITLE, -1, szOutBuffer, 32 * 1024, &nerror); reader.parse(szOutBuffer, value); Json::Value channels = value["params"]["table"]; for (unsigned int i = 0; i < return_root.size(); ++i) { std::string name = channels[i]["Name"].asString(); return_root[i]["Name"] = Utf8ToGbk(name.c_str()); } std::string result = return_writer.write(return_root); std::cout << result << std::endl; delete szOutBuffer; }
四.输出结果: