http://www.oschina.net/code/snippet_121944_14983
2015-07-07 10:18
155 查看
</pre><h1>C#读取第二代居民身份证信息</h1><p>依赖termb.dll及HWControl.dll </p><p align="center"><span style="font-size:24px;">第一种:</span></p><p align="left"><h3><em>1. </em> IDCard.cs </h3></p><p><pre name="code" class="csharp">using System; using System.Collections.Generic; using System.Text; namespace ysc { public class IDCard { public string Name;//姓名 public string Sex;//性别 public string CartNo; //身份证号 public string Nationality; //民族 public string Birthday; //生日 public string Address;//家庭住址 public string Institution;//发证机关 public string Begin_validity;// 有效期开始 public string End_validity;//有效期结束 public byte[] Photo;//照片; public string NewAddress;//最新住址 } }
2. IDCardForm.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace ysc { public partial class IDCardForm : Form { private IDCard card = null; private Form form = null; public IDCardForm(Form form) { InitializeComponent(); this.form = form; } public void Clear() { this.Photo.Image = null; this.UserName.Text = ""; this.Sex.Text = ""; this.Birthday.Text = ""; this.Address.Text = ""; this.Nationality.Text = ""; this.CartNo.Text = ""; this.Institution.Text = ""; this.Validity.Text = ""; this.tip.Text = ""; } private void ReadIDCard_Click(object sender, EventArgs e) { Clear(); this.tip.Text = "正在读卡"; this.ReadIDCard.Enabled = false; this.Refresh(); try { ReadIDCardService service = new ReadIDCardService(); card = service.Read(); tip.Text = "读卡成功"; Display(); } catch (Exception ex) { tip.Text = "读卡失败"; MessageBox.Show(ex.Message); } this.ReadIDCard.Enabled = true; } public void SafeDisplay(IDCard card) { this.card = card; this.Invoke(new EventHandler(DisplayCard)); } private void DisplayCard(object o, EventArgs e) { Display(); } private void Display() { MemoryStream ms = new MemoryStream(card.Photo);//将字节数组存入到二进制流中 ms.Position = 0; Bitmap img = new Bitmap(ms); ms.Close(); this.UserName.Text = card.Name; this.Photo.Image = img; this.Sex.Text = card.Sex; this.Birthday.Text = card.Birthday; this.Address.Text = card.Address; this.Nationality.Text = card.Nationality; this.CartNo.Text = card.CartNo; this.Institution.Text = card.Institution; this.Validity.Text = card.Begin_validity+" 至 "+card.End_validity; } private void Return_Click(object sender, EventArgs e) { form.Show(); this.Hide(); this.Dispose(); } } }
3. ReadIDCardService.cs
using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; using System.IO; using System; using System.Text; namespace ysc { class ReadIDCardService { int port = 2; private string textContent = ""; public IDCard Read() { SamPower(true); Thread.Sleep(500); //MessageBox.Show("sam power:" + GetSamPower()); //ScanPower(true); //MessageBox.Show("scan power:" + GetScanPower()); DeleteFile(); int result=InitComm(port); if (result != 1) { CleanUp(); throw new Exception("初始化" + port + "号串口失败"); } Thread.Sleep(900); result = Authenticate(); if (result != 1) { CleanUp(); throw new Exception("未放身份证或身份证放置不正确,若身份证放置后发生认证错误时,应移走身份证重新放置。"); } result = Read_Content(1); if (result == 0) { CleanUp(); throw new Exception("读卡错误"); } if (result == -1) { CleanUp(); throw new Exception("相片解码错误"); } if (result == -5) { CleanUp(); throw new Exception("软件未授权"); } IDCard data = ParseData(); CleanUp(); return data; } private IDCard ParseData() { //读取文本文件中的数据 textContent = ReadFile(); string name = this.GetName(); //MessageBox.Show("name:"+name); string sex = this.GetSex(); //MessageBox.Show("sex:" + sex); string nationality = this.GetNationality(); //MessageBox.Show("nationality:" + nationality); string birthday = this.GetBirthday(); //MessageBox.Show("birthday:" + birthday); string address = this.GetAddress(); //MessageBox.Show("address:" + address); string cartNo = this.GetCartNo(); //MessageBox.Show("cartNo:" + cartNo); string institution = this.GetInstitution(); //MessageBox.Show("institution:" + institution); string begin_validity = this.GetBegin_validity(); //MessageBox.Show("begin_validity:" + begin_validity); string end_validity = this.GetEnd_validity(); //MessageBox.Show("end_validity:" + end_validity); //读取照片 byte[] photo = ReadImage(); //构造IDCard对象 IDCard data = new IDCard(); data.Name = name; data.Sex = sex; data.Nationality = nationality; data.Birthday = birthday; data.Address = address; data.CartNo = cartNo; data.Institution = institution; data.Begin_validity = begin_validity; data.End_validity = end_validity; data.Photo = photo; return data; } private void CleanUp() { int result = CloseComm(); SamPower(false); //MessageBox.Show("sam power:" + GetSamPower()); //ScanPower(false); // MessageBox.Show("scan power:" + GetScanPower()); if (result != 1) { throw new Exception("关闭" + port + "号串口失败"); } } private byte[] ReadImage() { long length=new FileInfo("/Windows/zp.bmp").Length; byte[] data=new byte[length]; try { StreamReader reader = new StreamReader("/Windows/zp.bmp"); reader.BaseStream.Read(data, 0, (int)length); reader.Close(); } catch (Exception) { MessageBox.Show("读取照片失败"); } return data; } private void DeleteFile() { try { FileInfo file = new FileInfo("/Windows/zp.bmp"); file.Delete(); } catch (Exception) { MessageBox.Show("删除照片失败"); } try { FileInfo file = new FileInfo("/Windows/wz.txt"); file.Delete(); } catch (Exception) { MessageBox.Show("删除文件失败"); } } private string ReadFile() { string result = ""; try { StreamReader reader = new StreamReader("/Windows/wz.txt", Encoding.GetEncoding("Unicode")); result = reader.ReadToEnd(); reader.Close(); } catch (Exception) { MessageBox.Show("读取基本信息失败"); } return result; } //取姓名 private string GetName() { return GetText(0, 15).Trim(); } //取性别 private string GetSex() { string result=""; string text=GetText(15,1); if("9".Equals(text)) result="未说明"; if("1".Equals(text)) result="男"; if("2".Equals(text)) result="女"; if("0".Equals(text)) result="未知"; return result.Trim(); } //取民族 private string GetNationality() { string[] nationality={"汉","蒙古","回","藏","维吾尔","苗","彝","壮","布依", "朝鲜","满","侗","瑶","白","土家","哈尼","哈萨克","傣","黎","傈僳","佤","畲","高山","拉祜", "水","东乡","纳西","景颇","柯尔克孜","土","达斡尔","仫佬","羌","布朗","撒拉","毛南","仡佬", "锡伯","阿昌","普米","塔吉克","怒","乌孜别克","俄罗斯","鄂温克","德昂","保安","裕固","京", "塔塔尔","独龙","鄂伦春","赫哲","门巴","珞巴","基诺"}; int index = int.Parse(GetText(16, 2)); return nationality[index-1]; } //取生日 private string GetBirthday() { string text = GetText(18, 8); string result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); return result; } //取家庭住址 private string GetAddress() { return GetText(26, 35).Trim(); } //取身份证号 private string GetCartNo() { return GetText(61, 18).Trim(); } //取发证机关 private string GetInstitution() { return GetText(79, 15).Trim(); } // 有效期开始 private string GetBegin_validity() { string text = GetText(94, 8); string result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); return result; } //有效期结束 private string GetEnd_validity() { string text = GetText(102, 8); string result = ""; if (!"长期".Equals(text)) { result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); } return result; } //取照片 private string GetText(int startIndex,int length) { return textContent.Substring(startIndex,length); } [DllImport("PowerCtrl.dll", CharSet = CharSet.Unicode)] private static extern int OpenPower(); [DllImport("PowerCtrl.dll", CharSet = CharSet.Unicode)] private static extern void ClosePower(); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int InitComm(int Port); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int CloseComm(); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int Authenticate(); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int Read_Content(int Active); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool SamPower(bool value); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool GetSamPower(); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool ScanPower(bool value); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool GetScanPower(); } }
第二种:
4. IDCardScaner.cs
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; using System.IO; using System.Drawing; namespace ysc { public class IDCardScaner { private AutoScanIDCardForm cardForm; private AutoReadIDCardService auto; private bool run = false; public IDCardScaner(AutoScanIDCardForm cardForm) { this.cardForm = cardForm; this.auto = new AutoReadIDCardService(); } public void End() { this.run = false; auto.Close(); } public void Scan() { auto.Init(); if (run) { return; } object lockThis = new object(); lock (lockThis) { if (run) { return; } run = true; while (run) { try { cardForm.SafeTip("请放卡"); if (auto.Check()) { cardForm.SafeTip("正在读卡"); IDCard card = auto.ReadContent(); UpdateUI(card); cardForm.SafeTip("读卡成功"); } Thread.Sleep(1000); } catch (Exception) { } } } } private void UpdateUI(IDCard card) { cardForm.SafeDisplay(card); } } }
5. AutoReadIDCardService.cs
using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; using System.IO; using System; using System.Text; namespace ysc { class AutoReadIDCardService { private bool run = false; private string textContent = ""; public bool Init() { if (run) { return run; } object lockThis = new object(); lock (lockThis) { if (run) { return run; } int port = 2; SamPower(true); Thread.Sleep(500); int result = InitComm(port); if (result != 1) { Close(); return false; //throw new Exception("初始化2号串口失败"); } run = true; } return run; } public bool Check() { int result = Authenticate(); if (result != 1) { return false; //throw new Exception("未放身份证或身份证放置不正确,若身份证放置后发生认证错误时,应移走身份证重新放置。"); } return true; } public IDCard ReadContent() { //读取之前先删掉以前的数据 DeleteFile(); int result = Read_Content(1); if (result == 0) { return null; //throw new Exception("读卡错误"); } if (result == -1) { return null; //throw new Exception("相片解码错误"); } if (result == -5) { return null; // throw new Exception("软件未授权"); } //读取文本文件中的数据 textContent = ReadFile(); string name = this.GetName(); //MessageBox.Show("name:"+name); string sex = this.GetSex(); //MessageBox.Show("sex:" + sex); string nationality = this.GetNationality(); //MessageBox.Show("nationality:" + nationality); string birthday = this.GetBirthday(); //MessageBox.Show("birthday:" + birthday); string address = this.GetAddress(); //MessageBox.Show("address:" + address); string cartNo = this.GetCartNo(); //MessageBox.Show("cartNo:" + cartNo); string institution = this.GetInstitution(); //MessageBox.Show("institution:" + institution); string begin_validity = this.GetBegin_validity(); //MessageBox.Show("begin_validity:" + begin_validity); string end_validity = this.GetEnd_validity(); //MessageBox.Show("end_validity:" + end_validity); //读取照片 byte[] photo = ReadImage(); //构造IDCard对象 IDCard data = new IDCard(); data.Name = name; data.Sex = sex; data.Nationality = nationality; data.Birthday = birthday; data.Address = address; data.CartNo = cartNo; data.Institution = institution; data.Begin_validity = begin_validity; data.End_validity = end_validity; data.Photo = photo; return data; } public bool Close() { if (!run) { return true; } object lockThis = new object(); lock (lockThis) { if (!run) { return true; } int result = CloseComm(); SamPower(false); if (result != 1) { return false; //throw new Exception("关闭2号串口失败"); } run = false; } return true; } private byte[] ReadImage() { long length=new FileInfo("/Windows/zp.bmp").Length; byte[] data=new byte[length]; try { StreamReader reader = new StreamReader("/Windows/zp.bmp"); reader.BaseStream.Read(data, 0, (int)length); reader.Close(); } catch (Exception) { MessageBox.Show("读取照片失败"); } return data; } private void DeleteFile() { try { FileInfo file = new FileInfo("/Windows/zp.bmp"); file.Delete(); } catch (Exception) { MessageBox.Show("删除照片失败"); } try { FileInfo file = new FileInfo("/Windows/wz.txt"); file.Delete(); } catch (Exception) { MessageBox.Show("删除文件失败"); } } private string ReadFile() { string result = ""; try { StreamReader reader = new StreamReader("/Windows/wz.txt", Encoding.GetEncoding("Unicode")); result = reader.ReadToEnd(); reader.Close(); } catch (Exception) { MessageBox.Show("读取基本信息失败"); } return result; } //取姓名 private string GetName() { return GetText(0, 15).Trim(); } //取性别 private string GetSex() { string result=""; string text=GetText(15,1); if("9".Equals(text)) result="未说明"; if("1".Equals(text)) result="男"; if("2".Equals(text)) result="女"; if("0".Equals(text)) result="未知"; return result.Trim(); } //取民族 private string GetNationality() { string[] nationality={"汉","蒙古","回","藏","维吾尔","苗","彝","壮","布依", "朝鲜","满","侗","瑶","白","土家","哈尼","哈萨克","傣","黎","傈僳","佤","畲","高山","拉祜", "水","东乡","纳西","景颇","柯尔克孜","土","达斡尔","仫佬","羌","布朗","撒拉","毛南","仡佬", "锡伯","阿昌","普米","塔吉克","怒","乌孜别克","俄罗斯","鄂温克","德昂","保安","裕固","京", "塔塔尔","独龙","鄂伦春","赫哲","门巴","珞巴","基诺"}; int index = int.Parse(GetText(16, 2)); return nationality[index-1]; } //取生日 private string GetBirthday() { string text = GetText(18, 8); string result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); return result; } //取家庭住址 private string GetAddress() { return GetText(26, 35).Trim(); } //取身份证号 private string GetCartNo() { return GetText(61, 18).Trim(); } //取发证机关 private string GetInstitution() { return GetText(79, 15).Trim(); } // 有效期开始 private string GetBegin_validity() { string text = GetText(94, 8); string result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); return result; } //有效期结束 private string GetEnd_validity() { string text = GetText(102, 8); string result = ""; if (!"长期".Equals(text)) { result = text.Substring(0, 4) + '-' + text.Substring(4, 2) + '-' + text.Substring(6, 2); } return result; } //取照片 private string GetText(int startIndex,int length) { return textContent.Substring(startIndex,length); } [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int InitComm(int Port); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int CloseComm(); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int Authenticate(); [DllImport("termb.dll", CharSet = CharSet.Unicode)] private static extern int Read_Content(int Active); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool SamPower(bool value); [DllImport("HWControl.dll", CharSet = CharSet.Unicode)] private static extern bool GetSamPower(); } }
6. AutoScanIDCardForm.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace ysc { public partial class AutoScanIDCardForm : Form { private IDCard card = null; private static IDCardScaner scaner = null; private static Thread scanThread = null; private string tipText = ""; private Form form = null; public AutoScanIDCardForm(Form form) { InitializeComponent(); this.form = form; //构造IDCard扫描器 if (scaner == null && scanThread==null) { StartAutoScan(); } } private void StartAutoScan() { scaner = new IDCardScaner(this); scanThread = new Thread(new ThreadStart(scaner.Scan)); scanThread.IsBackground = true; //启动IDCard扫描线程 scanThread.Start(); this.ReadIDCard.Enabled = false; } public void Clear() { this.Photo.Image = null; this.UserName.Text = ""; this.Sex.Text = ""; this.Birthday.Text = ""; this.Address.Text = ""; this.Nationality.Text = ""; this.CartNo.Text = ""; this.Institution.Text = ""; this.Validity.Text = ""; this.tip.Text = ""; } public void SafeTip(string tip) { this.tipText = tip; this.Invoke(new EventHandler(DisplayTip)); } private void DisplayTip(object o, EventArgs e) { try { this.tip.Text = tipText; } catch (Exception) { } } public void SafeDisplay(IDCard card) { this.card = card; this.Invoke(new EventHandler(DisplayCard)); } private void DisplayCard(object o, EventArgs e) { try { Display(); } catch (Exception) { } } private void Display() { MemoryStream ms = new MemoryStream(card.Photo);//将字节数组存入到二进制流中 ms.Position = 0; Bitmap img = new Bitmap(ms); ms.Close(); this.UserName.Text = card.Name; this.Photo.Image = img; this.Sex.Text = card.Sex; this.Birthday.Text = card.Birthday; this.Address.Text = card.Address; this.Nationality.Text = card.Nationality; this.CartNo.Text = card.CartNo; this.Institution.Text = card.Institution; this.Validity.Text = card.Begin_validity+" 至 "+card.End_validity; } private void Return_Click(object sender, EventArgs e) { form.Show(); this.Hide(); EndAutoScan(); this.Dispose(); } private void ReadIDCard_Click(object sender, EventArgs e) { Clear(); this.tip.Text = "正在读卡"; this.ReadIDCard.Enabled = false; this.Refresh(); try { ReadIDCardService service = new ReadIDCardService(); card = service.Read(); tip.Text = "读卡成功"; Display(); } catch (Exception ex) { tip.Text = "读卡失败"; MessageBox.Show(ex.Message); } this.ReadIDCard.Enabled = true; } private void AutoScan_CheckStateChanged(object sender, EventArgs e) { if (this.AutoScan.Checked) { StartAutoScan(); } else { EndAutoScan(); this.ReadIDCard.Enabled = true; } } private void EndAutoScan() { if (scaner != null) { scaner.End(); scanThread = null; scaner = null; } } } }
相关文章推荐
- The method getServletContext() is undefined for the type HttpServletRequest
- 一次完整的HTTP事务过程分析
- The method getDispatcherType() is undefined for the type HttpServletRequest
- TCP协议中的三次握手和四次挥手
- 多台虚拟机的ip设置及相互访问
- 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView
- HTTP Session学习
- java网络编程----TCP UDP
- Linux网络编程经典书籍推荐
- 神经网络识别车牌字符
- 【网络流】 HDOJ 4289 Control
- vs2012引入新lib-----附cocos2dx3.2 http通信lib库添加
- vs2012引入新lib-----附cocos2dx3.2 http通信lib库添加 分类: cocos2d安装设置 2015-07-07 09:08 13人阅读 评论(0) 收藏
- STM32W108无线射频模块802.15.4/ZigBee协议栈
- 深入了解HTTP协议、HTTP协议原则
- ajax核心技术1---XMLHttpRequset对象的使用
- ajax核心技术1---XMLHttpRequset对象的使用
- iOS上的http请求:get、post以及同步、异步
- 计算机网络原理一
- VIEWGOOD(远古)校园网络电视台建设整体解决方案