身份證閱讀器程式碼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
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;//照片路徑;
}
--------------------------------------------
呼叫:
[DllImport("sdtapi.dll")]
private static extern int InitComm(int Port);//初始化
///
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