using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.IO; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ShenTun.IcCard.Common; namespace ShenTun.IcCard.Instance { /// /// 精伦(IDR-210) /// public class ReadCardByIDR210 : IReadCard { Response IReadCard.GetReadCard() { int iPort = 1001; //int baud = 115200; string sexId = string.Empty; string nationId = string.Empty; string birthTmp = string.Empty; Response res = new Response() { code = -101 }; int handle = -1; IDCardInfo idCard = new IDCardInfo(); try { IDR210.Routon_RepeatRead(true); int intOpenRet = IDR210.InitComm(iPort); if (intOpenRet != 1) { res.code = -101; res.message = "阅读机具未连接!"; return res; } //卡认证 handle = IDR210.Authenticate(); if (handle != 1) { res.code = -101; res.message = "卡认证失败!"; IDR210.CloseComm(); return res; } int cardType = IDR210.Routon_DecideIDCardType(); if (cardType == 100)//身份证 { //三种方式读取基本信息 //ReadBaseInfos(推荐使用) StringBuilder Name = new StringBuilder(31); StringBuilder Gender = new StringBuilder(3); StringBuilder Folk = new StringBuilder(10); StringBuilder BirthDay = new StringBuilder(9); StringBuilder Code = new StringBuilder(19); StringBuilder Address = new StringBuilder(71); StringBuilder Agency = new StringBuilder(31); StringBuilder ExpireStart = new StringBuilder(9); StringBuilder ExpireEnd = new StringBuilder(9); StringBuilder directory = new StringBuilder(100); StringBuilder pucFPMsg = new StringBuilder(1024); directory.Append("C:\\"); int intReadBaseInfosRet = IDR210.ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd); if (intReadBaseInfosRet != 1) { res.code = -101; res.message = "读卡失败!"; IDR210.CloseComm(); return res; } idCard.IDCode = Code.ToString(); idCard.Name = Name.ToString(); idCard.Sex = Gender.ToString(); if (!Folk.ToString().Contains("族")) idCard.Nation = Folk.ToString() + "族"; else idCard.Nation = Folk.ToString(); idCard.Birthday =string.Format("{0}-{1}-{2}", BirthDay.ToString().Substring(0, 4), BirthDay.ToString().Substring(4, 2), BirthDay.ToString().Substring(6, 2));; idCard.Address = Address.ToString(); string path_current = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //string path_current = Assembly.GetExecutingAssembly().Location; //string path_current = System.IO.Directory.GetCurrentDirectory(); string img = "IDR210\\photo.bmp"; string file = string.Format("{0}\\{1}", path_current, img); FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] imageByte = new byte[file.Length]; BinaryReader br = new BinaryReader(fs); imageByte = br.ReadBytes(Convert.ToInt32(fs.Length)); MemoryStream ms = new MemoryStream(imageByte); Bitmap bm = new Bitmap(ms); string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp); fs.Dispose(); ms.Dispose(); idCard.Photo = base64; } else if (cardType == 101) //外国人居留证 { StringBuilder Fgn_EnName = new StringBuilder(121); //英文姓名 StringBuilder Fgn_Gender = new StringBuilder(3); //性别 StringBuilder Fgn_Code = new StringBuilder(31); //外国身份证号码 StringBuilder Fgn_Nation = new StringBuilder(7); //国籍 StringBuilder Fgn_CnName = new StringBuilder(31); //中文姓名 StringBuilder Fgn_BirthDay = new StringBuilder(16); //出生日期 StringBuilder Fgn_ExpireStart = new StringBuilder(17); //证件有效期起始日期 StringBuilder Fgn_ExpireEnd = new StringBuilder(17); //证件有效期截至日期 StringBuilder Fgn_CardVersion = new StringBuilder(5); //证件版本号信息 StringBuilder Fgn_Agency = new StringBuilder(9); //申请受理机关代码 StringBuilder Fgn_CardType = new StringBuilder(3); //证件类型标识 StringBuilder Fgn_FutureItem = new StringBuilder(7); //预留项信息 暂无意义 int FornRet = IDR210.Routon_ReadAllForeignBaseInfos(Fgn_EnName, Fgn_Gender, Fgn_Code, Fgn_Nation, Fgn_CnName, Fgn_BirthDay, Fgn_ExpireStart, Fgn_ExpireEnd, Fgn_CardVersion, Fgn_Agency, Fgn_CardType, Fgn_FutureItem); if (FornRet != 1) { res.code = -101; res.message = "读卡失败!"; IDR210.CloseComm(); return res; } idCard.IDCode = Fgn_Code.ToString(); idCard.Name = Fgn_EnName.ToString() + " " + Fgn_CnName.ToString(); idCard.Sex = Fgn_Gender.ToString(); idCard.Birthday = string.Format("{0}-{1}-{2}", Fgn_BirthDay.ToString().Substring(0, 4), Fgn_BirthDay.ToString().Substring(4, 2), Fgn_BirthDay.ToString().Substring(6, 2)); //idCard.Nation = Fgn_Nation.ToString(); string path_current = System.IO.Directory.GetCurrentDirectory(); string img = "IDR210\\photo.bmp"; string file = string.Format("{0}\\{1}", path_current, img); FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] imageByte = new byte[file.Length]; BinaryReader br = new BinaryReader(fs); imageByte = br.ReadBytes(Convert.ToInt32(fs.Length)); MemoryStream ms = new MemoryStream(imageByte); Bitmap bm = new Bitmap(ms); string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp); fs.Dispose(); ms.Dispose(); idCard.Photo = base64; } else if (cardType == 102) //港澳台居住证 { StringBuilder GAT_Name = new StringBuilder(31); //姓名 StringBuilder GAT_Sex = new StringBuilder(3); //性别 StringBuilder GAT_FutureItem1 = new StringBuilder(5); //港澳台居住证第一块预留区信息 StringBuilder GAT_BirthDay = new StringBuilder(17); //出生日期 StringBuilder GAT_Address = new StringBuilder(71); //地址 StringBuilder GAT_CardNo = new StringBuilder(37); //证件号码 StringBuilder GAT_Agency = new StringBuilder(31); //签发机关 StringBuilder GAT_ExpireStart = new StringBuilder(17); //证件有效期起始日期 StringBuilder GAT_ExpireEnd = new StringBuilder(17); //证件有效期截至日期 StringBuilder GAT_PassCardNo = new StringBuilder(19); //通行证号码 StringBuilder GAT_PassNu = new StringBuilder(5); //签发次数 StringBuilder GAT_FutureItem2 = new StringBuilder(7); //港澳台居住证第二块预留区信息 StringBuilder GAT_CardType = new StringBuilder(3); //证件类型标识 StringBuilder GAT_FutureItem3 = new StringBuilder(7); //港澳台居住证第三块预留区信息 int GATRet = IDR210.Routon_ReadAllGATBaseInfos(GAT_Name, GAT_Sex, GAT_FutureItem1, GAT_BirthDay, GAT_Address, GAT_CardNo, GAT_Agency, GAT_ExpireStart, GAT_ExpireEnd, GAT_PassCardNo, GAT_PassNu, GAT_FutureItem2, GAT_CardType, GAT_FutureItem3); if (GATRet != 1) { res.code = -101; res.message = "读卡失败!"; IDR210.CloseComm(); return res; } idCard.IDCode = GAT_CardNo.ToString(); idCard.Name = GAT_Name.ToString(); idCard.Sex = GAT_Sex.ToString(); idCard.Birthday = string.Format("{0}-{1}-{2}", GAT_BirthDay.ToString().Substring(0, 4), GAT_BirthDay.ToString().Substring(4, 2), GAT_BirthDay.ToString().Substring(6, 2)); idCard.Address = GAT_Address.ToString(); string path_current = System.IO.Directory.GetCurrentDirectory(); string img = "IDR210\\photo.bmp"; string file = string.Format("{0}\\{1}", path_current, img); FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] imageByte = new byte[file.Length]; BinaryReader br = new BinaryReader(fs); imageByte = br.ReadBytes(Convert.ToInt32(fs.Length)); MemoryStream ms = new MemoryStream(imageByte); Bitmap bm = new Bitmap(ms); string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp); fs.Dispose(); ms.Dispose(); idCard.Photo = base64; } res.code = 1; res.data = idCard; res.message = "Success"; } catch (Exception ex) { throw new Exception(string.Format("读卡器接口操作失败:{0}", ex.Message)); } finally { if (handle > 0) IDR210.CloseComm(); } return res; } } }