using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace CollectInformation.HCUSBSDK { class HCUSBUtils { public const int INITIALIZED_INDEX = 1; public uint m_VersionNum = 0; public string szLogPath = Application.StartupPath + "/UsbSDKLog/"; public int g_nEnumDevIndex = INITIALIZED_INDEX; public CHCUsbSDK.USB_DEVICE_INFO[] m_aHidDevInfo;//这个存储着遍历到的设备,列表索引1开始,所以添加 public static CHCUsbSDK.EnumDeviceCallBack m_OnEnumDeviceCallBack = null;//遍历设备的回调 public int IndexOfInfo = INITIALIZED_INDEX;//信息列表更新信息的索引,初始值为1 public CHCUsbSDK.USB_USER_LOGIN_INFO StruCurUsbLoginInfo = new CHCUsbSDK.USB_USER_LOGIN_INFO(); public int UserID = -1; public string loginname = "admin"; //public string loginpwd = "free2021!"; public string loginpwd = "Admin123"; /// /// 初始化SDK /// /// SDK版本号 public string initSDK() { string r = null; bool res = CHCUsbSDK.USB_Init();//USB initialize IntPtr ptrLogPath = Marshal.StringToHGlobalAnsi(szLogPath);//写日志 CHCUsbSDK.USB_SetLogToFile(3, ptrLogPath, false);//这里用枚举参数不匹配,直接写了3, Marshal.FreeHGlobal(ptrLogPath); m_VersionNum = CHCUsbSDK.USB_GetSDKVersion(); r = string.Format("{0}.{1}.{2}.{3}", (0xff000000 & m_VersionNum) >> 24, (0x00ff0000 & m_VersionNum) >> 16, (0x0000ff00 & m_VersionNum) >> 8, (0x000000ff & m_VersionNum)); return r; } /// /// 遍历设备 /// public bool TraverseDevice() { bool r = false; this.m_aHidDevInfo = new CHCUsbSDK.USB_DEVICE_INFO[CHCUsbSDK.MAX_USB_DEV_LEN]; this.g_nEnumDevIndex = 1; bool bExceedFlag = false; int dwCount = CHCUsbSDK.USB_GetDeviceCount(); CHCUsbSDK.USB_DEVICE_INFO struDeviceInfo = new CHCUsbSDK.USB_DEVICE_INFO(); struDeviceInfo.Init(); IntPtr pDeviceInfo = Marshal.AllocHGlobal(dwCount * Marshal.SizeOf(struDeviceInfo)); //清空内存空间 for (int i = 0; i < dwCount * Marshal.SizeOf(struDeviceInfo); i++) { Marshal.WriteByte(pDeviceInfo, i, 0); } IntPtr pTempDeviceInfo = IntPtr.Zero; if (CHCUsbSDK.USB_EnumDevices(dwCount, pDeviceInfo)) //枚举设备 { pTempDeviceInfo = pDeviceInfo; while (--dwCount >= 0) { struDeviceInfo = new CHCUsbSDK.USB_DEVICE_INFO(); struDeviceInfo = (CHCUsbSDK.USB_DEVICE_INFO)Marshal.PtrToStructure(pTempDeviceInfo, typeof(CHCUsbSDK.USB_DEVICE_INFO)); if (this.g_nEnumDevIndex == 64) { bExceedFlag = true; break; } this.m_aHidDevInfo[this.g_nEnumDevIndex - 1] = struDeviceInfo; //存储到数组中方便登录获取数据 this.g_nEnumDevIndex++;//更新索引 pTempDeviceInfo += Marshal.SizeOf(struDeviceInfo); } if (bExceedFlag) { MessageBox.Show("The total number of devices could not be over 64numbers", "Error", MessageBoxButtons.OK); } else { r = true; } } else { MessageBox.Show(GetErrorMsg(), "Error", MessageBoxButtons.OK); } //释放内存 if (pDeviceInfo != IntPtr.Zero) { Marshal.FreeHGlobal(pDeviceInfo); pDeviceInfo = IntPtr.Zero; pTempDeviceInfo = IntPtr.Zero; } if (pTempDeviceInfo != IntPtr.Zero) { Marshal.FreeHGlobal(pTempDeviceInfo); pTempDeviceInfo = IntPtr.Zero; } return r; } /// /// 登录第一个设备,必须在遍历设备之后执行 /// /// public bool LoginDevice() { return LoginDevice(0); } /// /// 登录第设备,必须在遍历设备之后执行 /// /// 要登录的设备在列表中的位置 /// public bool LoginDevice(int index) { bool r = false; if (!JudgeValidOfLoginInfo()) { return r; } StruCurUsbLoginInfo.init(); StruCurUsbLoginInfo.dwSize = (int)Marshal.SizeOf(StruCurUsbLoginInfo); StruCurUsbLoginInfo.dwTimeout = 5000;//登录超时时间是5秒 StruCurUsbLoginInfo.dwVID = this.m_aHidDevInfo[index].dwVID; StruCurUsbLoginInfo.dwPID = this.m_aHidDevInfo[index].dwPID; System.Array.Copy(this.m_aHidDevInfo[index].szSerialNumber, StruCurUsbLoginInfo.szSerialNumber, this.m_aHidDevInfo[index].szSerialNumber.Length); byte[] TempUserArr = System.Text.Encoding.Default.GetBytes(this.loginname); System.Array.Copy(TempUserArr, StruCurUsbLoginInfo.szUserName, TempUserArr.Length); byte[] TempszPassword = System.Text.Encoding.Default.GetBytes(this.loginpwd); System.Array.Copy(TempszPassword, StruCurUsbLoginInfo.szPassword, TempszPassword.Length); CHCUsbSDK.USB_DEVICE_REG_RES StruDeviceRegRes = new CHCUsbSDK.USB_DEVICE_REG_RES(); StruDeviceRegRes.init(); StruDeviceRegRes.dwSize = (int)Marshal.SizeOf(StruDeviceRegRes); int UserIDTemp = UserID; UserID = CHCUsbSDK.USB_Login(ref StruCurUsbLoginInfo, ref StruDeviceRegRes); if (UserID == CHCUsbSDK.INVALID_USER_ID) { GetErrorMsg(); UserID = UserIDTemp;//为了解决重复登录时的问题,但是这次只考虑了只能登录一个设备,两个设备同时能登录的话,ID会覆盖得继续解决ID的问题 } else { string SuccesfulLoginInfo = "SUCCESS USB_Login,The device whose serial number is " + System.Text.Encoding.Default.GetString(StruCurUsbLoginInfo.szSerialNumber) + "login in successfully"; r = true; } return r; } /// /// 人像采集 /// /// 人像临时保存位置 /// 图片名称,获取到多个图片会按照顺序从0开始加前缀“0_” /// 其他结果内容 public string CaptureFace(string filePath, string imageName) { string r = null; CHCUsbSDK.USB_PT_PARAM struPTparam = new CHCUsbSDK.USB_PT_PARAM(); struPTparam.Init(); Int32 nInSize = Marshal.SizeOf(struPTparam); struPTparam.dwSize = nInSize; string strRequestUrl = "POST /ISAPI/AccessControl/CaptureFaceData"; int dwRequestUrlLen = strRequestUrl.Length; struPTparam.pRequestUrl = Marshal.StringToHGlobalAnsi(strRequestUrl); struPTparam.dwRequestUrlLen = dwRequestUrlLen; string strInputParam = "" + "true" + "binary" + "1" + ""; struPTparam.pInBuffer = Marshal.StringToHGlobalAnsi(strInputParam); struPTparam.dwInSize = strInputParam.Length; struPTparam.pOutBuffer = Marshal.AllocHGlobal(1024 * 1024); struPTparam.dwOutSize = 1024 * 1024; //存放人脸采集返回的图片数据的缓冲区 struPTparam.struData[0].pDataBuffer = Marshal.AllocHGlobal(300 * 1024); struPTparam.struData[0].dwDataSize = 300 * 1024; struPTparam.struData[1].pDataBuffer = Marshal.AllocHGlobal(300 * 1024); struPTparam.struData[1].dwDataSize = 300 * 1024; struPTparam.dwSendTimeOut = 200000; //发送超时时间 struPTparam.dwRecvTimeOut = 200000; //接收超时时间 if (!CHCUsbSDK.USB_TransConfig(UserID, ref struPTparam)) { String strErr = "USB_TransConfig failed, error code= " + CHCUsbSDK.USB_GetLastError(); MessageBox.Show(strErr); } else { int iSize = struPTparam.dwOutSize; byte[] byOutPut = new byte[iSize]; Marshal.Copy(struPTparam.pOutBuffer, byOutPut, 0, iSize); r = System.Text.Encoding.UTF8.GetString(byOutPut).TrimEnd('\0'); for (int i = 0; i < struPTparam.byNumOfData; i++) { //保存数据 if ((struPTparam.struData[i].dwDataSize != 0) && (struPTparam.struData[i].pDataBuffer != IntPtr.Zero)) { if (!Directory.Exists(filePath))//路径不存在 { Directory.CreateDirectory(filePath);//创建新路径 } string strFileName = filePath + "\\" + i + "_" + imageName; FileStream fs = new FileStream(strFileName, FileMode.Create); int iLen = (int)struPTparam.struData[i].dwDataSize; byte[] by = new byte[iLen]; Marshal.Copy(struPTparam.struData[i].pDataBuffer, by, 0, iLen); fs.Write(by, 0, iLen); fs.Close(); } } //释放内存 if (struPTparam.pRequestUrl != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pRequestUrl); struPTparam.pRequestUrl = IntPtr.Zero; } if (struPTparam.pInBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pInBuffer); struPTparam.pInBuffer = IntPtr.Zero; } if (struPTparam.pOutBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pOutBuffer); struPTparam.pOutBuffer = IntPtr.Zero; } for (int i = 0; i < CHCUsbSDK.MAX_DATA_NUM; i++) { if (struPTparam.struData[0].pDataBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.struData[0].pDataBuffer); struPTparam.struData[0].pDataBuffer = IntPtr.Zero; } } } return r; } /// /// 采集指纹或身份证信息 /// /// 采集类型:1、身份证;2、指纹 /// 输入参数 /// 采集结果 public string CollectionNoImage(int type, string inputStr) { string r = null; CHCUsbSDK.USB_PT_PARAM struPTparam = new CHCUsbSDK.USB_PT_PARAM(); struPTparam.Init(); Int32 nInSize = Marshal.SizeOf(struPTparam); struPTparam.dwSize = nInSize; string strRequestUrl = ""; if (type == 1) { // 身份证采集 strRequestUrl = "POST /ISAPI/AccessControl/CaptureIDInfo?format=json"; } else { // 指纹采集 strRequestUrl = "POST /ISAPI/AccessControl/CaptureFingerPrint"; } int dwRequestUrlLen = strRequestUrl.Length; struPTparam.pRequestUrl = Marshal.StringToHGlobalAnsi(strRequestUrl); struPTparam.dwRequestUrlLen = dwRequestUrlLen; string strInputParam = inputStr; struPTparam.pInBuffer = Marshal.StringToHGlobalAnsi(strInputParam); struPTparam.dwInSize = strInputParam.Length; struPTparam.pOutBuffer = Marshal.AllocHGlobal(1024 * 1024); struPTparam.dwOutSize = 1024 * 1024; struPTparam.dwSendTimeOut = 20000; //发送超时时间 struPTparam.dwRecvTimeOut = 20000; //接收超时时间 if (!CHCUsbSDK.USB_TransConfig(UserID, ref struPTparam)) { String strErr = "USB_TransConfig failed, error code= " + CHCUsbSDK.USB_GetLastError(); MessageBox.Show(strErr); } else { int iSize = struPTparam.dwOutSize; byte[] byOutPut = new byte[iSize]; Marshal.Copy(struPTparam.pOutBuffer, byOutPut, 0, iSize); r = System.Text.Encoding.UTF8.GetString(byOutPut); //释放内存 if (struPTparam.pRequestUrl != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pRequestUrl); struPTparam.pRequestUrl = IntPtr.Zero; } if (struPTparam.pInBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pInBuffer); struPTparam.pInBuffer = IntPtr.Zero; } if (struPTparam.pOutBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.pOutBuffer); struPTparam.pOutBuffer = IntPtr.Zero; } for (int i = 0; i < CHCUsbSDK.MAX_DATA_NUM; i++) { if (struPTparam.struData[0].pDataBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(struPTparam.struData[0].pDataBuffer); struPTparam.struData[0].pDataBuffer = IntPtr.Zero; } } } return r; } /// /// 转换USB_SDK_GetErrorMsg的结果成字符串 /// /// 字符串信息 public string GetErrorMsg() { string r = ""; IntPtr res = IntPtr.Zero; try { uint Error = CHCUsbSDK.USB_GetLastError(); res = CHCUsbSDK.USB_GetErrorMsg(Error); string sRes = Marshal.PtrToStringAnsi(res); r = sRes; res = IntPtr.Zero; } catch { res = IntPtr.Zero; r = "Fail To Show USB Error Message in Log Information."; } return r; } /// /// 判断登录设备的相关信息是否是有效的 /// /// private Boolean JudgeValidOfLoginInfo() { if (m_aHidDevInfo == null || m_aHidDevInfo.Length <= 0) { MessageBox.Show("No device .", "Attention", MessageBoxButtons.OK); return false; } return true; } } }