一般来说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);
-
-
- char[] param = { '\0' };
- byte[] pMsg = new byte[256];
- string[] baseinfo = new string[9];
-
-
- int intOpenRet = InitComm(1001);
- if (intOpenRet != 1)
- {
-
- return;
- }
-
- int intReadRet = Authenticate();
- if (intReadRet != 1)
- {
-
- CloseComm();
- return;
- }
-
- int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
- if (intReadBaseInfosRet != 1)
- {
-
- 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";
-
-
-
-
-
-
-
-
-
-
- SetImage("photo.bmp", pictureBox1);
- CloseComm();
-
}
3、 声明一个客户端调用的方法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 int pdwEnabledOptions);
-
- [PreserveSig()]
- void SetInterfaceSafeOptions(ref Guid id, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
- }
-
#region UI设计
-
-
-
- private System.ComponentModel.IContainer components = null;
-
-
-
-
-
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
-
#region 组件设计器生成的代码
-
-
-
-
-
- private void InitializeComponent()
- {
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- this.SuspendLayout();
-
-
-
- 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;
-
-
-
- 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;
-
-
#endregion
四、新建一个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) {
-
- }
- }
-
setInterval(Demo, 1000);
截个效果图:
|
请发表评论