ASP.NET程序读取二代身份证(附源码)
一般来说winform应用程序解决这个问题起来时很容易的,web应用程序就麻烦一点了。
这里我说说我的解决思路:
一、你必要有联机型居民身份证阅读器一个(带驱动光盘),这里我用的是精伦电子公司的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("B9BB
若要实现客户端访问服务器端数据,则必须实现IObjectSafe接口
接口声明如下:
[ComImport, GuidAttribute("CB5BDC81
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafe
{
[PreserveSig]
void GetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
void SetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 130);
this.pictureBox1.TabIndex = 12;
this.pictureBox1.TabStop = false;
//
// ReadCard
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.pictureBox1);
this.Name = "ReadCard";
this.Size = new System.Drawing.Size(107, 135);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
四、新建一个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:B9BB
</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>
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秒钟读取一次
大功告成。。。下载源码
截个效果图:
每天进步一点点...