业务需求
在学员录入的时候添加一个读取身份证填充对应信息的功能,但遇到如下问题:
- 官方提供的demo读取身份证使用了com组件和activeX控件的技术,并且只支持IE,我们的前端项目没有兼容IE
- 技术选型选择了使用.net core 3.1,但网上的二次开发demo都是winform程序开发
解决方案
开发对应的webapi注册win服务在用户的本地运行作为读取身份证信息的中转程序,前端调用对应的接口获取读取的信息。
开发demo 编写调用二次开发dll
编写调用二次开发dll接口工具类ReadIdCardHelper(直接调用使用)
public class ReadIdCardHelper
{
#region DllImport
[DllImport("idCardDll/sdtapi.dll")]
private static extern int InitComm(int iPort);//初始化
[DllImport("idCardDll/sdtapi.dll")]
private static extern int CloseComm();//关闭端口
[DllImport("idCardDll/sdtapi.dll")]
private static extern int Authenticate();//卡认证
[DllImport("idCardDll/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("idCardDll/sdtapi.dll")]//读取数据,推荐使用
private static extern int Routon_BeepLED(bool BeepON, bool LEDON, int duration);
#endregion
static public void readCard(ref IdCardMessageDto idCardMessage)
{
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 = InitComm(1001);
if (intOpenRet != 1)
{
Console.WriteLine("阅读机具未连接");
Routon_BeepLED(false,false,2000);
return;
}
//卡认证
int intReadRet = Authenticate();
if (intReadRet != 1)
{
Console.WriteLine("卡认证失败");
Routon_BeepLED(false, false, 2000);
CloseComm();
return;
}
//读卡
int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
if (intReadBaseInfosRet != 1)
{
Console.WriteLine("读卡失败");
Routon_BeepLED(false, false, 2000);
CloseComm();
return;
}
idCardMessage = new IdCardMessageDto();
idCardMessage.Code = Code.ToString().Trim();
idCardMessage.Name = Name.ToString().Trim();
idCardMessage.Gender = Gender.ToString().Trim();
idCardMessage.Folk = Folk.ToString().Trim();
idCardMessage.BirthDay = BirthDay.ToString().Trim();
idCardMessage.Address = Address.ToString().Trim();
idCardMessage.Agency = Agency.ToString().Trim();
idCardMessage.ExpireStart = ExpireStart.ToString().Trim();
idCardMessage.ExpireEnd = ExpireEnd.ToString().Trim();
idCardMessage.photo = System.IO.Directory.GetCurrentDirectory() + "\\photo.bmp";
Routon_BeepLED(true, true, 2000);
CloseComm();
}
DLL文件必须位于程序当前目录或系统定义的查询路径中(即:系统环境变量中Path所设置的路径)
DLLImport会按照顺序去查找DLL文件(程序当前目录>System32目录>环境变量Path所设置路径)。
可能遇到的坑
程序生成的位数不对【试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)】二次开发包是在32位系统下面开发的,在64位系统上面开发编译的时候需要将生成的目标平台设为X86
demo下载
链接: .net core 3.1精伦身份证读卡器IDR210-2(部标版)二次开发