概述
一般來說winform應用程序解決這個問題起來時很容易的,web應用程序就麻煩一點了。你必要有聯(lián)機型居民身份證閱讀器一個。
實現(xiàn)方式
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,調用動態(tài)鏈接庫
public class SynIDCardAPI
{
#region IDCardData
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct IDCardData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] //結構中使用的字串,[]類似限定說明 說明此公共結構傳輸?shù)椒峭泄艽a時封裝定義
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; //發(fā)證機關
[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類函數(shù) **************************/
[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);
/*************************身份證卡類函數(shù) ***************************/
[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();
/***********************設置附加功能函數(shù) ************************/
[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;
該文章在 2024/10/14 12:27:42 編輯過