身份证阅读器代码C#
两个类:
ReadIDCardService.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
public class ReadIDCardService
{
public IDCard ReadIDCard(int Port)
{
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);
//初始化端口
//int intOpenRet = InitComm(1001);
//if (intOpenRet != 1)
//{
// throw new Exception("");
//}
//设定当前操作的HID接口iDR210
if (HIDSelect(Port))
{
//卡认证
int intReadRet = Authenticate();
if (intReadRet != 1)
{
//CloseComm();
throw new Exception("");
}
//读身份证信息
int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
if (intReadBaseInfosRet != -3)
{
//CloseComm();
throw new Exception("");
}
//HID_BeepLED(true,true, 200);
IDCard CardData = new IDCard();
CardData.CardNo = Code.ToString().Trim();
CardData.Name = Name.ToString().Trim();
CardData.Sex = Gender.ToString().Trim();
CardData.Nationality = Folk.ToString().Trim();
CardData.Birthday = BirthDay.ToString().Trim();
CardData.Address = Address.ToString().Trim();
CardData.Institution = Agency.ToString().Trim();
CardData.Begin_validity = ExpireStart.ToString().Trim();
CardData.End_validity = ExpireEnd.ToString().Trim();
CardData.PhotoDirectory = System.IO.Directory.GetCurrentDirectory() + "//photo.bmp";
//CloseComm();
return CardData;
}
else
{
//CloseComm();
throw new Exception("");
}
}
[DllImport("sdtapi.dll")]
private static extern int InitComm(int Port);//初始化
[DllImport("Sdtapi.dll")]
private static extern int Authenticate();//卡认证
[DllImport("sdtapi.dll")]
private static extern void CloseComm(); //关闭端口
[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 HID_BeepLED(bool BeepON, bool LEDON, int duration); //蜂鸣器
[DllImport("sdtapi.dll")]
private static extern bool HIDSelect(int index); //设定当前操作的HID接口iDR210
[DllImport("sdtapi.dll")]
private static extern int CardOn(); //判断身份证是否在设备上
}
---------------------
IDCard.CS
--------------------------------------------
调用:
[DllImport("sdtapi.dll")]
IDCard.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class IDCard
{
public string Name;//姓名
public string Sex;//性别
public string CardNo; //身份证号
public string Nationality; //民族
public string Birthday; //生日
public string Address;//家庭住址
public string Institution;//发证机关
public string Begin_validity;// 有效期开始
public string End_validity;//有效期结束
public string PhotoDirectory;//照片路径;
}
--------------------------------------------
调用:
private static extern int InitComm(int Port);//初始化
///
int intOpenRet = InitComm(1001);
///
int intOpenRet = InitComm(1001);
if (intOpenRet != 1)
{
//初始化失败
}
try
{
ReadIDCardService ReadIDCardService = new ReadIDCardService();
IDCard CardData = ReadIDCardService.ReadIDCard(1);
string sfz = CardData.CardNo;//这里可以获取读卡后的身份证信息
}
catch (Exception ex)
{
if (ex.Message != "")
}
QQ:83199235