二代身份证读取-微调版
2011-12-29 14:39 GhostHorse 阅读(6639) 评论(27) 编辑 收藏 举报说起微调其实就是对 上一篇文章 今天意外和身份证结缘 中提到的代码码放和调用一些巧妙的用法(至少小鬼觉得很有意思很巧妙)。
为什么有必要在写一篇 写的理由千千万;不写的理由万万千。对于小鬼来说就是:我吃饱了,撑着了,该找点事做了。于是神情恍惚,目光呆滞的敲着键盘,陶醉的望着跳动的光标,嘴角上扬。
IDCardReader
#region Using
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
#endregion
/// <summary>
/// 用于获取二代身份证的基本信息(包括图片)
/// 调用时确保 由厂商提供的API的动态链接库等及lic文件
/// 设备 精伦电子公司的iDR200 或 iDR210 型号测试通过
/// 获取的头像图片在应用程序目录下“photo.bmp”文件
/// </summary>
public class IDCardReader
{
#region DllImport
[DllImport("sdtapi.dll")]
private static extern int InitComm(int iPort);//初始化
[DllImport("sdtapi.dll")]
private static extern int CloseComm();//关闭端口
[DllImport("sdtapi.dll")]
private static extern int Authenticate();//卡认证
[DllImport("sdtapi.dll")]//读取数据,推荐使用
private static extern int ReadBaseInfos(
StringBuilder Name,
StringBuilder Gender,
StringBuilder Folk,
StringBuilder BirthDay,
StringBuilder Code,
StringBuilder Address,
StringBuilder Agency,
StringBuilder ExpireStart,
StringBuilder ExpireEnd);
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);//用来大吼一声
#endregion
public event Action<ReadCardCompletedEventArgs> ReadCardCompleted;
public event Action<string> OnError;
/// <summary>
/// 获取身份证信息.
/// 如果成功就促发ReadCardCompleted事件,事件参数包含相关信息;
/// 否则促发OnError事件.
/// </summary>
public void ReadCard()
{
StringBuilder Name = new StringBuilder(31);
StringBuilder Gender = new StringBuilder(3);
StringBuilder Folk = new StringBuilder(10);
StringBuilder BirthDay = new StringBuilder(9);
StringBuilder Code = new StringBuilder(19);
StringBuilder Address = new StringBuilder(71);
StringBuilder Agency = new StringBuilder(31);
StringBuilder ExpireStart = new StringBuilder(9);
StringBuilder ExpireEnd = new StringBuilder(9);
//打开端口
int intOpenRet = 0;
for (int i = 1001; i < 1004; i++)
{
intOpenRet = InitComm(i);
if (intOpenRet == 1)
break;
}
if (intOpenRet != 1)
{
if (OnError != null) OnError("阅读机具未连接");
return;
}
//卡认证
int intReadRet = Authenticate();
if (intReadRet != 1)
{
if (OnError != null) OnError("卡认证失败");
CloseComm();
return;
}
//ReadBaseInfos(推荐使用)
int intReadBaseInfosRet = ReadBaseInfos(
Name, Gender, Folk, BirthDay,
Code, Address, Agency,
ExpireStart, ExpireEnd);
if (intReadBaseInfosRet == 0)
{
if (OnError != null) OnError("读卡错误");
CloseComm();
return;
}
Beep(2047, 200);
ReadCardCompletedEventArgs args = new ReadCardCompletedEventArgs();
args.Code = Code.ToString().Trim();
args.Name = Name.ToString().Trim();
args.Gender = Gender.ToString().Trim();
args.Folk = Folk.ToString().Trim();
args.BirthDay = BirthDay.ToString().Trim();
args.Address = Address.ToString().Trim();
args.Agency = Agency.ToString().Trim();
args.ExpireStart = ExpireStart.ToString().Trim();
args.ExpireEnd = ExpireEnd.ToString().Trim();
args.PhotoPath = Directory.GetCurrentDirectory() + "\\photo.bmp";
CloseComm();
if (ReadCardCompleted != null)
{
this.ReadCardCompleted(args);
}
}
}
/// <summary>
/// ReadCardCompleted事件参数类
/// 包含身份证基本信息和图片
/// </summary>
public class ReadCardCompletedEventArgs : EventArgs
{
public String Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public String Gender { get; set; }
/// <summary>
/// 民族
/// </summary>
public String Folk { get; set; }
public String BirthDay { get; set; }
/// <summary>
/// 身份证号
/// </summary>
public String Code { get; set; }
public String Address { get; set; }
/// <summary>
/// 发证机关
/// </summary>
public String Agency { get; set; }
public String ExpireStart { get; set; }
public String ExpireEnd { get; set; }
public String PhotoPath { get; set; }
}
代码没有什么改动 就换了一下名字 让编程生活体验更好一点。
当时为什么这样写? 这个问题是问这么写有什么好处。现阶段我只能说习惯这样写。
对了,模模糊糊感觉为了好异步处理这个类的实例。
为什么要处理 异步处理这个类的实例? 因为外部设备运行有一个响应时间,这个响应时间有长有短,不易控制,还容易界面无法响应。
这样界面就感觉卡卡的,用户体验也不好。这样的软件还容易被淘汰掉。得妥善解决这一问题!
异步调用是关键。
reader.Value.ReadCardCompleted += (args) =>
{
this.txtAddress.Text = args.Address;
this.txtAgency.Text = args.Agency;
this.txtBirthDay.Text = args.BirthDay;
this.txtCode.Text = args.Code;
this.txtExpireEnd.Text = args.ExpireEnd;
this.txtExpireStart.Text = args.ExpireStart;
this.txtFolk.Text = args.Folk;
this.txtGender.Text = args.Gender;
this.txtName.Text = args.Name;
this.picboxSignle.LoadAsync(args.PhotoPath);
};
//reader.Value.ReadCard();
Action delegateReader = () => reader.Value.ReadCard();
delegateReader.BeginInvoke(null, null);
这里用到了委托的异步调用, 明显改善了程序的性能和用户体验。
这里只是写了一种情况 当然其他具体情况 请看官们斟酌了。
namespace ReadCard
{
#region Using
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
#endregion
/// <summary>
/// 用于获取二代身份证的基本信息(包括图片)
/// 调用时确保 由厂商提供的API的动态链接库等及lic文件
/// 设备 精伦电子公司的iDR200 或 iDR210 型号测试通过
/// 获取的头像图片在应用程序目录下“photo.bmp”文件
/// </summary>
public class IDCardReader
{
#region DllImport
[DllImport("sdtapi.dll")]
private static extern int InitComm(int iPort);//初始化
[DllImport("sdtapi.dll")]
private static extern int CloseComm();//关闭端口
[DllImport("sdtapi.dll")]
private static extern int Authenticate();//卡认证
[DllImport("sdtapi.dll")]//读取数据,推荐使用
private static extern int ReadBaseInfos(
StringBuilder Name,
StringBuilder Gender,
StringBuilder Folk,
StringBuilder BirthDay,
StringBuilder Code,
StringBuilder Address,
StringBuilder Agency,
StringBuilder ExpireStart,
StringBuilder ExpireEnd);
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);//用来大吼一声
#endregion
public event Action<ReadCardCompletedEventArgs> ReadCardCompleted;
public event Action<string> OnError;
#region 参考代码
//enum ERetReadBaseInfos
//{
// 正确 = 1,
// 读卡错误 = 0,
// 没有最新住址信息 = 2,
// 相片解码错误 = -1,
// wlt文件后缀错误 = -2,
// wlt文件打开错误 = -3,
// wlt文件格式错误 = -4,
// 软件未授权 = -5,
// 无效参数 = -11,
//}
#endregion
/// <summary>
/// 获取身份证信息.
/// 如果成功就促发ReadCardCompleted事件,事件参数包含相关信息;
/// 否则促发OnError事件.
/// </summary>
public void ReadCard()
{
StringBuilder Name = new StringBuilder(31);
StringBuilder Gender = new StringBuilder(3);
StringBuilder Folk = new StringBuilder(10);
StringBuilder BirthDay = new StringBuilder(9);
StringBuilder Code = new StringBuilder(19);
StringBuilder Address = new StringBuilder(71);
StringBuilder Agency = new StringBuilder(31);
StringBuilder ExpireStart = new StringBuilder(9);
StringBuilder ExpireEnd = new StringBuilder(9);
//打开端口
int intOpenRet = 0;
for (int i = 1001; i < 1004; i++)
{
intOpenRet = InitComm(i);
if (intOpenRet == 1)
break;
}
if (intOpenRet != 1)
{
if (OnError != null) OnError("阅读机具未连接");
return;
}
//卡认证
int intReadRet = Authenticate();
if (intReadRet != 1)
{
if (OnError != null) OnError("卡认证失败");
CloseComm();
return;
}
//ReadBaseInfos(推荐使用)
int intReadBaseInfosRet = ReadBaseInfos(
Name, Gender, Folk, BirthDay,
Code, Address, Agency,
ExpireStart, ExpireEnd);
if (intReadBaseInfosRet == 0)
{
if (OnError != null) OnError("读卡错误");
CloseComm();
return;
}
Beep(2047, 200);
ReadCardCompletedEventArgs args = new ReadCardCompletedEventArgs();
args.Code = Code.ToString().Trim();
args.Name = Name.ToString().Trim();
args.Gender = Gender.ToString().Trim();
args.Folk = Folk.ToString().Trim();
args.BirthDay = BirthDay.ToString().Trim();
args.Address = Address.ToString().Trim();
args.Agency = Agency.ToString().Trim();
args.ExpireStart = ExpireStart.ToString().Trim();
args.ExpireEnd = ExpireEnd.ToString().Trim();
args.PhotoPath = Directory.GetCurrentDirectory() + "\\photo.bmp";
CloseComm();
if (ReadCardCompleted != null)
{
this.ReadCardCompleted(args);
}
}
}
/// <summary>
/// ReadCardCompleted事件参数类
/// 包含身份证基本信息和图片
/// </summary>
public class ReadCardCompletedEventArgs : EventArgs
{
public String Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public String Gender { get; set; }
/// <summary>
/// 民族
/// </summary>
public String Folk { get; set; }
public String BirthDay { get; set; }
/// <summary>
/// 身份证号
/// </summary>
public String Code { get; set; }
public String Address { get; set; }
/// <summary>
/// 发证机关
/// </summary>
public String Agency { get; set; }
public String ExpireStart { get; set; }
public String ExpireEnd { get; set; }
public String PhotoPath { get; set; }
}
}