最近做了一個項目,客戶需求要用第二代居民身份證閱讀器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.dll和WltRS.dll同時拷貝到調用此Dll的應用軟件Exe文件所在目錄下。“ 我的項目中沒有用到,但是也可以添加。WltRS.dll
把卡放在讀卡器上,點擊“讀身份證”按鈕就把身份證相關信息填充在對應的文本框中了,如” txt_xm.Text = GetName();//姓名“。
以下就是我項目讀身份證卡時,自動填充的一個效果圖片。
哦。還有一個問題,我里面的圖片處理方法不是一個很好的處理方法。我是直調用讀卡器生成的圖片來處理的,感覺這有些點。。也可以選擇用保存圖上信息文件來生成圖片(XP.wlt),我沒有寫。
嗯。差不多了。如果還有不足之處還高手指點下,謝謝大家和我一起分享。