using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Threading; using System.Windows.Forms; namespace dccdc.Selfhelp { public partial class frm_readCard : Form { public frm_readCard() { InitializeComponent(); } IDCardInfo _info; public frm_readCard(IDCardInfo info) : this() { _info = info; } private void frm_readCard_Load(object sender, EventArgs e) { this.TopMost = true; int port = 0; string sport = System.Configuration.ConfigurationManager.AppSettings["commPort"]; if (!int.TryParse(sport, out port)) { port = 1; } try { extend.close_comm(); extend.open_comm(port); extend.send_comm(13); } catch (Exception ex) { Status s = new Status(ex.Message); s.ShowDialog(); this.Close(); } // extend.test(1, 1); } /// /// 没有读到卡 /// int readcard = 0; /// /// 身份证信息 /// IDCardInfo info = new IDCardInfo(); int returncode = 0; int messagecode = 0; string errmsg = ""; private void t_readcard() { //MessageBox.Show("1"); IDCardData CardMsg = new IDCardData(); //extend.Init(); int code = 0; byte[] photoPath = new byte[260]; int r = extend.Read_IDCard(ref CardMsg, ref photoPath[0], ref code); if (r == 0 || r == 1) { info.Name = CardMsg.Name; info.Nation = CardMsg.Nation; info.IDCardNo = CardMsg.IDCardNo; info.GrantDept = CardMsg.GrantDept; info.Born = CardMsg.Born; info.Address = CardMsg.Address; info.UserLifeEnd = CardMsg.UserLifeEnd; info.UserLifeBegin = CardMsg.UserLifeBegin; info.Sex = CardMsg.Sex; info.reserved = CardMsg.reserved; info.PhotoFileName = CardMsg.PhotoFileName; info.FPInfo = System.Text.Encoding.ASCII.GetString(photoPath); info.FPInfo = info.FPInfo.Replace("\0", ""); if (!string.IsNullOrEmpty(info.PhotoFileName) && System.IO.File.Exists(info.PhotoFileName)) { FileStream fs = new FileStream(info.PhotoFileName, FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); fs.Close(); info.Photo = System.Drawing.Image.FromStream(new MemoryStream(bs)); File.Delete(info.PhotoFileName); } extend.Close_IDCard(); readcard = 1; } else { readcard = 1; returncode = r; messagecode = code; } } private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; try { readcard = 0; Thread t = new Thread(t_readcard); t.Start(); for (int i = 0; i <= 10 * 100; i++) { Thread.CurrentThread.Join(10); Application.DoEvents(); if (readcard == 1) { break; } } t.Abort(); if (readcard != 1) { extend.Close_IDCard(); Status s = new Status("没有检测到身份证"); s.ShowDialog(); s.Close(); this.Close(); return; } int r = returncode; int code = messagecode; if (r == 0 || r == 1) { //_info = new IDCardInfo(); _info.Name = info.Name; _info.Nation = info.Nation; _info.IDCardNo = info.IDCardNo; _info.GrantDept = info.GrantDept; _info.Born = info.Born; _info.Address = info.Address; _info.UserLifeEnd = info.UserLifeEnd; _info.UserLifeBegin = info.UserLifeBegin; _info.Sex = info.Sex; _info.reserved = info.reserved; _info.PhotoFileName = info.PhotoFileName; _info.FPInfo = info.FPInfo; _info.FPInfo = info.FPInfo.Replace("\0", ""); /*if (!string.IsNullOrEmpty(_info.PhotoFileName) && System.IO.File.Exists(_info.PhotoFileName)) { FileStream fs = new FileStream(_info.PhotoFileName, FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); fs.Close(); _info.Photo = System.Drawing.Image.FromStream(new MemoryStream(bs)); File.Delete(_info.PhotoFileName); }*/ _info.Photo = info.Photo; extend.Close_IDCard(); //_info = info; this.DialogResult = DialogResult.OK; } else { if (r != -1) { Status s = new Status("读取身份证失败:" + code + "_" + r); extend.Close_IDCard(); s.ShowDialog(); } this.Close(); } } catch (Exception ex) { Status s = new Status(ex.Message); extend.Close_IDCard(); s.ShowDialog(); this.Close(); } finally { extend.Close_IDCard(); //extend.send_comm(14); } } private void frm_readCard_FormClosing(object sender, FormClosingEventArgs e) { extend.Close_IDCard(); extend.send_comm(14); extend.close_comm(); } } }