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.

148 lines
5.9 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.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ShenTun.IcCard.Common;
  11. namespace ShenTun.IcCard.Instance
  12. {
  13. /// <summary>
  14. /// 德卡(T10-D)
  15. /// </summary>
  16. public class ReadCardByDCT10D : IReadCard
  17. {
  18. Response IReadCard.GetReadCard()
  19. {
  20. int iPort = 1001;
  21. int baud = 115200;
  22. string sexId = string.Empty;
  23. string nationId = string.Empty;
  24. string birthTmp = string.Empty;
  25. Response res = new Response()
  26. {
  27. code = -101
  28. };
  29. int handle = -1;
  30. IDCardInfo idCard = new IDCardInfo();
  31. try
  32. {
  33. handle = DCTD10.DCSDT_Open(iPort, baud);
  34. if (handle < 0)
  35. {
  36. res.code = -101;
  37. res.message = "打开读卡设备失败,请重新插拨读卡器!";
  38. return res;
  39. }
  40. byte[] management_number = new byte[9];
  41. byte[] serial_number = new byte[17];
  42. var find = DCTD10.DCSDT_SearchIdCard(handle, management_number, serial_number);
  43. if (find < 0)//巡卡失败
  44. {
  45. res.code = -102;
  46. res.message = "巡卡失败,请拿开身份证重新放置!";
  47. return res;
  48. }
  49. var type = DCTD10.DCSDT_GetIdCardType(handle);
  50. if (type < 0)
  51. {
  52. res.code = -103;
  53. res.message = "获取卡类型失败,请拿开身份证重新放置!";
  54. return res;
  55. }
  56. else
  57. {
  58. if (type == 0)
  59. {
  60. var result = DCTD10.DCSDT_IdCardRead(handle, 1);
  61. if (result < 0)
  62. {
  63. res.code = -104;
  64. res.message = "获取ID卡失败,请拿开身份证重新放置!";
  65. return res;
  66. }
  67. byte[] kName = new byte[64];
  68. byte[] bSex = new byte[8];
  69. byte[] bNation = new byte[12];
  70. byte[] bBirth = new byte[36];
  71. byte[] kAddress = new byte[144];
  72. byte[] kIdNumber = new byte[76];
  73. byte[] kPhoto = new byte[4096];
  74. var bname = DCTD10.DCSDT_IdCardContent(handle, 1, kName);
  75. var bsex = DCTD10.DCSDT_IdCardContent(handle, 2, bSex);
  76. var bnation = DCTD10.DCSDT_IdCardContent(handle, 3, bNation);
  77. var bbirth = DCTD10.DCSDT_IdCardContent(handle, 4, bBirth);
  78. var bAddress = DCTD10.DCSDT_IdCardContent(handle, 5, kAddress);
  79. var bIdNumber = DCTD10.DCSDT_IdCardContent(handle, 6, kIdNumber);
  80. var bPhoto = DCTD10.DCSDT_IdCardContent(handle, 10, kPhoto);
  81. idCard.Name = System.Text.Encoding.Default.GetString(kName).TrimEnd('\0');
  82. sexId = System.Text.Encoding.Default.GetString(bSex).TrimEnd('\0');
  83. var sex= BaseData.sList.Where(p => p.Id.Equals(sexId)).FirstOrDefault();
  84. idCard.Sex = sex.Name;
  85. idCard.IDCode = System.Text.Encoding.Default.GetString(kIdNumber).TrimEnd('\0');
  86. birthTmp = System.Text.Encoding.Default.GetString(bBirth).TrimEnd('\0');
  87. idCard.Birthday = string.Format("{0}-{1}-{2}", birthTmp.Substring(0, 4), birthTmp.Substring(4, 2), birthTmp.Substring(6, 2));
  88. idCard.Address = System.Text.Encoding.Default.GetString(kAddress).TrimEnd('\0');
  89. nationId = System.Text.Encoding.Default.GetString(bNation).TrimEnd('\0');
  90. var nation = BaseData.nList.Where(p => p.Id.Equals(nationId)).FirstOrDefault();
  91. idCard.Nation = nation.FullName;
  92. if (bPhoto == 0)
  93. {
  94. string path_current = System.IO.Directory.GetCurrentDirectory();
  95. string img = "zp.bmp";
  96. string file = string.Format("{0}\\{1}", path_current, img);
  97. byte[] byteArray = System.Text.Encoding.Default.GetBytes(file);
  98. var photo = DCTD10.DCSDT_PhotoToBmpFile(kPhoto, byteArray);
  99. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  100. byte[] imageByte = new byte[file.Length];
  101. BinaryReader br = new BinaryReader(fs);
  102. imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
  103. MemoryStream ms = new MemoryStream(imageByte);
  104. Bitmap bm = new Bitmap(ms);
  105. string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp);
  106. fs.Dispose();
  107. ms.Dispose();
  108. idCard.Photo = base64;
  109. }
  110. res.code = 1;
  111. res.data = idCard;
  112. res.message = "Success";
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. throw new Exception(string.Format("读卡器接口操作失败:{0}", ex.Message));
  119. }
  120. finally
  121. {
  122. if(handle>0)
  123. DCTD10.DCSDT_Close(handle);
  124. }
  125. return res;
  126. }
  127. }
  128. }