/****************************** * 说明:身份证阅读器(CVR-100)辅助类 * 创建人:龚宇超 * 创建日期:2018-05-30 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using Lskj.Util; using System.Windows.Forms; using System.Drawing; using System.IO; namespace Lskj.Control.Model { /// /// 身份证阅读器(CVR-100)辅助类 /// public class IDCardUtil { /// /// 是否正在读取 /// public static bool IsConnect = false; public static bool IsTip = false; /// /// 说明:初始化身份证阅读器 /// 创建人:龚宇超 /// 创建日期:2018-05-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 返回提示信息. /// true if XXXX, false otherwise. public static bool InitIDCard() { int iPort; int iRetUSB = 0; try { for (iPort = 1001; iPort <= 1016; iPort++) { iRetUSB = IDCardSDK.CVR_InitComm(iPort); if (iRetUSB == 1) { break; } } } catch (Exception ex) { } return iRetUSB == 1; } public static int CloseIDCard() { IsConnect = false; return IDCardSDK.CVR_CloseComm(); } /// /// 说明:是否可读身份验证 /// 创建人:龚宇超 /// 创建日期:2018-05-31 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if this instance is read; otherwise, false. public static bool IsRead() { if (IDCardSDK.CVR_Authenticate() == 1) return IDCardSDK.CVR_Read_Content(4) == 1; return false; } /// /// 说明:读取指定身份证内容 /// 创建人:龚宇超 /// 创建日期:2018-05-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the card. /// System.Object. public static object ReadCardMessage(IDCardType cardType) { object returnValue = string.Empty; switch (cardType) { case IDCardType.Name: { byte[] name = new byte[30]; int length = 30; IDCardSDK.GetPeopleName(ref name[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(name).Replace("\0", "").Trim(); } break; case IDCardType.Sex: { byte[] sex = new byte[30]; int length = 3; IDCardSDK.GetPeopleSex(ref sex[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(sex).Replace("\0", "").Trim(); } break; case IDCardType.Photo: { string path = Application.StartupPath + "\\zp.bmp"; returnValue = new Bitmap(ImageHelper.ReadImage(path)); break; } case IDCardType.Nation: { byte[] people = new byte[30]; int length = 3; IDCardSDK.GetPeopleNation(ref people[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(people).Replace("\0", "").Trim(); } break; case IDCardType.Birthday: { byte[] birthday = new byte[30]; int length = 16; IDCardSDK.GetPeopleBirthday(ref birthday[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(birthday).Replace("\0", "").Trim(); } break; case IDCardType.CardNo: { byte[] number = new byte[30]; int length = 36; IDCardSDK.GetPeopleIDCode(ref number[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim(); } break; case IDCardType.Address: { byte[] address = new byte[200]; int length = 200; IDCardSDK.GetPeopleAddress(ref address[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(address).Replace("\0", "").Trim(); } break; case IDCardType.Dept: { byte[] signdate = new byte[30]; int length = 30; IDCardSDK.GetDepartment(ref signdate[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(signdate).Replace("\0", "").Trim(); } break; case IDCardType.ValidBeginDate: { byte[] validtermOfStart = new byte[30]; int length = 16; IDCardSDK.GetStartDate(ref validtermOfStart[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(validtermOfStart).Replace("\0", "").Trim(); } break; case IDCardType.ValidEndDate: { byte[] validtermOfEnd = new byte[30]; int length = 16; IDCardSDK.GetEndDate(ref validtermOfEnd[0], ref length); returnValue = Encoding.GetEncoding("GB2312").GetString(validtermOfEnd).Replace("\0", "").Trim(); } break; case IDCardType.SamId: { byte[] samid = new byte[32]; IDCardSDK.CVR_GetSAMID(ref samid[0]); returnValue = Encoding.GetEncoding("GB2312").GetString(samid).Replace("\0", "").Trim(); } break; } return returnValue; } /// /// 说明:读取身份证全部信息 /// 创建人:龚宇超 /// 创建日期:2018-05-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Dictionary<System.Int32, System.Object>. public static Dictionary ReadCardAllMessage() { try { if (!IsConnect) IsConnect = InitIDCard(); //if (!IsConnect && !IsTip) //{ // MessageUtil.Show("设备未连接,请连接设备."); // IsTip = true; //} //else //{ // IsTip = false; //} Dictionary idCards = new Dictionary(); if (IsConnect && IsRead()) { idCards.Add(IDCardType.Name, ReadCardMessage(IDCardType.Name)); idCards.Add(IDCardType.Sex, ReadCardMessage(IDCardType.Sex)); idCards.Add(IDCardType.Address, ReadCardMessage(IDCardType.Address)); idCards.Add(IDCardType.Birthday, ReadCardMessage(IDCardType.Birthday)); idCards.Add(IDCardType.CardNo, ReadCardMessage(IDCardType.CardNo)); idCards.Add(IDCardType.Dept, ReadCardMessage(IDCardType.Dept)); idCards.Add(IDCardType.Nation, ReadCardMessage(IDCardType.Nation)); idCards.Add(IDCardType.Photo, ReadCardMessage(IDCardType.Photo)); idCards.Add(IDCardType.SamId, ReadCardMessage(IDCardType.SamId)); idCards.Add(IDCardType.ValidBeginDate, ReadCardMessage(IDCardType.ValidBeginDate)); idCards.Add(IDCardType.ValidEndDate, ReadCardMessage(IDCardType.ValidEndDate)); } return idCards; } catch (Exception ex) { LogHelper.Instance.WriteLog(ex); } return null; } } /// /// 身份证阅读器SDK /// public class IDCardSDK { [DllImport("termb.dll", EntryPoint = "CVR_InitComm", CharSet = CharSet.Auto, SetLastError = false)] public static extern int CVR_InitComm(int Port);//声明外部的标准动态库, 跟Win32API是一样的 [DllImport("termb.dll", EntryPoint = "CVR_Authenticate", CharSet = CharSet.Auto, SetLastError = false)] public static extern int CVR_Authenticate(); [DllImport("termb.dll", EntryPoint = "CVR_Read_Content", CharSet = CharSet.Auto, SetLastError = false)] public static extern int CVR_Read_Content(int Active); [DllImport("termb.dll", EntryPoint = "CVR_CloseComm", CharSet = CharSet.Auto, SetLastError = false)] public static extern int CVR_CloseComm(); [DllImport("termb.dll", EntryPoint = "GetPeopleName", CharSet = CharSet.Ansi, SetLastError = false)] public static extern unsafe int GetPeopleName(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetPeopleNation", CharSet = CharSet.Ansi, SetLastError = false)] public static extern int GetPeopleNation(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetPeopleBirthday", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetPeopleAddress", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetPeopleIDCode", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetDepartment", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetDepartment(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetStartDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetStartDate(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetEndDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetEndDate(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "GetPeopleSex", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetPeopleSex(ref byte strTmp, ref int strLen); [DllImport("termb.dll", EntryPoint = "CVR_GetSAMID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int CVR_GetSAMID(ref byte strTmp); [DllImport("termb.dll", EntryPoint = "GetManuID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetManuID(ref byte strTmp); } /// /// 读取身份证类型 /// public enum IDCardType { /// /// 姓名 /// Name = 1, /// /// 性别 /// Sex = 2, /// /// 照片 /// Photo = 3, /// /// 民族 /// Nation = 4, /// /// 出生日期 /// Birthday = 5, /// /// 身份证号 /// CardNo = 6, /// /// 地址 /// Address = 7, /// /// 签发机关 /// Dept = 8, /// /// 有效期起 /// ValidBeginDate = 9, /// /// 有效期止 /// ValidEndDate = 10, /// /// 安全模块号 /// SamId = 11 } }