HCUSBUtils.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.Windows.Forms;
  8. namespace CollectInformation.HCUSBSDK
  9. {
  10. class HCUSBUtils
  11. {
  12. public const int INITIALIZED_INDEX = 1;
  13. public uint m_VersionNum = 0;
  14. public string szLogPath = Application.StartupPath + "/UsbSDKLog/";
  15. public int g_nEnumDevIndex = INITIALIZED_INDEX;
  16. public CHCUsbSDK.USB_DEVICE_INFO[] m_aHidDevInfo;//这个存储着遍历到的设备,列表索引1开始,所以添加
  17. public static CHCUsbSDK.EnumDeviceCallBack m_OnEnumDeviceCallBack = null;//遍历设备的回调
  18. public int IndexOfInfo = INITIALIZED_INDEX;//信息列表更新信息的索引,初始值为1
  19. public CHCUsbSDK.USB_USER_LOGIN_INFO StruCurUsbLoginInfo = new CHCUsbSDK.USB_USER_LOGIN_INFO();
  20. public int UserID = -1;
  21. public string loginname = "admin";
  22. //public string loginpwd = "free2021!";
  23. public string loginpwd = "Admin123";
  24. /// <summary>
  25. /// 初始化SDK
  26. /// </summary>
  27. /// <returns>SDK版本号</returns>
  28. public string initSDK()
  29. {
  30. string r = null;
  31. bool res = CHCUsbSDK.USB_Init();//USB initialize
  32. IntPtr ptrLogPath = Marshal.StringToHGlobalAnsi(szLogPath);//写日志
  33. CHCUsbSDK.USB_SetLogToFile(3, ptrLogPath, false);//这里用枚举参数不匹配,直接写了3,
  34. Marshal.FreeHGlobal(ptrLogPath);
  35. m_VersionNum = CHCUsbSDK.USB_GetSDKVersion();
  36. r = string.Format("{0}.{1}.{2}.{3}", (0xff000000 & m_VersionNum) >> 24, (0x00ff0000 & m_VersionNum) >> 16, (0x0000ff00 & m_VersionNum) >> 8, (0x000000ff & m_VersionNum));
  37. return r;
  38. }
  39. /// <summary>
  40. /// 遍历设备
  41. /// </summary>
  42. public bool TraverseDevice()
  43. {
  44. bool r = false;
  45. this.m_aHidDevInfo = new CHCUsbSDK.USB_DEVICE_INFO[CHCUsbSDK.MAX_USB_DEV_LEN];
  46. this.g_nEnumDevIndex = 1;
  47. bool bExceedFlag = false;
  48. int dwCount = CHCUsbSDK.USB_GetDeviceCount();
  49. CHCUsbSDK.USB_DEVICE_INFO struDeviceInfo = new CHCUsbSDK.USB_DEVICE_INFO();
  50. struDeviceInfo.Init();
  51. IntPtr pDeviceInfo = Marshal.AllocHGlobal(dwCount * Marshal.SizeOf(struDeviceInfo));
  52. //清空内存空间
  53. for (int i = 0; i < dwCount * Marshal.SizeOf(struDeviceInfo); i++)
  54. {
  55. Marshal.WriteByte(pDeviceInfo, i, 0);
  56. }
  57. IntPtr pTempDeviceInfo = IntPtr.Zero;
  58. if (CHCUsbSDK.USB_EnumDevices(dwCount, pDeviceInfo)) //枚举设备
  59. {
  60. pTempDeviceInfo = pDeviceInfo;
  61. while (--dwCount >= 0)
  62. {
  63. struDeviceInfo = new CHCUsbSDK.USB_DEVICE_INFO();
  64. struDeviceInfo = (CHCUsbSDK.USB_DEVICE_INFO)Marshal.PtrToStructure(pTempDeviceInfo, typeof(CHCUsbSDK.USB_DEVICE_INFO));
  65. if (this.g_nEnumDevIndex == 64)
  66. {
  67. bExceedFlag = true;
  68. break;
  69. }
  70. this.m_aHidDevInfo[this.g_nEnumDevIndex - 1] = struDeviceInfo; //存储到数组中方便登录获取数据
  71. this.g_nEnumDevIndex++;//更新索引
  72. pTempDeviceInfo += Marshal.SizeOf(struDeviceInfo);
  73. }
  74. if (bExceedFlag)
  75. {
  76. MessageBox.Show("The total number of devices could not be over 64numbers", "Error", MessageBoxButtons.OK);
  77. }
  78. else
  79. {
  80. r = true;
  81. }
  82. }
  83. else
  84. {
  85. MessageBox.Show(GetErrorMsg(), "Error", MessageBoxButtons.OK);
  86. }
  87. //释放内存
  88. if (pDeviceInfo != IntPtr.Zero)
  89. {
  90. Marshal.FreeHGlobal(pDeviceInfo);
  91. pDeviceInfo = IntPtr.Zero;
  92. pTempDeviceInfo = IntPtr.Zero;
  93. }
  94. if (pTempDeviceInfo != IntPtr.Zero)
  95. {
  96. Marshal.FreeHGlobal(pTempDeviceInfo);
  97. pTempDeviceInfo = IntPtr.Zero;
  98. }
  99. return r;
  100. }
  101. /// <summary>
  102. /// 登录第一个设备,必须在遍历设备之后执行
  103. /// </summary>
  104. /// <returns></returns>
  105. public bool LoginDevice()
  106. {
  107. return LoginDevice(0);
  108. }
  109. /// <summary>
  110. /// 登录第设备,必须在遍历设备之后执行
  111. /// </summary>
  112. /// <param name="index">要登录的设备在列表中的位置</param>
  113. /// <returns></returns>
  114. public bool LoginDevice(int index)
  115. {
  116. bool r = false;
  117. if (!JudgeValidOfLoginInfo())
  118. {
  119. return r;
  120. }
  121. StruCurUsbLoginInfo.init();
  122. StruCurUsbLoginInfo.dwSize = (int)Marshal.SizeOf(StruCurUsbLoginInfo);
  123. StruCurUsbLoginInfo.dwTimeout = 5000;//登录超时时间是5秒
  124. StruCurUsbLoginInfo.dwVID = this.m_aHidDevInfo[index].dwVID;
  125. StruCurUsbLoginInfo.dwPID = this.m_aHidDevInfo[index].dwPID;
  126. System.Array.Copy(this.m_aHidDevInfo[index].szSerialNumber, StruCurUsbLoginInfo.szSerialNumber, this.m_aHidDevInfo[index].szSerialNumber.Length);
  127. byte[] TempUserArr = System.Text.Encoding.Default.GetBytes(this.loginname);
  128. System.Array.Copy(TempUserArr, StruCurUsbLoginInfo.szUserName, TempUserArr.Length);
  129. byte[] TempszPassword = System.Text.Encoding.Default.GetBytes(this.loginpwd);
  130. System.Array.Copy(TempszPassword, StruCurUsbLoginInfo.szPassword, TempszPassword.Length);
  131. CHCUsbSDK.USB_DEVICE_REG_RES StruDeviceRegRes = new CHCUsbSDK.USB_DEVICE_REG_RES();
  132. StruDeviceRegRes.init();
  133. StruDeviceRegRes.dwSize = (int)Marshal.SizeOf(StruDeviceRegRes);
  134. int UserIDTemp = UserID;
  135. UserID = CHCUsbSDK.USB_Login(ref StruCurUsbLoginInfo, ref StruDeviceRegRes);
  136. if (UserID == CHCUsbSDK.INVALID_USER_ID)
  137. {
  138. GetErrorMsg();
  139. UserID = UserIDTemp;//为了解决重复登录时的问题,但是这次只考虑了只能登录一个设备,两个设备同时能登录的话,ID会覆盖得继续解决ID的问题
  140. }
  141. else
  142. {
  143. string SuccesfulLoginInfo = "SUCCESS USB_Login,The device whose serial number is " + System.Text.Encoding.Default.GetString(StruCurUsbLoginInfo.szSerialNumber) + "login in successfully";
  144. r = true;
  145. }
  146. return r;
  147. }
  148. /// <summary>
  149. /// 人像采集
  150. /// </summary>
  151. /// <param name="filePath">人像临时保存位置</param>
  152. /// <param name="imageName">图片名称,获取到多个图片会按照顺序从0开始加前缀“0_”</param>
  153. /// <returns>其他结果内容</returns>
  154. public string CaptureFace(string filePath, string imageName)
  155. {
  156. string r = null;
  157. CHCUsbSDK.USB_PT_PARAM struPTparam = new CHCUsbSDK.USB_PT_PARAM();
  158. struPTparam.Init();
  159. Int32 nInSize = Marshal.SizeOf(struPTparam);
  160. struPTparam.dwSize = nInSize;
  161. string strRequestUrl = "POST /ISAPI/AccessControl/CaptureFaceData";
  162. int dwRequestUrlLen = strRequestUrl.Length;
  163. struPTparam.pRequestUrl = Marshal.StringToHGlobalAnsi(strRequestUrl);
  164. struPTparam.dwRequestUrlLen = dwRequestUrlLen;
  165. string strInputParam = "<CaptureFaceDataCond xmlns=\"http://www.isapi.org/ver20/XMLSchema\" version=\"2.0\">" +
  166. "<captureInfrared>true</captureInfrared>" +
  167. "<dataType>binary</dataType>" +
  168. "<readerID>1</readerID>" +
  169. "</CaptureFaceDataCond>";
  170. struPTparam.pInBuffer = Marshal.StringToHGlobalAnsi(strInputParam);
  171. struPTparam.dwInSize = strInputParam.Length;
  172. struPTparam.pOutBuffer = Marshal.AllocHGlobal(1024 * 1024);
  173. struPTparam.dwOutSize = 1024 * 1024;
  174. //存放人脸采集返回的图片数据的缓冲区
  175. struPTparam.struData[0].pDataBuffer = Marshal.AllocHGlobal(300 * 1024);
  176. struPTparam.struData[0].dwDataSize = 300 * 1024;
  177. struPTparam.struData[1].pDataBuffer = Marshal.AllocHGlobal(300 * 1024);
  178. struPTparam.struData[1].dwDataSize = 300 * 1024;
  179. struPTparam.dwSendTimeOut = 200000; //发送超时时间
  180. struPTparam.dwRecvTimeOut = 200000; //接收超时时间
  181. if (!CHCUsbSDK.USB_TransConfig(UserID, ref struPTparam))
  182. {
  183. String strErr = "USB_TransConfig failed, error code= " + CHCUsbSDK.USB_GetLastError();
  184. MessageBox.Show(strErr);
  185. }
  186. else
  187. {
  188. int iSize = struPTparam.dwOutSize;
  189. byte[] byOutPut = new byte[iSize];
  190. Marshal.Copy(struPTparam.pOutBuffer, byOutPut, 0, iSize);
  191. r = System.Text.Encoding.UTF8.GetString(byOutPut).TrimEnd('\0');
  192. for (int i = 0; i < struPTparam.byNumOfData; i++)
  193. {
  194. //保存数据
  195. if ((struPTparam.struData[i].dwDataSize != 0) && (struPTparam.struData[i].pDataBuffer != IntPtr.Zero))
  196. {
  197. if (!Directory.Exists(filePath))//路径不存在
  198. {
  199. Directory.CreateDirectory(filePath);//创建新路径
  200. }
  201. string strFileName = filePath + "\\" + i + "_" + imageName;
  202. FileStream fs = new FileStream(strFileName, FileMode.Create);
  203. int iLen = (int)struPTparam.struData[i].dwDataSize;
  204. byte[] by = new byte[iLen];
  205. Marshal.Copy(struPTparam.struData[i].pDataBuffer, by, 0, iLen);
  206. fs.Write(by, 0, iLen);
  207. fs.Close();
  208. }
  209. }
  210. //释放内存
  211. if (struPTparam.pRequestUrl != IntPtr.Zero)
  212. {
  213. Marshal.FreeHGlobal(struPTparam.pRequestUrl);
  214. struPTparam.pRequestUrl = IntPtr.Zero;
  215. }
  216. if (struPTparam.pInBuffer != IntPtr.Zero)
  217. {
  218. Marshal.FreeHGlobal(struPTparam.pInBuffer);
  219. struPTparam.pInBuffer = IntPtr.Zero;
  220. }
  221. if (struPTparam.pOutBuffer != IntPtr.Zero)
  222. {
  223. Marshal.FreeHGlobal(struPTparam.pOutBuffer);
  224. struPTparam.pOutBuffer = IntPtr.Zero;
  225. }
  226. for (int i = 0; i < CHCUsbSDK.MAX_DATA_NUM; i++)
  227. {
  228. if (struPTparam.struData[0].pDataBuffer != IntPtr.Zero)
  229. {
  230. Marshal.FreeHGlobal(struPTparam.struData[0].pDataBuffer);
  231. struPTparam.struData[0].pDataBuffer = IntPtr.Zero;
  232. }
  233. }
  234. }
  235. return r;
  236. }
  237. /// <summary>
  238. /// 采集指纹或身份证信息
  239. /// </summary>
  240. /// <param name="type">采集类型:1、身份证;2、指纹</param>
  241. /// <param name="inputStr">输入参数</param>
  242. /// <returns>采集结果</returns>
  243. public string CollectionNoImage(int type, string inputStr)
  244. {
  245. string r = null;
  246. CHCUsbSDK.USB_PT_PARAM struPTparam = new CHCUsbSDK.USB_PT_PARAM();
  247. struPTparam.Init();
  248. Int32 nInSize = Marshal.SizeOf(struPTparam);
  249. struPTparam.dwSize = nInSize;
  250. string strRequestUrl = "";
  251. if (type == 1)
  252. {
  253. // 身份证采集
  254. strRequestUrl = "POST /ISAPI/AccessControl/CaptureIDInfo?format=json";
  255. }
  256. else
  257. {
  258. // 指纹采集
  259. strRequestUrl = "POST /ISAPI/AccessControl/CaptureFingerPrint";
  260. }
  261. int dwRequestUrlLen = strRequestUrl.Length;
  262. struPTparam.pRequestUrl = Marshal.StringToHGlobalAnsi(strRequestUrl);
  263. struPTparam.dwRequestUrlLen = dwRequestUrlLen;
  264. string strInputParam = inputStr;
  265. struPTparam.pInBuffer = Marshal.StringToHGlobalAnsi(strInputParam);
  266. struPTparam.dwInSize = strInputParam.Length;
  267. struPTparam.pOutBuffer = Marshal.AllocHGlobal(1024 * 1024);
  268. struPTparam.dwOutSize = 1024 * 1024;
  269. struPTparam.dwSendTimeOut = 20000; //发送超时时间
  270. struPTparam.dwRecvTimeOut = 20000; //接收超时时间
  271. if (!CHCUsbSDK.USB_TransConfig(UserID, ref struPTparam))
  272. {
  273. String strErr = "USB_TransConfig failed, error code= " + CHCUsbSDK.USB_GetLastError();
  274. MessageBox.Show(strErr);
  275. }
  276. else
  277. {
  278. int iSize = struPTparam.dwOutSize;
  279. byte[] byOutPut = new byte[iSize];
  280. Marshal.Copy(struPTparam.pOutBuffer, byOutPut, 0, iSize);
  281. r = System.Text.Encoding.UTF8.GetString(byOutPut);
  282. //释放内存
  283. if (struPTparam.pRequestUrl != IntPtr.Zero)
  284. {
  285. Marshal.FreeHGlobal(struPTparam.pRequestUrl);
  286. struPTparam.pRequestUrl = IntPtr.Zero;
  287. }
  288. if (struPTparam.pInBuffer != IntPtr.Zero)
  289. {
  290. Marshal.FreeHGlobal(struPTparam.pInBuffer);
  291. struPTparam.pInBuffer = IntPtr.Zero;
  292. }
  293. if (struPTparam.pOutBuffer != IntPtr.Zero)
  294. {
  295. Marshal.FreeHGlobal(struPTparam.pOutBuffer);
  296. struPTparam.pOutBuffer = IntPtr.Zero;
  297. }
  298. for (int i = 0; i < CHCUsbSDK.MAX_DATA_NUM; i++)
  299. {
  300. if (struPTparam.struData[0].pDataBuffer != IntPtr.Zero)
  301. {
  302. Marshal.FreeHGlobal(struPTparam.struData[0].pDataBuffer);
  303. struPTparam.struData[0].pDataBuffer = IntPtr.Zero;
  304. }
  305. }
  306. }
  307. return r;
  308. }
  309. /// <summary>
  310. /// 转换USB_SDK_GetErrorMsg的结果成字符串
  311. /// </summary>
  312. /// <returns>字符串信息</returns>
  313. public string GetErrorMsg()
  314. {
  315. string r = "";
  316. IntPtr res = IntPtr.Zero;
  317. try
  318. {
  319. uint Error = CHCUsbSDK.USB_GetLastError();
  320. res = CHCUsbSDK.USB_GetErrorMsg(Error);
  321. string sRes = Marshal.PtrToStringAnsi(res);
  322. r = sRes;
  323. res = IntPtr.Zero;
  324. }
  325. catch
  326. {
  327. res = IntPtr.Zero;
  328. r = "Fail To Show USB Error Message in Log Information.";
  329. }
  330. return r;
  331. }
  332. /// <summary>
  333. /// 判断登录设备的相关信息是否是有效的
  334. /// </summary>
  335. /// <returns></returns>
  336. private Boolean JudgeValidOfLoginInfo()
  337. {
  338. if (m_aHidDevInfo == null || m_aHidDevInfo.Length <= 0)
  339. {
  340. MessageBox.Show("No device .", "Attention", MessageBoxButtons.OK);
  341. return false;
  342. }
  343. return true;
  344. }
  345. }
  346. }