| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace CarLocalMeter
- {
- /// <summary>
- /// 摄像头车号获取
- /// </summary>
- public class CarNoCls
- {
- #region
- private bool m_bInitSDK = false;
- private uint iLastErr = 0;
- private Int32 m_lUserID = -1;
- private Int32 m_lRealHandle = -1;
- //int iFileNumber = 0;
- private int[] iChannelNum = new int[96];
- private Int32[] m_lAlarmHandle = new Int32[200];
- private CHCNetSDK.MSGCallBack_V31 m_falarmData_V31 = null;
- private CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
- public delegate void MyDebugInfo(string str);
- private bool bQj = true; //是否是枪机
- #endregion
- public CarNoCls()
- {
- m_bInitSDK = CHCNetSDK.NET_DVR_Init();
- if (m_bInitSDK == false)
- {
- MessageBox.Show("NET_DVR_Init error!");
- return;
- }
- else
- {
- //保存SDK日志 To save the SDK log
- CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
- for (int i = 0; i < 64; i++)
- {
- iChannelNum[i] = -1;
- }
- //设置报警回调函数
- if (m_falarmData_V31 == null)
- {
- m_falarmData_V31 = new CHCNetSDK.MSGCallBack_V31(MsgCallback_V31);
- }
- CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(m_falarmData_V31, IntPtr.Zero);
- }
- }
- public bool MsgCallback_V31(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
- {
- //通过lCommand来判断接收到的报警信息类型,不同的lCommand对应不同的pAlarmInfo内容
- AlarmMessageHandle(lCommand, ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
- return true; //回调函数需要有返回,表示正常接收到数据
- }
- public void AlarmMessageHandle(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
- {
- if (lCommand == CHCNetSDK.COMM_ITS_PLATE_RESULT)
- {
- ProcessCommAlarm_ITSPlate(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
- }
- }
-
- private void ProcessCommAlarm_ITSPlate(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
- {
- CHCNetSDK.NET_ITS_PLATE_RESULT struITSPlateResult = new CHCNetSDK.NET_ITS_PLATE_RESULT();
- struITSPlateResult = (CHCNetSDK.NET_ITS_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_ITS_PLATE_RESULT));
- uint dwSize = (uint)Marshal.SizeOf(struITSPlateResult);
-
- //上传结果
- string stringPlateLicense = System.Text.Encoding.GetEncoding("GBK").GetString(struITSPlateResult.struPlateInfo.sLicense).TrimEnd('\0');
- string stringAlarm = "抓拍上传," + "车牌:" + stringPlateLicense + ",车辆序号:" + struITSPlateResult.struVehicleInfo.dwIndex;
- if (bQj)
- CacleCls.voicCarNo = stringPlateLicense.Replace("黄", "").Replace("蓝", "").Replace("绿", "").Replace("白", ""); //枪机
- else
- CacleCls.voicCarNo2 = stringPlateLicense.Replace("黄", "").Replace("蓝", "").Replace("绿", "").Replace("白", ""); //球机
- DebugInfo(stringAlarm);
- }
- public void Login(string Ip, int port, string uid, string pwd, bool flag = true)
- {
- bQj = flag;
- if (string.IsNullOrEmpty(Ip) || string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd))
- {
- DebugInfo("请先载入摄像头信息");
- }
- if (m_lUserID < 0)
- {
- string DVRIPAddress = Ip; //设备IP地址或者域名 Device IP
- Int16 DVRPortNumber = (Int16)port; //port
- string DVRUserName = uid;//设备登录用户名 User name to login
- string DVRPassword = pwd;//设备登录密码 Password to login
- //登录设备 Login the device
- m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref DeviceInfo);
- if (m_lUserID < 0)
- {
- iLastErr = CHCNetSDK.NET_DVR_GetLastError();
- //登录失败,输出错误号 Failed to login and output the error code
- DebugInfo($"NET_DVR_Login_V30 failed, error code= {iLastErr}");
- return;
- }
- else
- {
- //登录成功
- DebugInfo("NET_DVR_Login_V30 succ!");
- SetAlarm();
- }
- }
- }
- /// <summary>
- /// 登录后进行布防,布防后才能触发获取车号的功能
- /// </summary>
- public void SetAlarm()
- {
- CHCNetSDK.NET_DVR_SETUPALARM_PARAM struAlarmParam = new CHCNetSDK.NET_DVR_SETUPALARM_PARAM();
- struAlarmParam.dwSize = (uint)Marshal.SizeOf(struAlarmParam);
- struAlarmParam.byLevel = 1; //0- 一级布防,1- 二级布防
- struAlarmParam.byAlarmInfoType = 1;//智能交通设备有效,新报警信息类型
- struAlarmParam.byFaceAlarmDetection = 1;//1-人脸侦测
- m_lAlarmHandle[m_lUserID] = CHCNetSDK.NET_DVR_SetupAlarmChan_V41(m_lUserID, ref struAlarmParam);
- if (m_lAlarmHandle[m_lUserID] < 0)
- {
- iLastErr = CHCNetSDK.NET_DVR_GetLastError();
- string strErr = "布防失败,错误号:" + iLastErr; //布防失败,输出错误号
- DebugInfo(strErr);
- }
- else
- {
- //"布防成功"
- DebugInfo("布防成功");
- }
- }
- public void LoginOut()
- {
- //注销登录 Logout the device
- if (m_lRealHandle >= 0)
- {
- DebugInfo("Please stop live view firstly"); //登出前先停止预览 Stop live view before logout
- return;
- }
- if (m_lAlarmHandle[m_lUserID] >= 0)
- {
- if (!CHCNetSDK.NET_DVR_CloseAlarmChan_V30(m_lAlarmHandle[m_lUserID]))
- {
- iLastErr = CHCNetSDK.NET_DVR_GetLastError();
- string strErr = "撤防失败,错误号:" + iLastErr;
- DebugInfo(strErr);
- }
- else
- {
- m_lAlarmHandle[m_lUserID] = -1;
- }
- }
- if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
- {
- iLastErr = CHCNetSDK.NET_DVR_GetLastError();
- DebugInfo($"NET_DVR_Logout failed, error code= {iLastErr}");
- return;
- }
- DebugInfo("NET_DVR_Logout succ!");
- m_lUserID = -1;
- //程序退出的时候释放资源
- CHCNetSDK.NET_DVR_Cleanup();
- }
- CHCNetSDK.NET_DVR_JPEGPARA jpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
- public void CapturePictrue2(string fileUrl)
- {
- jpegPara.wPicQuality = 0;
- jpegPara.wPicSize = 4;
- string str = "";
- Log.GetInstance().WriteLog(LogType.VoiceLog, "完成快速仪表拍照:" + "车号:" + CacleCls.lockCarNo + ";重量:" + CacleCls.lockWgt + ";时间:" + DateTime.Now.ToLongTimeString());
- if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, 1, ref jpegPara, fileUrl))
- {
- iLastErr = CHCNetSDK.NET_DVR_GetLastError();
- str = "NET_DVR_CaptureJPEGPicture failed, error code= " + iLastErr;
- DebugInfo(str);
- return;
- }
- Log.GetInstance().WriteLog(LogType.VoiceLog, "结束快速仪表拍照:" + "车号:" + CacleCls.lockCarNo + ";重量:" + CacleCls.lockWgt + ";时间:" + DateTime.Now.ToLongTimeString());
- }
- protected void DebugInfo(string strMsg)
- {
- try
- {
- string m_szRunPath;
- m_szRunPath = System.Environment.CurrentDirectory;
- if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
- {
- System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
- }
- string strDate = System.DateTime.Now.ToString("yyyyMMdd");
- string strPathFile = m_szRunPath + "\\log\\" + strDate;
- if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
- {
- Directory.CreateDirectory(strPathFile);
- }
- System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\视频_" + strDate + ".log", true);
- tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- tw.WriteLine(strMsg);
- tw.WriteLine("\r\n");
- tw.Close();
- }
- catch { }
- }
- }
- }
|