1. 程式人生 > >分享下.NET程式讀取二代身份證(附原始碼

分享下.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("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);

}

UI設計 /// <summary>         /// 必需的設計器變數。         /// </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">&nbsp;</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">&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">性別:</td>

                <td id="td3">&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">民族:</td>

                <td id="td4">&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">出生日期:</td>

                <td id="td5">&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">地址:</td>

                <td id="td6">&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">簽發機關:</td>

                <td id="td7">&nbsp;</td>

                <td>&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">簽發時間:</td>

                <td id="td8">&nbsp;</td>

                <td>&nbsp;</td>

            </tr>

            <tr>

                <td style="text-align: right">有效截止時間:</td>

                <td id="td9">&nbsp;</td>

                <td>&nbsp;</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秒鐘讀取一次

大功告成。。。下載原始碼

截個效果圖:

 

每天進步一點點...