微客互联
氵沐子-微客互联欢迎您的光临>>

明泰MT8身份证读卡器c#代码by山西导通

下载附件:明泰MT8身份证读卡器c#

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Runtime.InteropServices;
  6 using System.Drawing;
  7 using System.IO;
  8 //明泰身份证读取功能
  9 namespace mt8
 10 {
 11     /// <summary>
 12     /// 身份证读取
 13     /// </summary>
 14     public class SFZ_32dll
 15     {
 16 
 17         /// <summary>
 18         /// 打开设备
 19         /// </summary>
 20         /// <param name="nPort">usb为0</param>
 21         /// <param name="ulBaud">9600</param>
 22         /// <returns>>0为读取成功</returns>
 23         [DllImport("mtx_32.dll", EntryPoint = "device_open", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 24         public static extern int device_open(int nPort, long ulBaud);
 25         /// <summary>
 26         /// 关闭设备
 27         /// </summary>
 28         /// <param name="icdev">设备id</param>
 29         /// <returns>0为成功</returns>
 30         [DllImport("mtx_32.dll", EntryPoint = "device_close", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 31         public static extern int device_close(int icdev);
 32 
 33 
 34         /// <summary>
 35         /// 开始读取身份证
 36         /// </summary>
 37         /// <param name="icdev">设备id</param>
 38         /// <param name="message">错误信息</param>
 39         /// <returns>0为成功</returns>
 40         [DllImport("mtx_32.dll", EntryPoint = "IDCard_ReadCard", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 41         public static extern int IDCard_ReadCard(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] message);
 42         /// <summary>
 43         /// 获取身份证信息
 44         /// </summary>
 45         /// <param name="icdev">设备id</param>
 46         /// <param name="index">0--姓名,1--性别,2--民族,3--出生日期,4--住址,5--公民身份号码,6--签发机关,7--有效期起始日期,8--有效期截止日期,9--预留区,10--照片文件名。</param>
 47         /// <param name="infodata"></param>
 48         /// <returns></returns>
 49         [DllImport("mtx_32.dll", EntryPoint = "IDCard_GetCardInfo", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 50         public static extern int IDCard_GetCardInfo(int icdev, int index, [MarshalAs(UnmanagedType.LPArray)]byte[] infodata);
 51 
 52         /// <summary>
 53         /// 读取身份证上的数据,同时可以设置照片保存路径。
 54         /// </summary>
 55         /// <param name="icdev">设备id</param>
 56         /// <param name="lpInPhoneSaveFile">照片路径</param>
 57         /// <param name="message">返回错误信息</param>
 58         /// <returns>0为正确</returns>
 59         [DllImport("mtx_32.dll", EntryPoint = "IDCard_ReadCard_Extra", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 60         public static extern int IDCard_ReadCard_Extra(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] lpInPhoneSaveFile, [MarshalAs(UnmanagedType.LPArray)]byte[] message);
 61    
 62        [DllImport("mtx_32.dll", EntryPoint = "device_beep", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
 63        public static extern int device_beep(int icdev, short delaytime,int times);
 64     }
 65     public class MT8IDCardRead
 66     {
 67         /// <summary>
 68         /// 身份证信息
 69         /// </summary>
 70         public IDCard IDCard1; 
 71         /// <summary>
 72         /// 读取身份证信息不包含图片
 73         /// </summary>
 74         /// <returns>是否成功</returns>
 75         public bool GetIDCardInfo()
 76         {
 77             int idev = 0;
 78             try
 79             {
 80                 IDCard1 = new IDCard();
 81                 int isOp = 0;
 82                 isOp = mt8.SFZ_32dll.device_open(0, 9600);
 83                 if (isOp > 0)
 84                 {
 85                     idev = isOp;
 86                     byte[] msr = new byte[100];
 87                   int rs=mt8.SFZ_32dll.IDCard_ReadCard(isOp,msr);
 88                
 89                   if (rs==0)
 90                   {
 91                       for (int i = 0; i < 10; i++)
 92                       {
 93                           byte[] info = new byte[79];
 94                           mt8.SFZ_32dll.IDCard_GetCardInfo(isOp,i,info);
 95                           string infoStr=UnicodeEncoding.Default.GetString(info);
 96                           switch (i)
 97                           {
 98                             case  0:
 99                              IDCard1.SName =infoStr.Trim();
100                              break;
101                             case 1:
102                              IDCard1.SSex = infoStr.Trim();
103                              break;
104                             case 2:
105                              IDCard1.SNation = infoStr.Trim();
106                              break;
107                             case 3:
108                              infoStr = infoStr.Trim().Insert(4, ".");
109                              infoStr = infoStr.Trim().Insert(7, ".");
110                              IDCard1.DBirthday =DateTime.Parse(infoStr);
111                              break;
112                             case 4:
113                              IDCard1.SAddress = infoStr.Trim();
114                              break;
115                             case 5:
116                              IDCard1.SIDNumber = infoStr.Trim();
117                              break;
118                             case 6:
119                              IDCard1.SIssueDepartment = infoStr.Trim();
120                              break;
121                             case 7:
122                              infoStr = infoStr.Trim().Insert(4, ".");
123                              infoStr = infoStr.Trim().Insert(7, ".");
124                              IDCard1.DValidFromDate =DateTime.Parse(infoStr);
125                              break;
126                             case 8:
127                              infoStr = infoStr.Trim().Insert(4, ".");
128                              infoStr = infoStr.Trim().Insert(7, ".");
129                              IDCard1.DValidExpiryDate = DateTime.Parse(infoStr);
130                              break;
131                             case 9:
132                              IDCard1.SReserve = infoStr.Trim();
133                              break;
134                               default:
135                                   break;
136                           }
137                          
138 
139                       }
140                       mt8.SFZ_32dll.device_beep(isOp, 1285, 2);
141                   }
142                   else
143                   {
144                       IDCard1.Message1 = "身份证读取失败!" + UnicodeEncoding.Default.GetString(msr);
145                       return false;
146                   }
147 
148                 }
149                 else
150                 {
151                     IDCard1.Message1 = "读卡器连接失败!";
152                     return false;
153                 }
154             }
155             catch (Exception)
156             {
157 
158                 IDCard1.Message1 = "读取信息异常请重试!";
159                 return false;
160             }
161             finally
162             {
163                 try
164                 {
165                     if (idev>0)
166                     {
167                       int a=  mt8.SFZ_32dll.device_close(idev);
168                       if (a==0)
169                       {
170                           IDCard1.Message1 += "读卡器已关闭!";
171                       }
172                       else
173                       {
174                           IDCard1.Message1 += "读卡器关闭失败!";
175                       }
176                     }
177                 }
178                 catch
179                 {
180                 }
181             }
182             return true;
183         }
184         /// <summary>
185         /// 读取身份证信息包含图片
186         /// </summary>
187         /// <returns>是否成功</returns>
188         public bool GetIDCardInfoWithPhoto()
189         {
190             int idev = 0;
191             try
192             {
193                 IDCard1 = new IDCard();
194                 string pathstr = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\IDCard.bmp"; 
195                 try
196                 {
197                     FileInfo fi = new FileInfo(pathstr);
198 
199                     if (fi.Exists)
200                     {
201                         fi.Delete();
202                     }
203 
204                 }
205                 catch
206                 {
207                 }
208                 int isOp = 0;
209                 isOp = mt8.SFZ_32dll.device_open(0, 9600);
210                 if (isOp > 0)
211                 {
212                     idev = isOp;
213                     byte[] msr = new byte[100];
214 
215                     byte[] path = System.Text.Encoding.ASCII.GetBytes(pathstr);
216                     int rs = mt8.SFZ_32dll.IDCard_ReadCard_Extra(isOp,path,msr);                    
217                     if (rs == 0)
218                     {
219                         for (int i = 0; i < 10; i++)
220                         {
221                             byte[] info = new byte[79];
222                             mt8.SFZ_32dll.IDCard_GetCardInfo(isOp, i, info);
223                             string infoStr = UnicodeEncoding.Default.GetString(info);
224                             switch (i)
225                             {
226                                 case 0:
227                                     IDCard1.SName = infoStr.Trim();
228                                     break;
229                                 case 1:
230                                     IDCard1.SSex = infoStr.Trim();
231                                     break;
232                                 case 2:
233                                     IDCard1.SNation = infoStr.Trim();
234                                     break;
235                                 case 3:
236                                     infoStr = infoStr.Trim().Insert(4, ".");
237                                     infoStr = infoStr.Trim().Insert(7, ".");
238                                     IDCard1.DBirthday = DateTime.Parse(infoStr);
239                                     break;
240                                 case 4:
241                                     IDCard1.SAddress = infoStr.Trim();
242                                     break;
243                                 case 5:
244                                     IDCard1.SIDNumber = infoStr.Trim();
245                                     break;
246                                 case 6:
247                                     IDCard1.SIssueDepartment = infoStr.Trim();
248                                     break;
249                                 case 7:
250                                     infoStr = infoStr.Trim().Insert(4, ".");
251                                     infoStr = infoStr.Trim().Insert(7, ".");
252                                     IDCard1.DValidFromDate = DateTime.Parse(infoStr);
253                                     break;
254                                 case 8:
255                                     infoStr = infoStr.Trim().Insert(4, ".");
256                                     infoStr = infoStr.Trim().Insert(7, ".");
257                                     IDCard1.DValidExpiryDate = DateTime.Parse(infoStr);
258                                     break;
259                                 case 9:
260                                     IDCard1.SReserve = infoStr.Trim();
261                                     break;
262                                 default:
263                                     break;
264                             }
265 
266                             mt8.SFZ_32dll.device_beep(isOp, 1285, 2);
267                         }
268                         Image cardim = GetImageFromPath(pathstr);
269                         if (cardim!=null)
270                         {
271                             IDCard1.BPhoto = cardim;
272                             IDCard1.SPhotoPath = pathstr;
273                         }
274 
275                     }
276                     else
277                     {
278                         IDCard1.Message1 = "身份证读取失败!" + UnicodeEncoding.Default.GetString(msr).Trim();
279                         return false;
280                     }
281 
282                 }
283                 else
284                 {
285                     IDCard1.Message1 = "读卡器连接失败!";
286                     return false;
287                 }
288             }
289             catch (Exception eer)
290             {
291 
292                 IDCard1.Message1 = "读取信息异常请重试!"+eer.Message;
293                 return false;
294             }
295             finally
296             {
297                 try
298                 {
299                     if (idev > 0)
300                     {
301                         int a = mt8.SFZ_32dll.device_close(idev);
302                         if (a == 0)
303                         {
304                             IDCard1.Message1 += "读卡器已关闭!";
305                         }
306                         else
307                         {
308                             IDCard1.Message1 += "读卡器关闭失败!";
309                         }
310                     }
311                 }
312                 catch
313                 {
314                 }
315             }
316             return true;
317         }
318         /// <summary>
319         /// 通过路径获取图片
320         /// </summary>
321         /// <param name="path"></param>
322         /// <returns></returns>
323         Image GetImageFromPath(string path)
324         {
325             Image im = null;
326             try
327             {
328                 if (File.Exists(path))
329                 {
330                     FileStream fs = File.OpenRead(path);
331                     im = Image.FromStream(fs);
332                     fs.Close();
333                     
334                 }
335             }
336             catch (Exception)
337             {
338             }
339             return im;
340         }
341     }
342     /// <summary>
343     /// 身份证信息类
344     /// </summary>
345     public class IDCard
346     {
347         string sName;
348         /// <summary>
349         /// 姓名
350         /// </summary>
351         public string SName
352         {
353             get { return sName; }
354             set { sName = value; }
355         }
356         string sSex; //性别
357         /// <summary>
358         /// 性别
359         /// </summary>
360         public string SSex
361         {
362             get { return sSex; }
363             set { sSex = value; }
364         }
365         string sNation; //名族
366         /// <summary>
367         /// 名族
368         /// </summary>
369         public string SNation
370         {
371             get { return sNation; }
372             set { sNation = value; }
373         }
374         DateTime dBirthday; //出生日期
375         /// <summary>
376         /// 出生日期
377         /// </summary>
378         public DateTime DBirthday
379         {
380             get { return dBirthday; }
381             set { dBirthday = value; }
382         }
383         string sAddress; //出生地址
384         /// <summary>
385         /// 出生地址
386         /// </summary>
387         public string SAddress
388         {
389             get { return sAddress; }
390             set { sAddress = value; }
391         }
392         string sIDNumber; //身份证号码
393         /// <summary>
394         /// 身份证号码
395         /// </summary>
396         public string SIDNumber
397         {
398             get { return sIDNumber; }
399             set { sIDNumber = value; }
400         }
401         string sIssueDepartment; //签发机关
402         /// <summary>
403         /// 签发机关
404         /// </summary>
405         public string SIssueDepartment
406         {
407             get { return sIssueDepartment; }
408             set { sIssueDepartment = value; }
409         }
410         DateTime dValidFromDate; //证件有效期起始日期
411         /// <summary>
412         /// 证件有效期起始日期
413         /// </summary>
414         public DateTime DValidFromDate
415         {
416             get { return dValidFromDate; }
417             set { dValidFromDate = value; }
418         }
419         DateTime dValidExpiryDate; //证件有效期截至日期
420         /// <summary>
421         /// 证件有效期截至日期
422         /// </summary>
423         public DateTime DValidExpiryDate
424         {
425             get { return dValidExpiryDate; }
426             set { dValidExpiryDate = value; }
427         }
428         string sReserve; //预留信息
429         /// <summary>
430         /// 预留信息
431         /// </summary>
432         public string SReserve
433         {
434             get { return sReserve; }
435             set { sReserve = value; }
436         }
437         Image bPhoto;
438         /// <summary>
439         /// 照片
440         /// </summary>
441         public Image BPhoto
442         {
443             get { return bPhoto; }
444             set { bPhoto = value; }
445         }
446         string Message; //错误信息
447         /// <summary>
448         /// 错误信息
449         /// </summary>
450         public string Message1
451         {
452             get { return Message; }
453             set { Message = value; }
454         }
455 
456         string sPhotoPath;
457         /// <summary>
458         /// 图片路径 
459         /// </summary>
460         public string SPhotoPath
461         {
462             get { return sPhotoPath; }
463             set { sPhotoPath = value; }
464         }
465     }
466     
467 }
View Code

 

posted on 2014-08-29 09:56  氵沐子  阅读(2647)  评论(1编辑  收藏  举报