CarNoCls.cs 9.3 KB

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