用C# 做一个读取身份证阅读程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C# 做一个读取身份证阅读程序相关的知识,希望对你有一定的参考价值。
我已经有sdtapi.dll,WltRS.dll两个最基本的类库,请高手提供详细代码,谢谢!
参考技术A 文件不全无法完成,而且身份证阅读有多个厂家。每个厂家的SDK包都不一样。下面只是个简单的例子,作参考
//首先,声明通用接口
[DllImport("sdtapi.dll")]
public static extern int SDT_OpenPort(int iPortID);
[DllImport("sdtapi.dll")]
public static extern int SDT_ClosePort(int iPortID);
[DllImport("sdtapi.dll")]
public static extern int SDT_PowerManagerBegin(int iPortID, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_AddSAMUser(int iPortID, string pcUserName, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_SAMLogin(int iPortID, string pcUserName, string pcPasswd, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_SAMLogout(int iPortID, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_UserManagerOK(int iPortID, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ChangeOwnPwd(int iPortID, string pcOldPasswd, string pcNewPasswd, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ChangeOtherPwd(int iPortID, string pcUserName, string pcNewPasswd, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_DeleteSAMUser(int iPortID, string pcUserName, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_StartFindIDCard(int iPortID, ref int pucIIN, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_SelectIDCard(int iPortID, ref int pucSN, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ReadBaseMsg(int iPortID, string pucCHMsg, ref int puiCHMsgLen, string pucPHMsg, ref int puiPHMsgLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ReadBaseMsgToFile(int iPortID, string fileName1, ref int puiCHMsgLen, string fileName2, ref int puiPHMsgLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_WriteAppMsg(int iPortID, ref byte pucSendData, int uiSendLen, ref byte pucRecvData, ref int puiRecvLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_WriteAppMsgOK(int iPortID, ref byte pucData, int uiLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_CancelWriteAppMsg(int iPortID, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ReadNewAppMsg(int iPortID, ref byte pucAppMsg, ref int puiAppMsgLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ReadAllAppMsg(int iPortID, ref byte pucAppMsg, ref int puiAppMsgLen, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_UsableAppMsg(int iPortID, ref byte ucByte, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_GetUnlockMsg(int iPortID, ref byte strMsg, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_GetSAMID(int iPortID, ref byte StrSAMID, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_SetMaxRFByte(int iPortID, byte ucByte, int iIfOpen);
[DllImport("sdtapi.dll")]
public static extern int SDT_ResetSAM(int iPortID, int iIfOpen);
[DllImport("WltRS.dll")]
public static extern int GetBmp(string file_name, int intf);
public delegate void De_ReadICCardComplete(clsEDZ objEDZ);
public event De_ReadICCardComplete ReadICCardComplete;
private clsEDZ objEDZ = new clsEDZ();
private int EdziIfOpen = 1; //自动开关串口
int EdziPortID;
public clsICCard()
public bool ReadICCard()
bool bUsbPort = false;
int intOpenPortRtn = 0;
int rtnTemp = 0;
int pucIIN = 0;
int pucSN = 0;
int puiCHMsgLen = 0;
int puiPHMsgLen = 0;
objEDZ = new clsEDZ();
//检测usb口的机具连接,必须先检测usb
for (int iPort = 1001; iPort <= 1016; iPort++)
intOpenPortRtn = SDT_OpenPort(iPort);
if (intOpenPortRtn == 144)
EdziPortID = iPort;
bUsbPort = true;
break;
//检测串口的机具连接
if (!bUsbPort)
for (int iPort = 1; iPort <= 2; iPort++)
intOpenPortRtn = SDT_OpenPort(iPort);
if (intOpenPortRtn == 144)
EdziPortID = iPort;
bUsbPort = false;
break;
if (intOpenPortRtn != 144)
MessageBox.Show("端口打开失败,请检测相应的端口或者重新连接读卡器!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
本回答被提问者和网友采纳
C# 调用动态链接库读取二代身份证信息
概述
一般来说winform应用程序解决这个问题起来时很容易的,web应用程序就麻烦一点了。你必要有联机型居民身份证阅读器一个。
实现方式
1、新建一个winform控件项目ReadCardControl,添加一个主类ReadIDCard
private bool ReadIDCard()
{
try
{
BLL.API.SynIDCardAPI.IDCardData CardMsg = new BLL.API.SynIDCardAPI.IDCardData();
int nRet, nPort;
byte[] pucIIN = new byte[4];
byte[] pucSN = new byte[8];
nPort = Convert.ToInt32(1001);
if (BLL.API.SynIDCardAPI.Syn_OpenPort(nPort) == 0)
{
if (BLL.API.SynIDCardAPI.Syn_SetMaxRFByte(nPort, 80, 0) == 0)
{
nRet = BLL.API.SynIDCardAPI.Syn_StartFindIDCard(nPort, ref pucIIN[0], 0);
nRet = BLL.API.SynIDCardAPI.Syn_SelectIDCard(nPort, ref pucSN[0], 0);
nRet = BLL.API.SynIDCardAPI.Syn_ReadMsg(nPort, 0, ref CardMsg);
if (nRet == 0)
{
txtAddress.Text = CardMsg.Address;
txtBirthday.Text = CardMsg.Born;
txtCerticate.Text = CardMsg.IDCardNo;
txtIssuingAuthoriy.Text = CardMsg.GrantDept;
txtNames.Text = CardMsg.Name;
txtNation.Text = Xw.Common.Helper.NationHelper.GetNationByNO(CardMsg.Nation);
txtSex.Text = CardMsg.Sex == "1" ? "男" : "女";
picMan.Image = Image.FromFile(CardMsg.PhotoFileName);
//lblMsg.Text = "读卡成功";
return true;
}
else
{
lblMsg.Text = "请放卡: 读取身份证信息错误";
return false;
}
}
}
else
{
lblMsg.Text = "打开端口失败";
return false;
}
return true;
}
catch (Exception ex)
{
Xw.Common.Helper.LogHelper.WriteErrorToLog(ex.Message);
return false;
}
}
2、声明DllImport,调用动态链接库
public class SynIDCardAPI
{
#region IDCardData
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct IDCardData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] //结构中使用的字串,[]类似限定说明 说明此公共结构传输到非托管代码时封装定义
public string Name; //姓名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string Sex; //性别
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Nation; //名族
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string Born; //出生日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)]
public string Address; //住址
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)]
public string IDCardNo; //身份证号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string GrantDept; //发证机关
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string UserLifeBegin; // 有效开始日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string UserLifeEnd; // 有效截止日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)]
public string reserved; // 保留
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string PhotoFileName; // 照片路径
}
#endregion
#region SynIDCardAPI
/************************端口类API *************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi)]
public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi)]
public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi)]
public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi)]
public static extern int Syn_OpenPort(int iPort);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi)]
public static extern int Syn_ClosePort(int iPort);
/**************************SAM类函数 **************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi)]
public static extern int Syn_ResetSAM(int iPort, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen);
/*************************身份证卡类函数 ***************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi)]
public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi)]
public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDN", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadIINSNDN(int iPort, ref byte pucIINSNDN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDNToASCII", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadIINSNDNToASCII(int iPort, ref byte pucIINSNDN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref uint puiAppMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi)]
public static extern int Syn_GetBmp(int iPort, ref byte Wlt_File);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi)]
public static extern int Syn_FindReader();
/***********************设置附加功能函数 ************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoName(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetSexType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetNationType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetBornType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetUserLifeBType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetUserLifeEType(int iType, int iOption);
#endregion
}
3、读取显示相关的信息
txtAddress.Text = CardMsg.Address;
txtBirthday.Text = CardMsg.Born;
txtCerticate.Text = CardMsg.IDCardNo;
txtIssuingAuthoriy.Text = CardMsg.GrantDept;
txtNames.Text = CardMsg.Name;
txtNation.Text = Xw.Common.Helper.NationHelper.GetNationByNO(CardMsg.Nation);
txtSex.Text = CardMsg.Sex == "1" ? "男" : "女";
picMan.Image = Image.FromFile(CardMsg.PhotoFileName);
//lblMsg.Text = "读卡成功";
return true;
以上是关于用C# 做一个读取身份证阅读程序的主要内容,如果未能解决你的问题,请参考以下文章