讀取二代身份證
阿新 • • 發佈:2018-12-30
一般來說winform應用程式解決這個問題起來時很容易的,web應用程式就麻煩一點了。
這裡我說說我的解決思路:
一、你必要有聯機型居民身份證閱讀器一個(帶驅動光碟),這裡我用的是精倫電子公司的iDR200,並有這個閱讀器的開發介面說明。
二、新建一個winform控制元件專案ReadCardControl,新增一個主類ReadCard
三、將這個控制元件封裝成activex元件
在ReadCard類的頭部新增
[Guid("B9BB196C-5008-4156-840F-9FE37BA81502")]
若要實現客戶端訪問伺服器端資料,則必須實現IObjectSafe介面
介面宣告如下:
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafe
{
[PreserveSig]
void GetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref intpdwEnabledOptions);
[PreserveSig()]
void SetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
四、新建一個web應用程式專案
1、介面:
<table style="width:100%;" border="1" width="100%" bordercolordark="#FFFFFF" cellspacing="0" cellpadding="2" bordercolor="#000000">
<tr>
<td style="width:33.3%; text-align: right;">證件號碼:</td>
<td style="width:33.3%" id="td1"> </td>
<td style="width:33.3%; text-align: left;" rowspan="6">
<object id="csharpActiveX" name="csharpActiveX" classid="clsid:B9BB196C-5008-4156-840F-9FE37BA81502"></object></td>
</tr>
<tr>
<td style="text-align: right">姓名:</td>
<td id="td2"> </td>
</tr>
<tr>
<td style="text-align: right">性別:</td>
<td id="td3"> </td>
</tr>
<tr>
<td style="text-align: right">民族:</td>
<td id="td4"> </td>
</tr>
<tr>
<td style="text-align: right">出生日期:</td>
<td id="td5"> </td>
</tr>
<tr>
<td style="text-align: right">地址:</td>
<td id="td6"> </td>
</tr>
<tr>
<td style="text-align: right">簽發機關:</td>
<td id="td7"> </td>
<td> </td>
</tr>
<tr>
<td style="text-align: right">簽發時間:</td>
<td id="td8"> </td>
<td> </td>
</tr>
<tr>
<td style="text-align: right">有效截止時間:</td>
<td id="td9"> </td>
<td> </td>
</tr>
</table>
2、Javascript程式碼:
function Demo() {
try {
var x = document.getElementById("csharpActiveX");
x.OnTimer();//讀取圖片
var v = x.Test();//讀取身份證其他資訊
var arrys = v.split(',');
if (arrys.length > 9) {
document.getElementById("td1").innerHTML = arrys[0];
document.getElementById("td2").innerHTML = arrys[1];
document.getElementById("td3").innerHTML = arrys[2];
document.getElementById("td4").innerHTML = arrys[3];
document.getElementById("td5").innerHTML = arrys[4];
document.getElementById("td6").innerHTML = arrys[5];
document.getElementById("td7").innerHTML = arrys[6];
document.getElementById("td8").innerHTML = arrys[7];
document.getElementById("td9").innerHTML = arrys[8];
}
}
catch (e) {
//alert(e.message)
}
}
setInterval(Demo, 1000);//設定每隔1秒鐘讀取一次
這裡我說說我的解決思路:
一、你必要有聯機型居民身份證閱讀器一個(帶驅動光碟),這裡我用的是精倫電子公司的iDR200,並有這個閱讀器的開發介面說明。
二、新建一個winform控制元件專案ReadCardControl,新增一個主類ReadCard
1、 宣告dll入口 [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 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("kernel32.dll")] private static extern int Beep(int dwFreq, int dwDuration);//用來大吼一聲 2、讀卡方法 private string[] arrys = null;//宣告用來儲存身份證資訊的陣列 public void OnTimer() { 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 len = 0; //string[] temp; char[] param = { '/0' }; byte[] pMsg = new byte[256]; string[] baseinfo = new string[9]; //開啟埠 int intOpenRet = InitComm(1001); if (intOpenRet != 1) { //SetText("閱讀機具未連線", lblMsg); return; } //卡認證 int intReadRet = Authenticate(); if (intReadRet != 1) { //SetText("卡認證失敗", lblMsg); CloseComm(); return; } //ReadBaseInfos(推薦使用) int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd); if (intReadBaseInfosRet != 1) { //SetText("讀卡失敗", lblMsg); CloseComm(); return; } Beep(2047, 200); arrys = new string[10]; arrys[0] = Code.ToString().Trim(); arrys[1] = Name.ToString().Trim(); arrys[2] = Gender.ToString().Trim(); arrys[3] = Folk.ToString().Trim(); arrys[4] = BirthDay.ToString().Trim(); arrys[5] = Address.ToString().Trim(); arrys[6] = Agency.ToString().Trim(); arrys[7] = ExpireStart.ToString().Trim(); arrys[8] = ExpireEnd.ToString().Trim(); arrys[9] = System.IO.Directory.GetCurrentDirectory() + "//photo.bmp"; //SetText("讀卡成功", lblMsg); //SetText("證件號碼:" + Code.ToString(), label1); //SetText("姓名:" + Name.ToString(), label2); //SetText("性別:" + Gender.ToString(), label3); //SetText("民族:" + Folk.ToString(), label4); //SetText("出生日期:" + BirthDay.ToString(), label5); //SetText("地址:" + Address.ToString(), label6); //SetText("簽發機關:" + Agency.ToString(), label7); //SetText("簽發時間:" + ExpireStart.ToString(), label8); //SetText("有效截止時間:" + ExpireEnd.ToString(), label9); SetImage("photo.bmp", pictureBox1); CloseComm(); } 2、 宣告一個客戶端呼叫的方法Test(這裡沒有直接用陣列) public string Test() { string str = ""; foreach (string s in arrys) { str += s + ","; } str = str.TrimEnd(','); return str; }
三、將這個控制元件封裝成activex元件
在ReadCard類的頭部新增
[Guid("B9BB196C-5008-4156-840F-9FE37BA81502")]
若要實現客戶端訪問伺服器端資料,則必須實現IObjectSafe介面
介面宣告如下:
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafe
{
[PreserveSig]
void GetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref intpdwEnabledOptions);
[PreserveSig()]
void SetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
四、新建一個web應用程式專案
1、介面:
<table style="width:100%;" border="1" width="100%" bordercolordark="#FFFFFF" cellspacing="0" cellpadding="2" bordercolor="#000000">
<tr>
<td style="width:33.3%; text-align: right;">證件號碼:</td>
<td style="width:33.3%" id="td1"> </td>
<td style="width:33.3%; text-align: left;" rowspan="6">
<object id="csharpActiveX" name="csharpActiveX" classid="clsid:B9BB196C-5008-4156-840F-9FE37BA81502"></object></td>
</tr>
<tr>
<td style="text-align: right">姓名:</td>
<td id="td2"> </td>
</tr>
<tr>
<td style="text-align: right">性別:</td>
<td id="td3"> </td>
</tr>
<tr>
<td style="text-align: right">民族:</td>
<td id="td4"> </td>
</tr>
<tr>
<td style="text-align: right">出生日期:</td>
<td id="td5"> </td>
</tr>
<tr>
<td style="text-align: right">地址:</td>
<td id="td6"> </td>
</tr>
<tr>
<td style="text-align: right">簽發機關:</td>
<td id="td7"> </td>
<td> </td>
</tr>
<tr>
<td style="text-align: right">簽發時間:</td>
<td id="td8"> </td>
<td> </td>
</tr>
<tr>
<td style="text-align: right">有效截止時間:</td>
<td id="td9"> </td>
<td> </td>
</tr>
</table>
2、Javascript程式碼:
function Demo() {
try {
var x = document.getElementById("csharpActiveX");
x.OnTimer();//讀取圖片
var v = x.Test();//讀取身份證其他資訊
var arrys = v.split(',');
if (arrys.length > 9) {
document.getElementById("td1").innerHTML = arrys[0];
document.getElementById("td2").innerHTML = arrys[1];
document.getElementById("td3").innerHTML = arrys[2];
document.getElementById("td4").innerHTML = arrys[3];
document.getElementById("td5").innerHTML = arrys[4];
document.getElementById("td6").innerHTML = arrys[5];
document.getElementById("td7").innerHTML = arrys[6];
document.getElementById("td8").innerHTML = arrys[7];
document.getElementById("td9").innerHTML = arrys[8];
}
}
catch (e) {
//alert(e.message)
}
}
setInterval(Demo, 1000);//設定每隔1秒鐘讀取一次