You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
11 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using ShenTun.IcCard.Common;
  12. namespace ShenTun.IcCard.Instance
  13. {
  14. /// <summary>
  15. /// 精伦(IDR-210)
  16. /// </summary>
  17. public class ReadCardByIDR210 : IReadCard
  18. {
  19. Response IReadCard.GetReadCard()
  20. {
  21. int iPort = 1001;
  22. //int baud = 115200;
  23. string sexId = string.Empty;
  24. string nationId = string.Empty;
  25. string birthTmp = string.Empty;
  26. Response res = new Response()
  27. {
  28. code = -101
  29. };
  30. int handle = -1;
  31. IDCardInfo idCard = new IDCardInfo();
  32. try
  33. {
  34. IDR210.Routon_RepeatRead(true);
  35. int intOpenRet = IDR210.InitComm(iPort);
  36. if (intOpenRet != 1)
  37. {
  38. res.code = -101;
  39. res.message = "阅读机具未连接!";
  40. return res;
  41. }
  42. //卡认证
  43. handle = IDR210.Authenticate();
  44. if (handle != 1)
  45. {
  46. res.code = -101;
  47. res.message = "卡认证失败!";
  48. IDR210.CloseComm();
  49. return res;
  50. }
  51. int cardType = IDR210.Routon_DecideIDCardType();
  52. if (cardType == 100)//身份证
  53. {
  54. //三种方式读取基本信息
  55. //ReadBaseInfos(推荐使用)
  56. StringBuilder Name = new StringBuilder(31);
  57. StringBuilder Gender = new StringBuilder(3);
  58. StringBuilder Folk = new StringBuilder(10);
  59. StringBuilder BirthDay = new StringBuilder(9);
  60. StringBuilder Code = new StringBuilder(19);
  61. StringBuilder Address = new StringBuilder(71);
  62. StringBuilder Agency = new StringBuilder(31);
  63. StringBuilder ExpireStart = new StringBuilder(9);
  64. StringBuilder ExpireEnd = new StringBuilder(9);
  65. StringBuilder directory = new StringBuilder(100);
  66. StringBuilder pucFPMsg = new StringBuilder(1024);
  67. directory.Append("C:\\");
  68. int intReadBaseInfosRet = IDR210.ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
  69. if (intReadBaseInfosRet != 1)
  70. {
  71. res.code = -101;
  72. res.message = "读卡失败!";
  73. IDR210.CloseComm();
  74. return res;
  75. }
  76. idCard.IDCode = Code.ToString();
  77. idCard.Name = Name.ToString();
  78. idCard.Sex = Gender.ToString();
  79. if (!Folk.ToString().Contains("族"))
  80. idCard.Nation = Folk.ToString() + "族";
  81. else
  82. idCard.Nation = Folk.ToString();
  83. idCard.Birthday =string.Format("{0}-{1}-{2}", BirthDay.ToString().Substring(0, 4), BirthDay.ToString().Substring(4, 2), BirthDay.ToString().Substring(6, 2));;
  84. idCard.Address = Address.ToString();
  85. string path_current = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  86. //string path_current = Assembly.GetExecutingAssembly().Location;
  87. //string path_current = System.IO.Directory.GetCurrentDirectory();
  88. string img = "IDR210\\photo.bmp";
  89. string file = string.Format("{0}\\{1}", path_current, img);
  90. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  91. byte[] imageByte = new byte[file.Length];
  92. BinaryReader br = new BinaryReader(fs);
  93. imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
  94. MemoryStream ms = new MemoryStream(imageByte);
  95. Bitmap bm = new Bitmap(ms);
  96. string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp);
  97. fs.Dispose();
  98. ms.Dispose();
  99. idCard.Photo = base64;
  100. }
  101. else if (cardType == 101) //外国人居留证
  102. {
  103. StringBuilder Fgn_EnName = new StringBuilder(121); //英文姓名
  104. StringBuilder Fgn_Gender = new StringBuilder(3); //性别
  105. StringBuilder Fgn_Code = new StringBuilder(31); //外国身份证号码
  106. StringBuilder Fgn_Nation = new StringBuilder(7); //国籍
  107. StringBuilder Fgn_CnName = new StringBuilder(31); //中文姓名
  108. StringBuilder Fgn_BirthDay = new StringBuilder(16); //出生日期
  109. StringBuilder Fgn_ExpireStart = new StringBuilder(17); //证件有效期起始日期
  110. StringBuilder Fgn_ExpireEnd = new StringBuilder(17); //证件有效期截至日期
  111. StringBuilder Fgn_CardVersion = new StringBuilder(5); //证件版本号信息
  112. StringBuilder Fgn_Agency = new StringBuilder(9); //申请受理机关代码
  113. StringBuilder Fgn_CardType = new StringBuilder(3); //证件类型标识
  114. StringBuilder Fgn_FutureItem = new StringBuilder(7); //预留项信息 暂无意义
  115. int FornRet = IDR210.Routon_ReadAllForeignBaseInfos(Fgn_EnName, Fgn_Gender, Fgn_Code, Fgn_Nation, Fgn_CnName,
  116. Fgn_BirthDay, Fgn_ExpireStart, Fgn_ExpireEnd, Fgn_CardVersion, Fgn_Agency, Fgn_CardType, Fgn_FutureItem);
  117. if (FornRet != 1)
  118. {
  119. res.code = -101;
  120. res.message = "读卡失败!";
  121. IDR210.CloseComm();
  122. return res;
  123. }
  124. idCard.IDCode = Fgn_Code.ToString();
  125. idCard.Name = Fgn_EnName.ToString() + " " + Fgn_CnName.ToString();
  126. idCard.Sex = Fgn_Gender.ToString();
  127. 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));
  128. //idCard.Nation = Fgn_Nation.ToString();
  129. string path_current = System.IO.Directory.GetCurrentDirectory();
  130. string img = "IDR210\\photo.bmp";
  131. string file = string.Format("{0}\\{1}", path_current, img);
  132. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  133. byte[] imageByte = new byte[file.Length];
  134. BinaryReader br = new BinaryReader(fs);
  135. imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
  136. MemoryStream ms = new MemoryStream(imageByte);
  137. Bitmap bm = new Bitmap(ms);
  138. string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp);
  139. fs.Dispose();
  140. ms.Dispose();
  141. idCard.Photo = base64;
  142. }
  143. else if (cardType == 102) //港澳台居住证
  144. {
  145. StringBuilder GAT_Name = new StringBuilder(31); //姓名
  146. StringBuilder GAT_Sex = new StringBuilder(3); //性别
  147. StringBuilder GAT_FutureItem1 = new StringBuilder(5); //港澳台居住证第一块预留区信息
  148. StringBuilder GAT_BirthDay = new StringBuilder(17); //出生日期
  149. StringBuilder GAT_Address = new StringBuilder(71); //地址
  150. StringBuilder GAT_CardNo = new StringBuilder(37); //证件号码
  151. StringBuilder GAT_Agency = new StringBuilder(31); //签发机关
  152. StringBuilder GAT_ExpireStart = new StringBuilder(17); //证件有效期起始日期
  153. StringBuilder GAT_ExpireEnd = new StringBuilder(17); //证件有效期截至日期
  154. StringBuilder GAT_PassCardNo = new StringBuilder(19); //通行证号码
  155. StringBuilder GAT_PassNu = new StringBuilder(5); //签发次数
  156. StringBuilder GAT_FutureItem2 = new StringBuilder(7); //港澳台居住证第二块预留区信息
  157. StringBuilder GAT_CardType = new StringBuilder(3); //证件类型标识
  158. StringBuilder GAT_FutureItem3 = new StringBuilder(7); //港澳台居住证第三块预留区信息
  159. int GATRet = IDR210.Routon_ReadAllGATBaseInfos(GAT_Name, GAT_Sex, GAT_FutureItem1, GAT_BirthDay, GAT_Address, GAT_CardNo,
  160. GAT_Agency, GAT_ExpireStart, GAT_ExpireEnd, GAT_PassCardNo, GAT_PassNu, GAT_FutureItem2, GAT_CardType, GAT_FutureItem3);
  161. if (GATRet != 1)
  162. {
  163. res.code = -101;
  164. res.message = "读卡失败!";
  165. IDR210.CloseComm();
  166. return res;
  167. }
  168. idCard.IDCode = GAT_CardNo.ToString();
  169. idCard.Name = GAT_Name.ToString();
  170. idCard.Sex = GAT_Sex.ToString();
  171. 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));
  172. idCard.Address = GAT_Address.ToString();
  173. string path_current = System.IO.Directory.GetCurrentDirectory();
  174. string img = "IDR210\\photo.bmp";
  175. string file = string.Format("{0}\\{1}", path_current, img);
  176. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  177. byte[] imageByte = new byte[file.Length];
  178. BinaryReader br = new BinaryReader(fs);
  179. imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
  180. MemoryStream ms = new MemoryStream(imageByte);
  181. Bitmap bm = new Bitmap(ms);
  182. string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp);
  183. fs.Dispose();
  184. ms.Dispose();
  185. idCard.Photo = base64;
  186. }
  187. res.code = 1;
  188. res.data = idCard;
  189. res.message = "Success";
  190. }
  191. catch (Exception ex)
  192. {
  193. throw new Exception(string.Format("读卡器接口操作失败:{0}", ex.Message));
  194. }
  195. finally
  196. {
  197. if (handle > 0)
  198. IDR210.CloseComm();
  199. }
  200. return res;
  201. }
  202. }
  203. }