读取身份证上的数据,将照片保存到指定路径
环境- jdk1.8 32位
- window 11
- Maven
- 乱找的dll,找了一堆,终于发现可以用的了。官方开发包的dll只能读出信息,读不出图片,求赐教
- 所有阅读器采用的是相同的内部硬件,厂商只是套了个外壳
- 代码看上去很多,大部分都是类似的方法,如果只获取身份证其中一项信息,代码会很少
- 官方下载的说明文档里说只要调用接口中CVR_Read_Content和CVR_Read_FPContent方法就可以在对应的位置生成两个文件一个是txt文档记录身份证上的信息,一个是bmp身份证图片,这套dll只能生成在默认的路径C:Users用户AppDataLocalTempchinaidcard
- 用官方提供的开发包,死活弄不出图片,在网上找了其他的安装包,把下图中3个dll文件复制到程序中,居然能用了。jdk64位的还是弄不出图片。
- 要不是我水平差,要不就是华视故意的,官网提供的dll文件居然不能用,如果是故意的那这公司可真的恶心。
- 感觉Termb和WltRS这两个dll文件是配套的,换其中一个,程序可能就跑不起来。
- 目前网上可以找到的读取身份证信息的程序都是用的32位dll,没找到64位的dll。
- 使用官方开发包内的64位dll可以读取身份证的信息,就是无法读取图片。感觉还是dll的事情,找了两天也没找到可用的。无论问哪儿要开发包,客服都要让我提供购买的订单号,上级单位发下来的设备,我去哪儿要订单号。
- 对华视公司的看法,小气、没格局。要个最新的开发包非要个订单号,给产品照片都不行,产品的开发包也藏着掖着,官网提供的开发包里面写的是乱七八糟,特指java的demo,其他语言我看不懂。既然想卖产品那应该是应用的越广泛卖的越好,别人用你的设备做更广泛的应用,设备就能卖的更多,所以要你开发包,你应该高兴才对。看看自己做的程序,做了十几年了,界面还是难看的要死。
依赖
2. 拷贝dll文件到resourcesnet.java.dev.jna jna 5.11.0
下载
3. 接口映射Termb.dll中的方法使用jna连接dll文件,接口中所有方法都是Termb中的方法
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
public interface CardLibrary extends Library {
// 参数1:只写名称,不写后缀,符合jni规范,可跨平台操作,linux中后缀为so
CardLibrary INSTANCE = Native.load("Termb", CardLibrary.class);
int CVR_InitComm(int Port);
int CVR_CloseComm();
int CVR_Authenticate();
int CVR_Read_Content(int active);
int CVR_Read_FPContent();
int GetPeopleName(byte[] strTmp, byte[] strLen);
int GetPeopleSex(byte[] strTmp, byte[] strLen);
int GetPeopleNation(byte[] strTmp, byte[] strLen);
int GetPeopleBirthday(byte[] strTmp, byte[] strLen);
int GetPeopleAddress(byte[] strTmp, byte[] strLen);
int GetPeopleIDCode(byte[] strTmp, byte[] strLen);
int GetDepartment(byte[] strTmp, byte[] strLen);
int GetStartDate(byte[] strTmp, byte[] strLen);
int GetEndDate(byte[] strTmp, byte[] strLen);
int GetBMPData(byte[] strTmp, IntByReference strLen);
int GetJpgData(byte[] strTmp, IntByReference strLen);
int CVR_GetSAMId(byte[] strTmp, byte[] strLen);
}
4. 调用接口中的方法
import com.sun.jna.ptr.IntByReference;
import java.io.FileOutputStream;
public class Card {
public static String GetPeopleName() throws Exception {
try {
byte[] a = new byte[30];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleName(a, b) == 1 ? new String(a,"gb2312").trim() : "";
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public static String GetPeopleSex() throws Exception {
try {
byte[] a = new byte[2];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleSex(a, b) == 1 ? new String(a,"gb2312").trim() : "";
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public static String GetPeopleNation() throws Exception {
try {
byte[] a = new byte[20];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleNation(a, b) == 1 ? new String(a,"gb2312").trim() : "";
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public static String GetPeopleBirthday() throws Exception {
try
{
byte[] a = new byte[16];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleBirthday(a,b) == 1 ? new String(a).trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String GetPeopleAddress() throws Exception {
try
{
byte[] a = new byte[70];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleAddress(a,b) == 1 ? new String(a,"gb2312").trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String GetPeopleIDCode() throws Exception {
try
{
byte[] a = new byte[36];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetPeopleIDCode(a,b) == 1 ? new String(a).trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String GetDepartment() throws Exception {
try
{
byte[] a = new byte[30];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetDepartment(a,b) == 1 ? new String(a,"gb2312").trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String GetStartDate() throws Exception {
try
{
byte[] a = new byte[16];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetStartDate(a,b) == 1 ? new String(a,"gb2312").trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String GetEndDate() throws Exception {
try
{
byte[] a = new byte[16];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.GetEndDate(a,b) == 1 ? new String(a,"gb2312").trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static String CVR_GetSAMId() throws Exception {
try
{
byte[] a = new byte[4*10];
byte[] b = new byte[4];
return CardLibrary.INSTANCE.CVR_GetSAMId(a,b) == 1 ? new String(a).trim() : "";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "";
}
public static boolean GetBMPData() throws Exception {
try
{
byte[] a = new byte[38862];
IntByReference len = new IntByReference();
FileOutputStream fos = new FileOutputStream("D:\123.bmp");
if (CardLibrary.INSTANCE.GetBMPData(a,len) == 1){
fos.write(a,0,len.getValue());
fos.flush();
fos.close();
return true;
}else return false;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return false;
}
public static boolean GetJpgData() throws Exception {
try
{ // 返回固定的38862字节,使用看图软件另存后5000字节左右,
// 说明文档中没有明确说明返回的字节数。
byte[] a = new byte[38862];
IntByReference len = new IntByReference();
FileOutputStream fos = new FileOutputStream("D:\123.jpg");
if (CardLibrary.INSTANCE.GetJpgData(a,len) == 1){
System.out.println("len = " + len);
fos.write(a,0,len.getValue());
fos.flush();
fos.close();
return true;
}else return false;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return false;
}
}
5. 连接、认证、获取信息、关闭 阅读器
public class CardMain {
public static void main(String[] args) {
// 测试所有端口,打开连接
int init = 0;
for (int i = 1001; i < 1016; i++) {
init = CardLibrary.INSTANCE.CVR_InitComm(i);
if(init == 1) {
System.out.println("初始化硬件成功");
break;
}
}
// 验证初始化信息
if (init != 1) {
System.out.println("初始化硬件失败");
return;
}
// 卡认证
int auth = 0;
while (true) {
auth = CardLibrary.INSTANCE.CVR_Authenticate();
if (auth == 1){
System.out.println("读卡器和卡片之间的合法身份确认:身份认证成功");
}
if (auth != 1) {
System.out.println("读卡器和卡片之间的合法身份确认:身份认证失败");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
// 读取卡信息,认证成功后文本信息和照片的默认保存路径
//C:Users用户AppDataLocalTempchinaidcard
int read = CardLibrary.INSTANCE.CVR_Read_Content(1); //默认路径文件:config.ini fp.dat wz.txt xp.bmp xp.jpg zp.bmp xp.wlt
// 使用手册上说:执行后会在运行目录下生成以下文件信息。使用这套dll还是会生成到默认路径
// int read = CardLibrary.INSTANCE.CVR_Read_FPContent(); //默认路径文件:config.ini fp.dat wz.txt xp.bmp xp.jpg zp.bmp
if (read != 1) {
System.out.println("获取信息失败");
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("成功获取信息");
try {
System.err.println(Card.GetPeopleName());
System.err.println(Card.GetPeopleSex());
System.err.println(Card.GetPeopleNation());
System.err.println(Card.GetPeopleBirthday());
System.err.println(Card.GetPeopleAddress());
System.err.println(Card.GetPeopleIDCode());
System.err.println(Card.GetDepartment());
System.err.println(Card.GetStartDate());
System.err.println(Card.GetEndDate());
System.err.println(Card.CVR_GetSAMId());
if (Card.GetBMPData())
System.err.println("bmp图片获取成功");
if (Card.GetJpgData())
System.err.println("jpg图片获取成功");
} catch (Exception e) {
e.printStackTrace();
}
}
int i = CardLibrary.INSTANCE.CVR_CloseComm();
if (i == 1){
System.out.println("关闭连接成功");
}
}
}



