《C#读取二代身份证信息源码.docx》由会员分享,可在线阅读,更多相关《C#读取二代身份证信息源码.docx(40页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C#读取二代身份证信息源码using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System .Windows. Forms;using System.Runtime. InteropServices;using System. Ref lection ;using System.IO;namespace ICCard(public class cisICCard(首先,声明通用接口Dlllmport(,sdtapi.dll,)public static exte
2、rn int SDT_OpenPort(int iPortID);DlllmportCsdtapi.dir)public static extern int SDT_ClosePort(int iPortlD);DlllmportCsdtapi.dir)public static extern int SDT_PowerManagerBegin(int iPortID, int ilfOpen);DlllmportCsdtapi.dir)public static extern int SDT_AddSAMUser(int iPortID, string pclIserName, int il
3、fOpen);Dlllmport(,sdtapi.dll,)public static extern int SDT_SAMLogout(int iPortID, int ilfOpen);DlllmportCsdt 叩 i.dll)public static extern int SDT_UserManagerOK(int iPortID, int ilfOpen);Dlllmport(,sdtapi.dll,)public static extern int SDT_ChangeOwnPwd(int iPortID, string pcOldPasswd, string pcNewPass
4、wd, int ilfOpen);Dlllmport(sdtapi.dir)public static extern int SDT_ChangeOtherPwd(int iPortID, string pcUserName, string pcNewPasswd, int ilfOpen);Dlllmport(*sdtapi.dir)public static extern int SDT_DeleteSAMUser(int iPortID, string pclIserName, int ilfOpen);Dlllmport(,sdtapi.dll,)public static exter
5、n int SDT_StartFindIDCard(int iPortID, ref int pucllN, int ilfOpen);DlllmportCsdt 叩 i.dl public static extern int SDT_SelectlDCard(int iPortID, ref int pucSN, int ilfOpen);Dlllmport(,sdtapi.dll,)public static extern int SDT_ReadBaseMsg(int iPortID, string pucCHMsg, ref int puiCHMsgLen, string pucPHM
6、sg, ref int puiPHMsgLen, int ilfOpen);Dlllmport(sdtapi.dir)public static extern int SDT_ReadBaseMsgToFile(int iPortID, string fileNamel, ref int puiCHMsgLen, string fileName2, ref int puiPHMsgLen, int ilfOpen);public static extern int SDT_WriteAppMsg(int iPortID, ref byte pucSendData, int uiSendLen,
7、 ref byte pucRecvData, ref int puiRecvLen, int ilfOpen);Dlllmport(sdtapi.dir)public static extern int SDT_WriteAppMsgOK(int i Port ID, ref byte pucData, int uiLen, int ilfOpen);DlllmportCsdtapi.dir)public static extern int SDT_CancelWriteAppMsg(int iPortID, int ilfOpen);Dlllmport(sdtapi.dir)public s
8、tatic extern int SDT_ReadNewAppMsg(int iPortID, ref byte pucAppMsg, ref int puiAppMsgLen, int ilfOpen);Dlllmport(,sdtapi.dll,)public static extern int SDT_ReadAIIAppMsg(int iPortID, ref byte pucAppMsg, ref int puiAppMsgLen, int ilfOpen);Dlllmport(sdtapi.dir)public static extern int SDT_UsableAppMsg(
9、int iPortID, ref byte ucByte, int ilfOpen);DlllmportCsdtapi.dir)public static extern int SDT_GetUnlockMsg(int i Port ID, ref byte strMsg, int ilfOpen);Dlllmport(sdtapi.dir)public static extern int SDT_GetSAMID(int iPortID, ref byte StrSAMID, int ilfOpen);DlllmportCsdtapi.dll1)public static extern in
10、t SDT_SetMaxRFByte(int iPortID, byte ucByte, int ilfOpen);DlllmportCsdtapi.dll)public static extern int SDT_ResetSAM (int iPortID, int i If Open);A href=DIIImportCWItRS.dir)public static extern int GetBmp(string file_name, int intf);public delegate void De_ReadlCCardComplete(clsEDZ objEDZ);public ev
11、ent De_ReadlCCardComplete ReadICCardComplete;private cIsEDZ objEDZ = new clsEDZ();private int EdzilfOpen = 1;自动开关串 口int EdziPortID;public clslCCard()()public bool ReadlCCard()(bool bUsbPort = false;int intOpenPortRtn = 0;int rtnTemp = 0;int pucllN = 0;int pucSN = 0;int puiCHMsgLen = 0;int puiPHMsgLe
12、n = 0;objEDZ = new clsEDZ();检测usb 的机具连接,必须先检测usbfor (int iPort = 1001; iPort = 1016; iPort+)if (intOpenPortRtn = 144)EdziPortID = iPort;bllsbPort = true;break;)检测串口的机具连接if (IbUsbPort)(for (int iPort = 1; iPort = 2; iPort+)(intOpenPortRtn = SDT_OpenPort(iPort);if (intOpenPortRtn = 144)EdziPortID = iP
13、ort;bllsbPort = false;break;MessageBox.ShowC端口打开失败,请检测相应的端口或者重新连接读卡器!,提示, MessageBoxButtons.OK, MessageBox Icon .Error);return false;)在这里,如果您想下一次不用再耗费检査端口的检査的过程,您可以把EdziPortID保存下来,可以 保存在注册表中,也可以保存在配置文件中,我就不多写了,但是,您要考虑机具连接端口被用户改变的情况哦下面找卡rtnTemp = SDT_StartFindlDCard(EdziPortlD, ref pucllN, EdzilfOpen
14、);if (rtnTemp != 159)(rtnTemp = SDT_StartFindlDCard(EdziPortlD, ref pucllN, EdzilfOpen); 再找卡if (rtnTemp != 159)(rtnTemp = SDT_ClosePort(EdziPortlD);MessageBox.ShowC未放卡或者卡未放好,请重新放卡!,提示,MessageBoxButtons.OK, MessageBox Icon.Error);return false;)选卡rtnTemp = SDT_SelectlDCard(EdziPortlD, ref pucSN, Edzil
15、fOpen);if (rtnTemp != 144)rtnTemp = SDT_SelectlDCard(EdziPortlD, ref pucSN, EdzilfOpen); 再选卡if (rtnTemp != 144)(rtnTemp = SDT_ClosePort(EdziPortlD);MessageBox.Show(读卡失败!,提示,MessageBoxButtons.OK, MessageBoxIcon.Error);return false;)注意,在这里,用户必须有应用程序当前目录的读写权限检测串口的机具连接if (IbUsbPort)(for (int iPort = 1;
16、iPort = 2; iPort+)(intOpenPortRtn = SDT_OpenPort(iPort);if (intOpenPortRtn = 144)(EdziPortID = iPort;bllsbPort = false;break;if (intOpenPortRtn != 144)(MessageBox.Show(端口打开失败,请检测相应的端口或者重新连接读卡器!,提示, MessageBoxButtons.OK, MessageBox Icon .Error);return false;在这里,如果您想下一次不用再耗费检查端口的检查的过程,您可以把EdziPortID保存
17、下来,可以 保存在注册表中,也可以保存在配置文件中,我就不多写了,但是,您要考虑机具连接端n被用户改变的情况哦下面找卡rtnTemp = SDT_StartFindIDCard(EdziPortID, ref pucllN, EdzilfOpen);A href=http:www.bird-if (rtnTemp != 159)(rtnTemp = SDT_StartFindlDCard(EdziPortlD, ref pucllN, EdzilfOpen); 再找卡if (rtnTemp != 159)(rtnTemp = SDT_ClosePort(EdziPortlD);MessageB
18、ox.Show未放卡或者卡未放好,请重新放卡!提示。,MessageBoxButtons.OK, MessageBox Icon .Error);return false;选卡rtnTemp = SDT_SelectlDCard(EdziPortlD, ref pucSN, EdzilfOpen);if (rtnTemp != 144)(rtnTemp = SDT_SelectlDCard(EdziPortlD, ref pucSN, EdzilfOpen)i 再选 kif (rtnTemp != 144)rtnTemp = SDT_ClosePort(EdziPortlD);MessageB
19、ox.Show(读卡失败!,提示,MessageBoxButtons.OK, MessageBoxIcon.Error);return false;)注意,在这里,用户必须有应用程序当前冃录的读写权限Fileinfo objFile = new Filelnfo(,wz.txt);if (objFile.Exists)(objFile.Attributes = FileAttributes.Normal;objFile.Delete();)objFile = new Filelnfo(zp.bmp*);if (objFile.Exists)objFile.Attributes = FileAt
20、tributes.Normal;objFile.Delete();)objFile = new Filelnfo(*zp.wlt);if (objFile.Exists)(objFile.Attributes = FileAttributes.Normal;objFile.Delete();)rtnTemp = SDT_ReadBaseMsgToFile(EdziPortlD, *wz.txt*, ref puiCHMsgLen, *zp.wlt ref puiPHMsgLen, EdzilfOpen);if (rtnTemp != 144)(rtnTemp = SDT_ClosePort(E
21、dziPortlD);MessageBox.Show(读卡失败!,提示,MessageBoxButtons.OK, MessageBoxIcon.Error);return false;)下面解析照片,注意,如果在C盘根目录下没有机具厂商的授权文件Termb.Lic,照片解析将会失败if (bUsbPort)rtnTemp = GetBmp(zp.wlt, 2);elsertnTemp = GetBmp(zp.wlt, 1);switch (rtnTemp)case 0:MessageBox.Show(调用 sdtapi.dl! 错误!,提示,MessageBoxButtons.OK, Mes
22、sageBoxIcon.Error);break;case 1: 正常break;case -1:MessageBox.Show相片解码错误!,提示,MessageBoxButtons.OK, MessageBox Icon .Error);break;case -2:MessageBox.Show(,wlt 文件后缀错误!,提示,MessageBoxButtons.OK, MessageBox Icon .Error);break;case -3:MessageBox.Show(wlt 文件打开错误!,提示,MessageBoxButtons.OK, MessageBox Icon .Err
23、or);break;case -4:MessageBox.Show(wlt 文件格式错误!,提示,MessageBoxButtons.OK, MessageBox Icon. Error);break;case -5:MessageBox.Show(软件未授权!,提示,MessageBoxButtons.OK, MessageBoxIcon.Error);break;case -6:MessageBox.Show。设备连接错误! 二提示,MessageBoxButtons.OK, MessageBoxIcon.Error);break;)rtnTemp = SDT_ClosePort(Edzi
24、 Port ID);Fileinfo f = new Filelnfo(wz.txt);FileStream fs = f.OpenRead();byte bt = new byteffs.Length;fs.Read(bt, 0, (int)fs.Length);fs.Close();string str = System.Text.UnicodeEncoding.Unicode.GetString(bt);objEDZ.Name = System.Text.UnicodeEncoding.Unicode.GetString(bt, 0, 30).Trim();objEDZ.Sex_Code
25、 = System.Text.UnicodeEncoding.Unicode.GetString(bt, 30, 2).Trim();objEDZ.NATION_Code = System.Text.UnicodeEncoding.Unicode.GetString(bt, 32, 4).Trim();string strBird = System.Text.UnicodeEncoding.Unicode.GetString(bt, 36, 16).Trim();objEDZ.BIRTH = Convert.ToDateTime(strBird.Substring(0, 4) + 年+ str
26、Bird.Substring(4, 2) + 月+ strBird.Substring(6) + 日);objEDZ.ADDRESS = System.Text.UnicodeEncoding.Unicode.GetString(bt, 52, 70).Trim();objEDZ.lDC = System.Text.UnicodeEncoding.Unicode.GetString(bt, 122, 36).Trim();objEDZ.REGORG = System.Text.UnicodeEncoding.Unicode.GetString(bt, 158, 30).Trim();strin
27、g strTem = System.Text.UnicodeEncoding.Unicode.GetString(bt, 188, bt.GetLength(O)- 188).Trim();objEDZ.STARTDATE = Convert.ToDateTime(strTem.Substring(0, 4) + 年,+ strTem.Substring(4, 2) + 月+ strTem.Substring(6, 2) + 日);strTem = strTem.Substring(8);if (strTem.Trim() !=长期)objEDZ.ENDDATE = Convert.ToDat
28、eTime(strTem.Substring(0, 4) +,年+ strTem.Substring(4, 2) + 月 + strTem.Substring(6, 2) + 甘);)elseobjEDZ.ENDDATE = DateTime. Max Value;)objFile = new Filelnfo(zp.bmp);if (objFile.Exists)(Image img = Image.FromFile(*zp.bmp4);objED 乙 PIC_lmage=(lmage)img.Clone();System.lO.MemoryStream m = new MemoryStre
29、am();img.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);objEDZ.PIC_Byte = m.ToArray();img.Dispose();img = null;)ReadlCCardComplete(objEDZ);return true;public class cIsEDZprivate System.Collections.SortedList IstMZ = new SortedList();private string _NameI 姓名private string _Sex_Code;性别代码private stri
30、ng _Sex_CName; 性别private string _IDC; 身份证号码private string _NATION_Code; 民族代码private string _NATION_CNameJ 民族private DateTime _BIRTHJ 出生日期private string _ADDRESS; 住址private string _REGORGJ签发机关private DateTime .STARTDATE;身份证有效起始日期private DateTime _ENDDATE;身份证有效截至日期private string _Period_Of_Validity=Co
31、de;有效期限代码,许多原来系统上面为了一代证考虑,常常存在这样的字段,二代证中已经没有了private string _Period_Of_Validity_CName;有效期限private byte _PIC_Byte;照片二进制private Image _PIC_lmage; 照片public clsEDZ()lstMZ.Add(O2蒙占族);IstMZ.Addg 回族);lstMZ.Add(X)4,藏族);IstMZ.Addg 维吾尔族);lstMZ.Add(O6,苗族);lstMZ.Add(l07,彝族);lstMZ.Add(O8,壮族);lstMZ.Add(O9,布依族);Is
32、tMZ.Add。,朝鲜族);lstMZ.Add(11,满族);lstMZ.Add2,侗族);lstMZ.Add(13,瑶族);lstMZ.Add(14白族);lstMZ.Add(l15, 土家族);lstMZ.Add(*16,哈尼族);IstMZ.Add“,哈萨克族);lstMZ.Add(18,傣族);lstMZ.Add”9,黎族);lstMZ.Add(2O,傑傑族);lstMZ.AddC21 仮族);lstMZ.Add(23高山族);lstMZ.Add(24拉祜族);lstMZ.Add(25水族);IstMZ. Add(26东乡族);lstMZ.Add(27纳西族);IstM 乙 Add(,
33、28景颇族);lstMZ.Add(29柯尔克孜族);lstMZ.Add(3O土族);lstMZ.Add(31达翰尔族);lstMZ.Add(32么佬族);lstMZ.Add(33羌族);lstMZ.Add(34布朗族);lstMZ.Add(35撒拉族);lstMZ.Add(36毛南族,);lstMZ.Add(37化佬族);lstMZ.Add(38锡伯族);lstMZ.Add(39阿昌族);lstMZ.Add(40普米族);lstMZ.Add(41塔吉克族);lstMZ.Add(42怒族);lstMZ.Add(43乌孜别克族);IstMZ. Add(44俄罗斯族);lstMZ.Add(46,德昂
34、族);lstMZ.Add(47,保安族);lstMZ.Add(48,裕固族);lstMZ.Add(49,京族);lstMZ.Add(5O,塔塔尔族);lstMZ.Add(51,独龙族);lstMZ.Add(52,鄂伦春族);lstMZ.Add(53,赫哲族);lstMZ.Add(54, 门巴族);lstMZ.Add(55,珞巴族);lstMZ.Add(56,基诺族);lstMZ.Add(57,其它);lstMZ.Add(98,国人入籍);)public string Name(get return _Name; set _Name = value; )public string Sex_Cod
35、eset_Sex_Code = value;switch (value)(case *1*:Sex_CName =身;break;case 2:Sex_CName =女;break;)public string Sex_CName(get return _Sex_CName; set _Sex_CName = value; )public string I DCget return _IDC; set _IDC = value; )public string NATION_Code(get return _NATION_Code; set(_NATION_Code = value;if (Is
36、tM乙Contains(value)NATION_CName = lstMZvalue.ToString();)public string NATION_CName(get return _NATION_CName; set _NATION_CName = value; )public DateTime BIRTH(get return _BIRTH; set _BIRTH = value; public string ADDRESSget return _ADDRESS; set _ADDRESS = value; public string REGORG(get return _REGOR
37、G; set _REGORG = value; public DateTime STARTDATE(get return _STARTDATE; set _STARTDATE = value; )public DateTime END DATE(get return _ENDDATE; set(_ENDDATE = value;if (_ENDDATE = DateTime.MaxValue)_Period_OLValidity_Code = 3;_Period_Of_Validity_CName =长期;)else(if (_STARTDATE != DateTime.MinValue)(s
38、witch (value.AddDays(1).Year - _STARTDATE.Year)(case 5:_Period_Of_Validity_Code = 4;_Period_Of_Validity_CName = 5 年;break;case 10:_Period_Of_Validity_Code = T;_Period_Of_Validity_CName = 。年,;break;case 20:_Period_Of_Validity_Code = 2;_Period_OfValidity_CName = *20 年;break;)public string Period_Of_Va
39、lidity_Code(get return _Period_Of_Validity_Code; set _Period_Of_Validity_Code = value; )public string Period_Of_Validity_CName(get return _Period_Of_Validity_CName; set _Period_Of_Validity_CName = value;)public byte PIC_Byte(get return _PIC_Byte; set _PIC_Byte = value;)public Image PIC_lmageget retu
40、rn _PIC_lmage; set _PIC_lmage = value; )转载二代身份证读卡器接口函数Delphi声明(2010-06-30 22:09:33)转载原文标签:转载原文地址:二代身份证读卡器接n函数Delphi声明作者:虚伪人牛.前段时间做项目时用的,拿来看全是C+的dll,就对应写了一下Delphi的声明 查看串口当前波特率function SDT_GetCOMBaud(iPort: integer; puiBaudRate: Pinteger):integer;stdcall;External SDTAPI. DLL;iPortin表示端口,puiBaudRateout
41、无符号指针,指向普通串口当前波特率,返回值 0X90一成功,0x1一端口打开失败/端口号不合法,0x5无法获得SAM_V的波特率,串口不可用。 设置SAM V的串口的波特率function SDT_StetCOMBaud(iPort: integer; uiCurrBaud: integer; uiSetBaud: integer): integer;stdcall ;External SDTAPI. DLL ; 手持终端 iPort in表示端口,uiCurrBaudin调用改API前已设置的业务终端与SAM V通信的波特 率(出厂默认为115200)uiCurrBaud 只能为 11520
42、, 57600, 378400, 19200, 9600。如果 uiCurrBaud 数值不是这些值之 一,函数返回0x21,如果和已设置的不一样,函数返回0x02表示不能设置调用AP!不成功。uiSetBaudin将要设置的SAM_V波特率,只 能为(同上)那些值,如果不是这些数值,返回也同上函数返回0x90一成功,0x1一端口打开失败/端口号不合法,0x2-超时,设置不成功,0x21 -uiCurrBaud, uiSetBaud 输入参数数值错误打开串口/USBfunction SDT OpenPort(iPort: integer): integer;stdcall;External1
43、SDTAPL DLL, ;iPortin表示端口号,1-16(十进制)为串口,1001-1016(十进制)为USB ,缺省的个USB设备端口是1001o函数返回0x90一打开端口成功,1打开端口失败/端口号不合法关闭串口/USBfunction SDT ClosePort(iPort: integer): integer;stdcall;ExternalSDTAPL DLL;iPort in表示端口号,返回值0x90关闭串口成功,0x01一端口号不合法对SAM_V复位function SDT_ResetSAM(iPort: integer; ilfOpen: integer):integer;
44、stdcal1;External SDTAPL DLL;iPortin表示端口号,目前串 口和 USB 只支持 16 个,串口: 0001-0016, USB: 1001 -1016。 ilfOpenin表示不在该函数内部打开和关闭串口,非表示在API函数内部包含了打开串 口和关闭串口函数,之前不需要调用SDT_OpenPort 和 SDT_ClosePort返回值0x90成功,其他失败设置射频适配器最大通信字节数function SDT SetMaxRFByte(iPort: integer;ucByte: Char;blf0pen:integer):integer;stdcal1;External SDTAPL DLL;iPort in表示端口号,ucBytein无符号字符,24-255,表示射频适配器最大通信字节数, ilf0penin参见 SDT_ResetSAM返回值0x90成功,其他一失败对SAM V进行状态检测function SDT_GetSAMStatus(iPort: integer;ilfOpen:integer):integer;stdcal1;External SDTAPL DLL;