CarNoCls.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace CarLocalMeter
  10. {
  11. /// <summary>
  12. /// 摄像头车号获取
  13. /// </summary>
  14. public class CarNoCls
  15. {
  16. #region
  17. private bool m_bInitSDK = false;
  18. private uint iLastErr = 0;
  19. private Int32 m_lUserID = -1;
  20. private Int32 m_lRealHandle = -1;
  21. //int iFileNumber = 0;
  22. private int[] iChannelNum = new int[96];
  23. private Int32[] m_lAlarmHandle = new Int32[200];
  24. private CHCNetSDK.MSGCallBack_V31 m_falarmData_V31 = null;
  25. private CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
  26. public delegate void MyDebugInfo(string str);
  27. #endregion
  28. public CarNoCls()
  29. {
  30. m_bInitSDK = CHCNetSDK.NET_DVR_Init();
  31. if (m_bInitSDK == false)
  32. {
  33. MessageBox.Show("NET_DVR_Init error!");
  34. return;
  35. }
  36. else
  37. {
  38. //保存SDK日志 To save the SDK log
  39. CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
  40. for (int i = 0; i < 64; i++)
  41. {
  42. iChannelNum[i] = -1;
  43. }
  44. //设置报警回调函数
  45. if (m_falarmData_V31 == null)
  46. {
  47. m_falarmData_V31 = new CHCNetSDK.MSGCallBack_V31(MsgCallback_V31);
  48. }
  49. CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(m_falarmData_V31, IntPtr.Zero);
  50. }
  51. }
  52. public bool MsgCallback_V31(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
  53. {
  54. //通过lCommand来判断接收到的报警信息类型,不同的lCommand对应不同的pAlarmInfo内容
  55. AlarmMessageHandle(lCommand, ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
  56. return true; //回调函数需要有返回,表示正常接收到数据
  57. }
  58. public void AlarmMessageHandle(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
  59. {
  60. if (lCommand == CHCNetSDK.COMM_ITS_PLATE_RESULT)
  61. {
  62. ProcessCommAlarm_ITSPlate(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
  63. }
  64. }
  65. private void ProcessCommAlarm_ITSPlate(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
  66. {
  67. CHCNetSDK.NET_ITS_PLATE_RESULT struITSPlateResult = new CHCNetSDK.NET_ITS_PLATE_RESULT();
  68. struITSPlateResult = (CHCNetSDK.NET_ITS_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_ITS_PLATE_RESULT));
  69. uint dwSize = (uint)Marshal.SizeOf(struITSPlateResult);
  70. //上传结果
  71. string stringPlateLicense = System.Text.Encoding.GetEncoding("GBK").GetString(struITSPlateResult.struPlateInfo.sLicense).TrimEnd('\0');
  72. string stringAlarm = "抓拍上传," + "车牌:" + stringPlateLicense + ",车辆序号:" + struITSPlateResult.struVehicleInfo.dwIndex;
  73. CacleCls.voicCarNo = stringPlateLicense.Replace("黄", "").Replace("蓝", "").Replace("绿", "").Replace("白", "");
  74. DebugInfo(stringAlarm);
  75. }
  76. public void Login()
  77. {
  78. if (string.IsNullOrEmpty(AppConfigCache.voiceCarNoIp) || string.IsNullOrEmpty(AppConfigCache.voiceCarNoUid) || string.IsNullOrEmpty(AppConfigCache.voiceCarNoPwd))
  79. {
  80. DebugInfo("请先载入摄像头信息");
  81. }
  82. if (m_lUserID < 0)
  83. {
  84. string DVRIPAddress = AppConfigCache.voiceCarNoIp; //设备IP地址或者域名 Device IP
  85. Int16 DVRPortNumber = (Int16)AppConfigCache.voiceCarNoPort; //port
  86. string DVRUserName = AppConfigCache.voiceCarNoUid;//设备登录用户名 User name to login
  87. string DVRPassword = AppConfigCache.voiceCarNoPwd;//设备登录密码 Password to login
  88. //登录设备 Login the device
  89. m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref DeviceInfo);
  90. if (m_lUserID < 0)
  91. {
  92. iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  93. //登录失败,输出错误号 Failed to login and output the error code
  94. DebugInfo($"NET_DVR_Login_V30 failed, error code= {iLastErr}");
  95. return;
  96. }
  97. else
  98. {
  99. //登录成功
  100. DebugInfo("NET_DVR_Login_V30 succ!");
  101. SetAlarm();
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// 登录后进行布防,布防后才能触发获取车号的功能
  107. /// </summary>
  108. public void SetAlarm()
  109. {
  110. CHCNetSDK.NET_DVR_SETUPALARM_PARAM struAlarmParam = new CHCNetSDK.NET_DVR_SETUPALARM_PARAM();
  111. struAlarmParam.dwSize = (uint)Marshal.SizeOf(struAlarmParam);
  112. struAlarmParam.byLevel = 1; //0- 一级布防,1- 二级布防
  113. struAlarmParam.byAlarmInfoType = 1;//智能交通设备有效,新报警信息类型
  114. struAlarmParam.byFaceAlarmDetection = 1;//1-人脸侦测
  115. m_lAlarmHandle[m_lUserID] = CHCNetSDK.NET_DVR_SetupAlarmChan_V41(m_lUserID, ref struAlarmParam);
  116. if (m_lAlarmHandle[m_lUserID] < 0)
  117. {
  118. iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  119. string strErr = "布防失败,错误号:" + iLastErr; //布防失败,输出错误号
  120. DebugInfo(strErr);
  121. }
  122. else
  123. {
  124. //"布防成功"
  125. DebugInfo("布防成功");
  126. }
  127. }
  128. public void LoginOut()
  129. {
  130. //注销登录 Logout the device
  131. if (m_lRealHandle >= 0)
  132. {
  133. DebugInfo("Please stop live view firstly"); //登出前先停止预览 Stop live view before logout
  134. return;
  135. }
  136. if (m_lAlarmHandle[m_lUserID] >= 0)
  137. {
  138. if (!CHCNetSDK.NET_DVR_CloseAlarmChan_V30(m_lAlarmHandle[m_lUserID]))
  139. {
  140. iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  141. string strErr = "撤防失败,错误号:" + iLastErr;
  142. DebugInfo(strErr);
  143. }
  144. else
  145. {
  146. m_lAlarmHandle[m_lUserID] = -1;
  147. }
  148. }
  149. if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
  150. {
  151. iLastErr = CHCNetSDK.NET_DVR_GetLastError();
  152. DebugInfo($"NET_DVR_Logout failed, error code= {iLastErr}");
  153. return;
  154. }
  155. DebugInfo("NET_DVR_Logout succ!");
  156. m_lUserID = -1;
  157. //程序退出的时候释放资源
  158. CHCNetSDK.NET_DVR_Cleanup();
  159. }
  160. protected void DebugInfo(string strMsg)
  161. {
  162. try
  163. {
  164. string m_szRunPath;
  165. m_szRunPath = System.Environment.CurrentDirectory;
  166. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  167. {
  168. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  169. }
  170. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  171. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  172. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  173. {
  174. Directory.CreateDirectory(strPathFile);
  175. }
  176. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\视频_" + strDate + ".log", true);
  177. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  178. tw.WriteLine(strMsg);
  179. tw.WriteLine("\r\n");
  180. tw.Close();
  181. }
  182. catch { }
  183. }
  184. }
  185. }