WPF—通过华视电子身份证识别仪读取身份证信息

Posted king10086

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF—通过华视电子身份证识别仪读取身份证信息相关的知识,希望对你有一定的参考价值。

1.源码

 /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int iPort;
        int iRetUSB = 0, iRetCOM = 0;
        static Hashtable hash = new Hashtable();//转JSON用
        public BitmapSource ImgCur { get; set; }
        public MainWindow()
        {
           
            for (iPort = 1001; iPort <= 1016; iPort++)
            {
                iRetUSB = CVRSDK.CVR_InitComm(iPort);
                if (iRetUSB == 1)
                {
                    break;
                }
            }
            if (iRetUSB != 1)
            {
                for (iPort = 1; iPort <= 4; iPort++)
                {
                    iRetCOM = CVRSDK.CVR_InitComm(iPort);
                    if (iRetCOM == 1)
                    {
                        break;
                    }
                }
            }
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(800);
            timer.Tick += timer1_Tick;
            timer.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            int authenticate = CVRSDK.CVR_Authenticate();
            if (authenticate == 1)
            {
                int readContent = CVRSDK.CVR_Read_Content(4);
                if (readContent == 1)
                {

                    FillData();
                }
            }
        }
        private void FillData()
        {
            byte[] name = new byte[30];
            int length = 30;
            CVRSDK.GetPeopleName(ref name[0], ref length);
            byte[] number = new byte[30];
            length = 36;
            CVRSDK.GetPeopleIDCode(ref number[0], ref length);
            byte[] people = new byte[30];
            length = 3;
            CVRSDK.GetPeopleNation(ref people[0], ref length);
            byte[] validtermOfStart = new byte[30];
            length = 16;
            CVRSDK.GetStartDate(ref validtermOfStart[0], ref length);
            byte[] birthday = new byte[30];
            length = 16;
            CVRSDK.GetPeopleBirthday(ref birthday[0], ref length);
            byte[] address = new byte[30];
            length = 70;
            CVRSDK.GetPeopleAddress(ref address[0], ref length);
            byte[] validtermOfEnd = new byte[30];
            length = 16;
            CVRSDK.GetEndDate(ref validtermOfEnd[0], ref length);
            byte[] signdate = new byte[30];
            length = 30;
            CVRSDK.GetDepartment(ref signdate[0], ref length);
            byte[] sex = new byte[30];
            length = 3;
            CVRSDK.GetPeopleSex(ref sex[0], ref length);

            byte[] samid = new byte[32];
            CVRSDK.CVR_GetSAMID(ref samid[0]);

            CurCard card = new CurCard();
            card.Address = System.Text.Encoding.GetEncoding("GB2312").GetString(address).Replace("", "").Trim();
            card.Sex = System.Text.Encoding.GetEncoding("GB2312").GetString(sex).Replace("", "").Trim();
            card.BirthDay = System.Text.Encoding.GetEncoding("GB2312").GetString(birthday).Replace("", "").Trim();
            card.Gov = System.Text.Encoding.GetEncoding("GB2312").GetString(signdate).Replace("", "").Trim();
            card.IdentityId = System.Text.Encoding.GetEncoding("GB2312").GetString(number).Replace("", "").Trim();
            card.Name = System.Text.Encoding.GetEncoding("GB2312").GetString(name).Replace("", "").Trim();
            card.National = System.Text.Encoding.GetEncoding("GB2312").GetString(people).Replace("", "").Trim();
            card.ValidDate = System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfStart).Replace("", "").Trim() + "-" + System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfEnd).Replace("", "").Trim();
            card.ImgAdr = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "zp.bmp");
            ImgCur = GetStreamBitmapSourceFromPath(card.ImgAdr);

            //var A = card.Address + card.Sex + card.BirthDay + card.Gov + card.IdentityId + card.Name + card.National + card.ValidDate + card.ImgAdr;
            hash.Add("Address", card.Address);
            hash.Add("Sex", card.Sex);
            hash.Add("BirthDay", card.BirthDay);
            hash.Add("Gov", card.Gov);
            hash.Add("IdentityId", card.IdentityId);
            hash.Add("Name", card.Name);
            hash.Add("National", card.National);
            hash.Add("ValidDate", card.ValidDate);
            hash.Add("ImgAdr", card.ImgAdr);
            var A = JsonConvert.SerializeObject(hash);
            MessageBox.Show(A);
        }
        public static BitmapSource GetStreamBitmapSourceFromPath(string path)
        {
            BitmapImage bitmapImage;
            using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open)))
            {
                FileInfo fi = new FileInfo(path);
                byte[] bytes = reader.ReadBytes((int)fi.Length);
                reader.Close();
                bitmapImage = new BitmapImage();
                try
                {
                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = new MemoryStream(bytes);
                    bitmapImage.EndInit();
                }
                catch
                {
                    return null;
                }
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            }

            return bitmapImage;
        }
    }
    public class CurCard
    {
        public string Address { get; set; }
        public string Sex { get; set; }
        public string BirthDay { get; set; }
        public string Gov { get; set; }
        public string IdentityId { get; set; }
        public string Name { get; set; }
        public string National { get; set; }
        public string ValidDate { get; set; }
        public string ImgAdr { get; set; }

    }

2.读取的身份证信息转成了json,方便通过http传输,需要NuGet包引用  Newtonsoft.Json

3.SDk的下载链接,由于不支持DLL格式文件上传,下载下来解压放在根目录上即可。https://files-cdn.cnblogs.com/files/king10086/termb.zip

 

以上是关于WPF—通过华视电子身份证识别仪读取身份证信息的主要内容,如果未能解决你的问题,请参考以下文章

华视电子居民身份证阅读机具使用指南

华视身份证读卡器获取uid号vue单文件组件版本

java编程,关于身份证验证问题。求大神指点

Python使用阿里API进行身份证识别

ios 身份证照片识别信息

自动化神器!Python 批量读取身份证信息写入 Excel