c# winform 身份证信息读取

技术文摘
0 162
admin
xukai 5 2021/1/19 18:44:59
编辑此贴 收藏

描述:-


二代身份证信息读取源码分享


上软件运行界面:

温馨提示:运行程序别忘了装驱动哦!


上完整源码。界面可以自己画!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;

namespace PeopleCardInfoRed
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //lblName.Text = DateTime.Now.Ticks.ToString();
            InitControl();
            InitThread();
            ReadCard();
        }

        int ReadFalseCount = 0;

        /// <summary>
        /// 读卡方法
        /// </summary>
        void ReadCard()
        {
            if (IsScanning)
            {
                return;
            }
            Thread ThreadReadCard = new Thread(new ThreadStart(delegate
            {
                IsScanning = true;
                while (IsScanning)
                {
                    Thread.Sleep(500);
                    int iPort = Project.DevicePort;
                    if (Project.IsOpenedPort)
                    {
                        IsReading = true;
                        IntPtr tmp = Marshal.AllocCoTaskMem(4);
                        int FindIDCard = API.SDT_StartFindIDCard(iPort, tmp, 0);
                        if (FindIDCard == 0x9f)
                        {
                            IntPtr tmp1 = Marshal.AllocCoTaskMem(8);
                            int SelectIDCard = API.SDT_SelectIDCard(iPort, tmp1, 0);
                        }
                        IntPtr ptr_txt = Marshal.AllocCoTaskMem(256);
                        IntPtr ptr_txtlen = Marshal.AllocCoTaskMem(4);
                        IntPtr ptr_pic = Marshal.AllocCoTaskMem(1024);
                        IntPtr ptr_piclen = Marshal.AllocCoTaskMem(4);
                        if (API.SDT_ReadBaseMsg(iPort, ptr_txt, ptr_txtlen, ptr_pic, ptr_piclen, 0) == 0x90)
                        {
                            ReadFalseCount = 0;
                            int txtLen = Marshal.ReadInt32(ptr_txtlen);
                            byte[] txt = new byte[txtLen];
                            for (int i = 0; i < txtLen; i++)
                            {
                                txt[i] = Marshal.ReadByte(ptr_txt, i);
                            }
                            //文本解析
                            string Name = string.Empty;
                            string Sex = string.Empty;
                            string Mz = string.Empty;
                            string Birthday = string.Empty;
                            string Address = string.Empty;
                            string IDNum = string.Empty;
                            string Fzjg = string.Empty;
                            string Yxq = string.Empty;
                            string AppendMsg = string.Empty;
                            string NewAddress = string.Empty;
                            {
                                Name = System.Text.Encoding.Unicode.GetString(txt, 0x00, 30).Trim();//姓名
                                Sex = System.Text.Encoding.Unicode.GetString(txt, 0x1E, 2).Trim();//性别
                                IDNum = System.Text.Encoding.Unicode.GetString(txt, 0x7A, 36).Trim();//身份证号
                                IntPtr ptr_newappendmsg = Marshal.AllocCoTaskMem(70);
                                IntPtr ptr_newappendmsglen = Marshal.AllocCoTaskMem(4);
                                if (API.SDT_ReadNewAppMsg(iPort, ptr_newappendmsg, ptr_newappendmsglen, 0) == 0x90)
                                {
                                    int NewAppendMsgLen = Marshal.ReadInt32(ptr_newappendmsglen);
                                    byte[] NewAppendMsg = new byte[NewAppendMsgLen];
                                    for (int i = 0; i < NewAppendMsgLen; i++)
                                    {
                                        NewAppendMsg[i] = Marshal.ReadByte(ptr_newappendmsg, i);
                                    }
                                    NewAddress = System.Text.Encoding.Unicode.GetString(NewAppendMsg);
                                }
                                if (FindIDCard != 0x9f)
                                {
                                    if (!lblIDCardNum.Text.Equals(string.Empty))
                                    {
                                        Thread.Sleep(500);
                                        continue;
                                    }
                                }
                            }
                            Image img = null;
                            //照片解析
                            {
                                int picLen = Marshal.ReadInt32(ptr_piclen);
                                byte[] pic = new byte[picLen];
                                for (int i = 0; i < picLen; i++)
                                {
                                    pic[i] = Marshal.ReadByte(ptr_pic, i);
                                }
                                string zpPath = Application.StartupPath + "\\" + IDNum;
                                FileStream fs = new FileStream(zpPath + ".wlt", FileMode.Create);
                                fs.Write(pic, 0, pic.Length);
                                fs.Close();
                                int zpResult = API.GetBmp(zpPath + ".wlt", 1);
                                if (zpResult == 1)
                                {
                                    FileStream zpfs = new FileStream(zpPath + ".bmp", FileMode.Open);
                                    byte[] zp = new byte[zpfs.Length];
                                    zpfs.Read(zp, 0, (int)zpfs.Length);
                                    zpfs.Close();
                                    File.Delete(zpPath + ".bmp");
                                    MemoryStream ms = new MemoryStream(zp);
                                    ms.Write(zp, 0, zp.Length);
                                    img = Image.FromStream(ms);
                                }
                            }
                            SetCardInfo(
                                Name,
                                Convert.ToInt32(Sex) == 1 ? "男" : "女",
                                IDNum,
                                img
                            );
                        }
                        else
                        {
                            SetInfo("未读到卡");
                            ReadFalseCount++;
                            if (ReadFalseCount >= 10)
                            {
                                ReadFalseCount = 0;
                                API.SDT_ResetSAM(iPort, 0);
                                API.SDT_ClosePort(iPort);
                                Project.IsOpenedPort = false;
                                IsReading = false;
                                Thread.Sleep(500);
                            }
                        }
                        IsReading = false;
                    }
                    else
                    {
                        SetInfo("未打开端口");
                        //IsScanning = false;
                        Thread.Sleep(500);
                        continue;
                    }
                }
                IsScanning = false;
                // Thread.Sleep(500);
            }));
            ThreadReadCard.IsBackground = true;
            ThreadReadCard.Name = "循环读卡进程";
            ThreadReadCard.Start();
        }

        void SetInfo(
    string sInfo
       )
        {
            richTextBox1.Invoke(new EventHandler(delegate
            {
                richTextBox1.AppendText(sInfo);
            }));
        }

        void SetCardInfo(
            string Name,
            string Sex,
            string IDCardNumber,
            Image zp)
        {
            lblName.Invoke(new EventHandler(delegate
            {
                lblName.Text = Name;
            }));
            lblIDCardNum.Invoke(new EventHandler(delegate
            {
                lblIDCardNum.Text = IDCardNumber;
            }));
            picPhoto.Invoke(new EventHandler(delegate
            {
                picPhoto.Image = zp;
            }));
        }

        void InitThread()
        {
            CheckPort();
        }

        bool IsReading = false;
        bool IsScanning = false;
        void CheckPort()
        {
            Thread CheckPortThread = new Thread(new ThreadStart(delegate
            {
                while (true)
                {
                    if (IsReading)
                    {
                        Thread.Sleep(500);
                        continue;
                    }
                    int OpenResult = 0x01;
                    int i = 0;
                    if (!Project.IsOpenedPort)
                    {
                        for (i = 1001; i <= 1016; i++)
                        {
                            try
                            {
                                API.SDT_ClosePort(i);
                            }
                            catch { }
                            try { OpenResult = API.SDT_OpenPort(i); }
                            catch { }
                            if (OpenResult == 0x90)
                            {
                                if (i != Project.DevicePort)
                                {
                                    Project.DevicePort = i;
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        OpenResult = API.SDT_OpenPort(Project.DevicePort);
                    }
                    Console.WriteLine(Project.DevicePort);
                    string tmpText = string.Empty;
                    if (OpenResult == 0x90)
                    {
                        Project.IsOpenedPort = true;
                        tmpText = "USB端口 " + Project.DevicePort.ToString();
                    }
                    else
                    {
                        Project.IsOpenedPort = false;
                        tmpText = "端口打开失败...";
                    }
                    lblPortState.Invoke(new EventHandler(delegate
                    {
                        lblPortState.Text = tmpText;
                    }));
                    if (Project.IsOpenedPort)
                    {
                        Thread.Sleep(300);
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                    Console.WriteLine("Port Scanning");
                }
            }));
            CheckPortThread.IsBackground = true;
            CheckPortThread.Name = "端口循环检测线程";
            CheckPortThread.Start();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        void InitControl()
        {
            #region 初始化变量
            //Setting.Read();
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            picPhoto.Image = null;
            lblIDCardNum.Text = "";
            lblName.Text = "";
            lblSex.Text = "";
        }
    }
}


个人签名:己所不欲勿施于人

回帖
  • 消灭零回复
本周热议
非法访问资源ip地址公布 1
不用写一句sql的ORM框架,支持多种数据库 1
c# winform 身份证信息读取 0
c#如何集成控制Led屏,实现数据发送自动开机、关机等操作! 0
c#如何获取电脑mac地址,超简单! 0
批量检测指定ip某些端口是否开启,用来做什么自己想吧!!! 0
c# 实现文件下载功能,带下载进度条,可用到软件的自动更新中 0
winform程序如何做到开机自动启动 0
怎么判断两张图片是否完全相同,通过读取图片内容进行对比 0
c# 如何打印条形码 0
热门资讯
看图猜成语:明明很简单,是不是大意了 65 
【每日一习话】把事业放在心上 把责任扛在肩上 64 
第一观察 | 从两会“如期”召开看百年大党的执政能力 63 
美日共同发表"对中国措辞最强硬"声明 外交部撂重话 61 
玖月奇迹王小玮近照曝光!穿紧身裤秀出小鸟腿,皮肤白到发光 60 
成人体验馆被封,客人一天点3个娃娃:男人的孤独,谁能懂? 59 
《奇葩说》第七季总决赛,肖骁用不是自己风格的夺冠后,获得表扬 59 
联播+ | 谋定而动 习近平引领京津冀携手下好“一盘棋” 58 
强力堆料,几乎没有短板、铭瑄iCraft B550M电竞之心主板评测 58 
美媒承认搞错了,但已经来不及了 56 
广东一男子离职索赔270万,公司:你月薪才25万 55 
王霜首次回应结束留洋原因:不因贾秀全 只因这3个字 54 
扒女明星每日穿搭,千元Coach这样搭,千元CHANEL这样买! 53 
亲戚之子前来投奔杜月笙,故意在家中掉钱,捡与不捡两个结局 29 
多名主播带货遭网友围攻 耐克小姐姐被骂哭 阿迪小哥要去找工作 18