zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    使用S7netplus存取西门子PLC字符串数据
    学习使用Nginx配置服务器
    Bootstrap4设置footer固定在底部
    ASP.NET Core MVC项目Razor页面实时编译
    在Asp.NET Core MVC项目中通过Libman安装Bootstrap
    在Asp.Net Core Web项目中使用不同的环境
    C#简单使用System.Threading.Timer
    在ASP.Net Core Web API中使用Swagger进行版本控制
    ASP.Net Core Web API解决跨域问题
    LeetCode刷题-- 搜索插入位置
  • 原文地址:https://www.cnblogs.com/king10086/p/13177583.html
Copyright © 2011-2022 走看看