private void Reader_OnReadCardCompleted(object sender, ReadCardCompletedEventArgs e) { txt.Dispatcher.BeginInvoke(new Action(() => { txt.Text = e.Name + e.IDC; })); reader.Stop(); reader.Close(); }
private void Retrieve(string content, ReadCardCompletedEventArgs e) { // 提取姓名 e.Name = content.Substring(0, 15).Trim(); // 提取性别 e.Gender = (content[15] == '1') ? "男" : "女"; // 提取民族编码和民族名称 e.EthnicCode = content.Substring(16, 2); e.EthnicName = Ethnics.Contains(e.EthnicCode) ? (string)Ethnics[e.EthnicCode] : string.Empty; // 获取出生日期 e.Birth = DateTime.ParseExact(content.Substring(18, 8), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd"); // 获取住址 e.Address = content.Substring(26, 35).Trim(); // 获取身份证编码 e.IDC = content.Substring(61, 18).Trim(); // 获取签发机关 e.IssuingAuthority = content.Substring(79, 15).Trim(); // 获取有效期限开始日期 e.ValidDateStart = DateTime.ParseExact(content.Substring(94, 8), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy.MM.dd"); // 获取有限期限截止日期 if (content.Substring(102, 2).Equals("长期")) { e.ValidDateEnd = "长期"; } else { e.ValidDateEnd = DateTime.ParseExact(content.Substring(102, 8), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy.MM.dd"); } }
/// <summary> /// 线程池回调方法 /// </summary> /// <param name="state">回调方法要使用的信息对象</param> private void CardReaderCallback(Object state) { InnerIsAlive = true; try { // 读取身份证 while (true) { if (shouldStop) { break; } // 等待外部同步信号 if (ExternalSyncEvent != null) { ExternalSyncEvent.WaitOne(); if (shouldStop) { break; } } // 验证卡 if (Authenticate() >= 0) { // 读基本信息 if (ReadContent() >= 0) { // 存储身份证信息 ReadCardCompletedEventArgs Args = new ReadCardCompletedEventArgs(); if (File.Exists(CHMsgFileName)) { // 文字信息 using (StreamReader sr = new StreamReader(CHMsgFileName, Encoding.Unicode, true)) { // 提取数据 Retrieve(sr.ReadToEnd(), Args); } } // 照片信息 if (((IDCardBiometrics)state & IDCardBiometrics.Photo) == IDCardBiometrics.Photo) { if (File.Exists(PhotoFileName)) { using (FileStream fs = new FileStream(PhotoFileName, FileMode.Open, FileAccess.Read)) { Args.Photo = new byte[fs.Length]; fs.Read(Args.Photo, 0, (int)fs.Length); } } } // 关闭读卡操作,等待下次信号 if (ExternalSyncEvent != null) { ExternalSyncEvent.Reset(); if (shouldStop) { break; } } // 处理读卡结束事件 if (OnReadCardCompleted != null) { OnReadCardCompleted(this, Args); } } } } // End While } catch (Exception exception) { // 阻止异常抛出 } finally { InnerIsAlive = false; } }