说明:本人用的是精伦身份证阅读器idr210 免驱
摘要:目前市面上读取身份证技术已经很成熟了,大多数文章都介绍了读取身份证的详细信息,但是这些信息里并不包含身份证的物理卡号,所以本文讲解的重点在读取身份证的物理卡号,这也是博主在开发过程中踩到的一个坑
什么是物理卡号?
我们目前所用的所有电子卡,无论是身份证还是IC卡,甚至是银行卡,它都有一个自己的唯一标识,该标识就是物理卡号,精伦读卡器读出的身份证物理卡号是138EC00375381712这样的一个格式,下面就上代码。
需要接入精伦提供的库文件:
Dewlt.dll
JpgDll.dll
SavePhoto.dll
Sdtapi.dll
Sdtapi_bkup.dll
System.Web.Services.dll
WltRS.dll
以上库文件需要的下面留言
新建一个控制台应用程序,导入以上几个库文件,使用本代码即可运行
1、身份证信息Model类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyReadCard
{class IdCardMessageDto{/// <summary>/// 证件号/// </summary>public string Code { get; set; }/// <summary>/// 姓名/// </summary>public string Name { get; set; }/// <summary>/// 性别/// </summary>public string Gender { get; set; }/// <summary>/// 民族/// </summary>public string Folk { get; set; }/// <summary>/// 生日/// </summary>public string BirthDay { get; set; }/// <summary>/// 家庭地址/// </summary>public string Address { get; set; }/// <summary>/// 签发机关/// </summary>public string Agency { get; set; }/// <summary>/// 有效期起始日期/// </summary>public string ExpireStart { get; set; }/// <summary>/// 有效期截止日期/// </summary>public string ExpireEnd { get; set; }/// <summary>/// 照片/// </summary>public string photo { get; set; }/// <summary>/// 物理卡号/// </summary>public string IINSNDN { get; set; }}
}
该类是身份证信息model类,这个不用多说,IINSNDN这个字段就是物理卡号字段
2、读取身份证信息类
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace MyReadCard
{class ReadIdCardHelper{#region Api[DllImport("Sdtapi.dll")]private static extern int InitComm(int iPort);[DllImport("Sdtapi.dll")]private static extern int Authenticate();[DllImport("Sdtapi.dll")]private static extern int ReadBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd);[DllImport("Sdtapi.dll")]private static extern int ReadBaseInfosPhoto(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd, StringBuilder directory);[DllImport("Sdtapi.dll")]private static extern int ReadBaseInfosFPPhoto(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd, StringBuilder directory, StringBuilder pucFPMsg, ref int puiFPMsgLen);[DllImport("Sdtapi.dll")]private static extern int Routon_DecideIDCardType();[DllImport("Sdtapi.dll")]private static extern int Routon_ReadAllForeignBaseInfos(StringBuilder EnName, StringBuilder Gender, StringBuilder Code, StringBuilder Nation, StringBuilder CnName, StringBuilder BirthDay, StringBuilder ExpireStart, StringBuilder ExpireEnd, StringBuilder CardVertion, StringBuilder Agency, StringBuilder CardType, StringBuilder FutureItem);[DllImport("Sdtapi.dll")]private static extern int Routon_ReadAllGATBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder FutureItem1, StringBuilder BirthDay, StringBuilder Address, StringBuilder Code, StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd, StringBuilder PassID, StringBuilder SignCnt, StringBuilder FutureItem2, StringBuilder CardType, StringBuilder FutureItem3);[DllImport("Sdtapi.dll")]private static extern int CloseComm();[DllImport("Sdtapi.dll")]private static extern int ReadBaseMsg(byte[] pMsg, ref int len);[DllImport("Sdtapi.dll")]private static extern int ReadBaseMsgW(byte[] pMsg, ref int len);[DllImport("Sdtapi.dll")]private static extern int Routon_IC_FindCard();[DllImport("Sdtapi.dll")]private static extern int Routon_IC_HL_ReadCardSN(StringBuilder SN);[DllImport("Sdtapi.dll")]private static extern int Routon_RepeatRead(bool isRepeat);[DllImport("Sdtapi.dll")]private static extern int Routon_ReadIINSNDN(StringBuilder IINS);[DllImport("Sdtapi.dll")]private static extern int ReadIINSNDN(StringBuilder IINS);#endregionpublic static string IdCardInfoData = "";static public void readCard(ref IdCardMessageDto idCardMessage){IdCardInfoData = "";Routon_RepeatRead(true);//打开端口int intOpenRet = InitComm(1001);if (intOpenRet != 1){return ;}//卡认证int intReadRet = Authenticate();if (intReadRet != 1){CloseComm();return ;}int cardType = Routon_DecideIDCardType();if (cardType == 100)//身份证{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 CardId = new StringBuilder(16);StringBuilder directory = new StringBuilder(100);StringBuilder pucFPMsg = new StringBuilder(1024);StringBuilder IINS = new StringBuilder(16);int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);if (intReadBaseInfosRet != 1){CloseComm();return ;}int intReadIINS = ReadIINSNDN(IINS);if (intReadIINS != 1){CloseComm();return ;}idCardMessage = new IdCardMessageDto();idCardMessage.Code = Code.ToString().Trim();idCardMessage.Name = Name.ToString().Trim();idCardMessage.Gender = Gender.ToString().Trim();idCardMessage.Folk = Folk.ToString().Trim();idCardMessage.BirthDay = BirthDay.ToString().Trim();idCardMessage.Address = Address.ToString().Trim();idCardMessage.Agency = Agency.ToString().Trim();idCardMessage.ExpireStart = ExpireStart.ToString().Trim();idCardMessage.ExpireEnd = ExpireEnd.ToString().Trim();idCardMessage.IINSNDN = IINS.ToString().Trim();idCardMessage.photo = System.IO.Directory.GetCurrentDirectory() + "\\photo.bmp";IdCardInfoData = JsonConvert.SerializeObject(idCardMessage);}}}
}
[DllImport("Sdtapi.dll")]
private static extern int ReadIINSNDN(StringBuilder IINS); 这里就是调用精伦dll库里读取物理卡号的方法int intReadIINS = ReadIINSNDN(IINS); 将读取到的物理卡号存储到IINS里
if (intReadIINS != 1)
{
CloseComm();
return ;
}读取到物理卡号 intReadIINS 的值就为1
3、Program调用类
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyReadCard
{class Program{public static string IdCardInfoData = "";static void Main(string[] args){IdCardMessageDto idCardMessage = null; ReadIdCardHelper.readCard(ref idCardMessage);IdCardInfoData = ReadIdCardHelper.IdCardInfoData;//var data = JsonConvert.DeserializeObject<IdCardMessageDto>(IdCardInfoData);Console.WriteLine(IdCardInfoData);}}
}