zoukankan      html  css  js  c++  java
  • 第二代居民身份证阅读器GTICR100(国腾)接口类调用方法

         最近做了一个项目,客户需求要用第二代居民身份证阅读器GTICR-100(国腾)来读取用户的信息。我做的这个项目是C/S开发的(C#  windowsForms),用到的一些参考资料以及这个国腾硬件驱动:分享地址:http://pan.baidu.com/share/link?shareid=4042448463&uk=3744480727

         以下我会和大家一起来分享里面的资源,如何使用它,我也是一个新手,这个调用也是查询很多的资料和信息,一开始也是找了很多的资料,但是还是没有用起来,自己看了许多相关的资料,不知不觉也找到了解决。在此也谢谢提供资料的同胞们,今天也让我来分享一下自己成功调用接口类的喜悦吧!给予地址里面资料我一一讲解。

         先从第一步开始吧,(必须有硬件做为前提)把我给你们的地址下载资料或者自己找驱动下载。这里我就只讲我资料里面的东西使用了,其他的也许类似吧。如图 

         然后给大家介绍里面打开的部分内容,我主要讲我用的部分资料。其他呢,对大家也不是没有若以后用到也可以作为参考。好,,继续。。

          先把驱动安装好,看里面安装说明操作。接下我们开始测试一下你的安装的驱动是不是安培正确,里面有两种测试工具。

          1.比较好用的,他是一个应用软件,只需安装就可以快速的测试,硬件是不是好坏或者驱动是不是安装成功。但是不好测试打开的端口号。感觉在开发中不太好好使,个人的感觉啊。。

          

                     2.这个比较不怎么好用,但是自己比较喜欢。先看看里面的东西吧!如图:

    用这个测试就比较麻烦些,还要自己找端口号:USB端口是1001~1004,串口是:1,2,3,4;还有类型,1,2,3,5。我们用的是USB端口是1001,类型这里就多尝试几个类型。我的是类型选择的是5,这个好像跟电脑类型有关吧,有的类型是1.多尝试下就可以找到的。

         

    测试一把看下:我们选中端口是:1001  类型:5  然后打开端口,”测试消息区“里会有提示。 先打开端口按钮:如 “调用InitComm,返回结果:1 ”则打开端口成功。反不成功。 然后读卡:"调用mRead_Content,返回结果:1" 读卡成功。返回0就失败。关闭端口:“调用mCloseComm,返回结果:1”就成功。寻卡and选卡:“调用mAuthenticate,返回结果:1”则成功,反之就失败。这里要注意一下:  有时类型要选几个试下。USB端口是一般是1001,但类型不一定是5,特别是第一次读取的时候,如果读起了,还是要让他多读取几次,要让你的电脑端口激活识辨他的硬件。可以点击循环读卡测试按钮。

             以上的操作没有问题了的话,证明硬件和驱动已经搞写了。接下我们就开始写代码了。在这里我写了一个第二代居民身份证阅读器GTICR-100(国腾)接口类调用方法。给大家作为参考。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    
    namespace PEIS.tjdj
    {
        public class ICCardClasTests
        {
            #region 身份证类国腾(termb.dll)
            /// <summary>
            /// 初始化串口;
            /// </summary>
            /// <param name="iPort"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int InitComm(int iPort);
            /// <summary>
            /// 关闭串口
            /// </summary>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int CloseComm();
            /// <summary>
            /// 卡认证
            /// </summary>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int Authenticate();
            /// <summary>
            /// 读卡操作。
            /// </summary>
            /// <param name="iActive"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int Read_Content(int iActive);
            /// <summary>
            /// 得到姓名信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleName(Byte[] buf, int iLen);
            /// <summary>
            /// 得到性别信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleSex(Byte[] buf, int iLen);
            /// <summary>
            /// 得到民族信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleNation(Byte[] buf, int iLen);
            /// <summary>
            /// 得到发证机关信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetDepartment(Byte[] buf, int iLen);
            /// <summary>
            /// 得到出生日期
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleBirthday(Byte[] buf, int iLen);
            /// <summary>
            /// 得到地址信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleAddress(Byte[] buf, int iLen);
            /// <summary>
            /// 得到有效启始日期
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetStartDate(Byte[] buf, int iLen);
            /// <summary>
            /// 得到有效截止日期
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetEndDate(Byte[] buf, int iLen);
    
            /// <summary>
            /// 得到卡号信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleIDCode(Byte[] buf, int iLen);
    
            /// <summary>
            /// 得到图片信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPhotoBMP(Byte[] buf, int iLen);
    
            #endregion
    
            #region 第二代身份证读值方法
            /// <summary>
            /// 姓名
            /// </summary>
            /// <returns></returns>
            private static string GetName()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetPeopleName(asciiBytes, 100);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
            }
    
            /// <summary>
            /// 性别
            /// </summary>
            /// <returns></returns>
            private static string GetSex()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetPeopleSex(asciiBytes, 100);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
            }
    
            /// <summary>
            /// 民族
            /// </summary>
            /// <returns></returns>
            private static string GetNation()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetPeopleNation(asciiBytes, 100);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
            }
    
            /// <summary>
            /// 出生日期
            /// </summary>
            /// <returns></returns>
            private static string GetBirthday()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
    
                int dwRet = GetPeopleBirthday(asciiBytes, 100);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
    
            }
            /// <summary>
            /// 地址
            /// </summary>
            /// <returns></returns>
            private static string GetAddress()
            {
    
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetPeopleAddress(asciiBytes, 100);
    
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
    
            }
            /// <summary>
            /// 得到发证机关信息
            /// </summary>
            /// <returns></returns>
            private static string GetDepartmentIC()
            {
    
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetDepartment(asciiBytes, 100);
    
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
            }
            /// <summary>
            /// 有效开始日期
            /// </summary>
            /// <returns></returns>
            private static string GetStartDateIC()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetStartDate(asciiBytes, 100);
    
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
    
            }
    
            /// <summary>
            /// 结束日期
            /// </summary>
            /// <returns></returns>
            private static string GetEndDateIC()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetEndDate(asciiBytes, 100);
    
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
    
            }
    
            /// <summary>
            /// 获得卡号信息
            /// </summary>
            /// <returns></returns>
            private static string GetIDCode()
            {
                Byte[] asciiBytes = null;
                asciiBytes = new Byte[100];
                int dwRet = GetPeopleIDCode(asciiBytes, 100);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
                gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
                return new string(asciiChars);
            }
    
    
            #endregion
    
            private int iPort;
            private int port;
            private void bt_readcard_Click(object sender, EventArgs e)
            {
    
                #region 注意,在这里,用户必须有应用程序当前目录的读写权限,(wz.txt:保存的是身份证的信息;zp.bmp:保存的是身份证照片;zp.wlt:保存的是图片二进制信息;)
                FileInfo objFile = new FileInfo("wz.txt");
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    objFile.Delete();
                }
    
                string filename = string.Empty;//复制相片路
                objFile = new FileInfo("zp.bmp");
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    filename = Application.StartupPath + @"\photo\" + Guid.NewGuid().ToString() + "ZP.bmp";
                    File.Copy(Application.StartupPath + @"\ZP.bmp", filename, true);
                    objFile.Delete();
                }
    
                objFile = new FileInfo("zp.wlt");
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    objFile.Delete();
                }
                #endregion
                #region 获取连接端口
                String sText;
                iPort = 0;
                bool bolAuthent = false; //标记变量判断身份证卡是否寻找到
                if (port <= 0)
                {
                    for (iPort = 1001; iPort < 1017; iPort++)
                    {
                        //初始化端口方法返回是不是等于1
                        if (InitComm(iPort) == 1)
                        {
                            //寻卡 129
                            if (Authenticate() == 1)
                            {
                                bolAuthent = true;
                                /*
                                 * Read_Content(1);//读卡方法里面参数的解释:
                                 * 1    读基本信息    形成文字信息文件WZ.TXT、相片文件XP.WLT和ZP.BMP
                                 * 2    只读文字信息    形成文字信息文件WZ.TXT和相片文件XP.WLT
                                 * 3    读最新住址信息    形成最新住址文件NEWADD.TXT
                                 * 5    读芯片管理号    形成二进制文件IINSNDN.bin
                                */
                                Read_Content(1);
                                sText = "读卡器连接在" + iPort + "USB端口上";
                                port = iPort;
                                CloseComm();//关闭端口
                            }
                            else
                            {
                                bolAuthent = false;
                            }
    
                        }
                        CloseComm();
                    }
                    for (iPort = 1; iPort < 17; iPort++)
                    {
                        if (InitComm(iPort) == 1)
                        {
                            //寻卡
                            if (Authenticate() == 1)
                            {
                                bolAuthent = true;
                                Read_Content(1);
                                sText = "读卡器连接在串口" + iPort + "";
                                port = iPort;
                                CloseComm();
                            }
                            else
                            {
                                bolAuthent = false;
                            }
                        }
                        CloseComm();
                    }
                }
                #endregion
    
               
                #region 读二代身份证
                int nRet = InitComm(port);
                if (nRet == 1)
                {
                    //寻卡
                    if (bolAuthent == true)
                    {
    
                        //  GetPhotoBMPIC();
                        txt_xm.Text = GetName();//姓名
                        cmb_xb.Text = GetSex();//性别
                        txt_sfzh.Text = GetIDCode();//身份证号
                        txt_nl.Text = tjdjbiz.GetNl(GetIDCode()).ToString();//年龄
                        dtp_csrq.Value = Convert.ToDateTime(CheckSfzh.GetBrithdayFromIdCard(GetIDCode()));//出生日期
    
                        if (string.IsNullOrEmpty(filename))//判断路径是不是为空,如果为空就重新添加
                        {
                            filename = Application.StartupPath + @"\photo\" + Guid.NewGuid().ToString() + "ZP.bmp";
                            File.Copy(Application.StartupPath + @"\ZP.bmp", filename, true);
                            objFile = new FileInfo("zp.bmp");
                            if (objFile.Exists)
                            {
                                objFile.Attributes = FileAttributes.Normal;
                                objFile.Delete();
                            }
                        }
                        pb_photo.Image = Image.FromFile(filename);
    
                        FileStream fs;
                        using (fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                        {
                            BinaryReader br = new BinaryReader(fs);
                            image = br.ReadBytes((int)fs.Length);
    
                        }
                    }
                    else
                    {
                        sText = "请把身份证放好,再重试!";
                        MessageBox.Show(sText);
                    }
                    port = 0;//把端口初始化为0
                }
                else
                {
                    sText = "对不起,打开端口错误!请把身份证拿起,再重试!";
                    MessageBox.Show(sText);
                }
                CloseComm();
                #endregion
    
            }
        }
    }


    说明一下代码中有一个事件是“读身份证”按钮:  private void bt_readcard_Click(object sender, EventArgs e){...}

    还有里

           /// <summary>
            /// 得到姓名信息
            /// </summary>
            /// <param name="buf"></param>
            /// <param name="iLen"></param>
            /// <returns></returns>
            [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
            public static extern int GetPeopleName(Byte[] buf, int iLen);

          参数跟参考资料里的不一样。如图:

     

         记得要把第三插入引入到项目中” termb.dllWltRS.dll同时拷贝到调用此Dll的应用软件Exe文件所在目录下“   我的项目中没有用到,但是也可以添加。WltRS.dll

        把卡放在读卡器上,点击“读身份证”按钮就把身份证相关信息填充在对应的文本框中了,如”  txt_xm.Text = GetName();//姓名“。

    以下就是我项目读身份证卡时,自动填充的一个效果图片。

    哦。还有一个问题,我里面的图片处理方法不是一个很好的处理方法。我是直调用读卡器生成的图片来处理的,感觉这有些点。。也可以选择用保存图上信息文件来生成图片(XP.wlt),我没有写。

    嗯。差不多了。如果还有不足之处还高手指点下,谢谢大家和我一起分享。

              

           

       

          

            

  • 相关阅读:
    【Linux开发】Linux及Arm-Linux程序开发笔记(零基础入门篇)
    【Linux开发】Linux及Arm-Linux程序开发笔记(零基础入门篇)
    【Linux开发】Linux启动脚本设置
    【Linux开发】Linux启动脚本设置
    【Qt开发】【Linux开发】调试记录:QFontDatabase::loadFromCache Error
    【Qt开发】【Linux开发】调试记录:QFontDatabase::loadFromCache Error
    【Linux开发】【Qt开发】ARM QT移植详细步骤教程
    【Linux开发】【Qt开发】ARM QT移植详细步骤教程
    【linux开发】apt源设置
    【linux开发】apt源设置
  • 原文地址:https://www.cnblogs.com/LikeNeng/p/3265551.html
Copyright © 2011-2022 走看看