我经常要把小区海康或者大华的硬盘录像机推送到自己搭建的gb28181监控平台,每次几百个摄像头编码,有点头大,就用了1个多周写了个批量设置海康和大华硬盘录像机的通道编码的程序,海康和大华的SDK简直不是人看的. 太乱了.
大华读取通道编码的代码
/// <summary>/// 获取通道编码id列表/// QQ : 771811549/// </summary>/// <returns>通道编码id列表</returns>public DataTable GetInputIDs(){DataTable dt = new DataTable();dt.Columns.Add("id", typeof(string));dt.Columns.Add("videoInputID", typeof(string));if (info.stuSipServerInfo[0].nChannelInfoNum > 0)// 释放内存{ Marshal.FreeHGlobal(info.stuSipServerInfo[0].pstuChannnelInfo); }info.stuSipServerInfo[0].nChannelInfoNum = (uint)m_DeviceInfo.nChanNum;//设置通道个数info.stuSipServerInfo[0].pstuChannnelInfo = Marshal.AllocHGlobal(channelInfoSize * m_DeviceInfo.nChanNum);//分配内存NETClient.CLIENT_GetConfig(m_LoginID, -1, ref info);//获取数据NET_SIP_SERVER_INFO ser = info.stuSipServerInfo[0];for (int j = 0; j < ser.nChannelInfoNum; j++){IntPtr currentPtr = new IntPtr(ser.pstuChannnelInfo.ToInt64() + (j * channelInfoSize));//获取数组偏移指针NET_CHANNEL_INFO channelInfo = (NET_CHANNEL_INFO)Marshal.PtrToStructure(currentPtr, typeof(NET_CHANNEL_INFO));//获取指针对象DataRow dr = dt.NewRow();dr["id"] = j + 1;//通道iddr["videoInputID"] = channelInfo.szID;//通道编号dt.Rows.Add(dr);}return dt;}
海康部分代码
public string XML透传(string Url, string Param = ""){CHCNetSDK.NET_DVR_XML_CONFIG_INPUT pInputXml = new CHCNetSDK.NET_DVR_XML_CONFIG_INPUT(); //XML透传接口Int32 nInSize = Marshal.SizeOf(pInputXml);pInputXml.dwSize = (uint)nInSize;//设置大小string strRequestUrl = Url;//设置urluint dwRequestUrlLen = (uint)strRequestUrl.Length;//url长度pInputXml.lpRequestUrl = Marshal.StringToHGlobalAnsi(strRequestUrl);//请求信令pInputXml.dwRequestUrlLen = dwRequestUrlLen;//设置长度string strInputParam = Param;//设置参数pInputXml.lpInBuffer = Marshal.StringToHGlobalAnsi(strInputParam);//设置缓冲区pInputXml.dwInBufferSize = (uint)strInputParam.Length;//设置缓冲区大小CHCNetSDK.NET_DVR_XML_CONFIG_OUTPUT pOutputXml = new CHCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();pOutputXml.dwSize = (uint)Marshal.SizeOf(pInputXml);//结构体大小pOutputXml.lpOutBuffer = Marshal.AllocHGlobal(3 * 1024 * 1024);//输出参数缓冲区pOutputXml.dwOutBufferSize = 3 * 1024 * 1024;//输出参数缓冲区大小pOutputXml.lpStatusBuffer = Marshal.AllocHGlobal(4096 * 4);//返回的状态参数pOutputXml.dwStatusSize = 4096 * 4; //状态缓冲区大小(内存大小) //透传if (!CHCNetSDK.NET_DVR_STDXMLConfig(m_lUserID, ref pInputXml, ref pOutputXml)){uint iLastErr = CHCNetSDK.NET_DVR_GetLastError();//失败MessageBox.Show("错误号 = " + iLastErr);//XML透传失败,输出错误号}//获取输出uint iXMSize = pOutputXml.dwReturnedXMLSize;byte[] managedArray = new byte[iXMSize];Marshal.Copy(pOutputXml.lpOutBuffer, managedArray, 0, (int)iXMSize);string jg = Encoding.UTF8.GetString(managedArray);//释放资源Marshal.FreeHGlobal(pInputXml.lpRequestUrl);Marshal.FreeHGlobal(pOutputXml.lpOutBuffer);Marshal.FreeHGlobal(pOutputXml.lpStatusBuffer);return jg;}
软件截图: