123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- 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";
- /// <summary>
- /// 初始化SDK
- /// </summary>
- /// <returns>SDK版本号</returns>
- 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;
- }
- /// <summary>
- /// 遍历设备
- /// </summary>
- 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;
- }
- /// <summary>
- /// 登录第一个设备,必须在遍历设备之后执行
- /// </summary>
- /// <returns></returns>
- public bool LoginDevice()
- {
- return LoginDevice(0);
- }
- /// <summary>
- /// 登录第设备,必须在遍历设备之后执行
- /// </summary>
- /// <param name="index">要登录的设备在列表中的位置</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 人像采集
- /// </summary>
- /// <param name="filePath">人像临时保存位置</param>
- /// <param name="imageName">图片名称,获取到多个图片会按照顺序从0开始加前缀“0_”</param>
- /// <returns>其他结果内容</returns>
- 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 = "<CaptureFaceDataCond xmlns=\"http://www.isapi.org/ver20/XMLSchema\" version=\"2.0\">" +
- "<captureInfrared>true</captureInfrared>" +
- "<dataType>binary</dataType>" +
- "<readerID>1</readerID>" +
- "</CaptureFaceDataCond>";
- 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;
- }
- /// <summary>
- /// 采集指纹或身份证信息
- /// </summary>
- /// <param name="type">采集类型:1、身份证;2、指纹</param>
- /// <param name="inputStr">输入参数</param>
- /// <returns>采集结果</returns>
- 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;
- }
- /// <summary>
- /// 转换USB_SDK_GetErrorMsg的结果成字符串
- /// </summary>
- /// <returns>字符串信息</returns>
- 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;
- }
- /// <summary>
- /// 判断登录设备的相关信息是否是有效的
- /// </summary>
- /// <returns></returns>
- private Boolean JudgeValidOfLoginInfo()
- {
- if (m_aHidDevInfo == null || m_aHidDevInfo.Length <= 0)
- {
- MessageBox.Show("No device .", "Attention", MessageBoxButtons.OK);
- return false;
- }
- return true;
- }
- }
- }
|