最近接手一个任务,在nc人力资源模块通过精伦的身份证读卡器获取人员信息;
查阅了网上的资料,尝试了多种方式,最后采取了以下这种=调用读卡器dll
第一步: 在工程中导入 jna.jar 这个包。
第二步: 将相关dll放到系统目录,注意64位和32位有区别的
第三步: java代码
package nc.itf.idrControl;
import com.sun.jna.*;
public class test {
interface CLibrary extends Library {
CLibrary sdtapi = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "sdtapi" : "c"),CLibrary.class);
int InitComm(int port);
int Authenticate();
int ReadBaseInfosPhoto(byte[] Name,byte[] Gender,byte[] Folk,byte[] BirthDay,byte[] Code,byte[] Address,byte[] Agency,byte[] ExpireStart,byte[] ExpireEnd,String Dir);
}
public static void main(String[] args) throws Exception {
byte sid[] = new byte[37];
byte name[] = new byte[31];
byte sex[] = new byte[3];
byte folk[] = new byte[10];
byte birth[] = new byte[9];
byte code[] = new byte[19];
byte add[] = new byte[71];
byte agency[] = new byte[31];
byte expirestart[] = new byte[9];
byte expireend[] = new byte[9];
//初始化端口
if (CLibrary.sdtapi.InitComm(1001) == 1){
//卡认证
int ret = CLibrary.sdtapi.Authenticate();
if(ret == 1){
//读取卡信息
ret = CLibrary.sdtapi.ReadBaseInfosPhoto(name,sex,folk,birth,code,add,agency,expirestart,expireend,"D:\\Project\\20180105zhongnan\\nchome_bk\\shenfenzheng");
if(ret == 1){
System.out.println(new String(name,"GBK"));
System.out.println(new String(sex));
System.out.println(new String(folk));
System.out.println(new String(birth));
System.out.println(new String(code));
System.out.println(new String(add));
System.out.println(new String(agency));
System.out.println(new String(expirestart));
System.out.println(new String(expireend));
}
}
}else{
System.out.println("读取失败!");
}
}
}
第四步: 总结
相关dll,与提供读卡器的厂商沟通,他们会提供dll和demo;当然没有的话,网上大多数也能找到,我的就是在CSDL花费了5个大洋下的....
后续:在提交到服务器上运行后,客户使用UClient登陆客户端访问功能时出现一个问题:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError:
Could not initialize class nc.ui.hi.psndoc.action.CardImportActionReadCard$CLibrary
at nc.ui.hi.psndoc.action.CardImportActionReadCard.getPsndocVO(CardImportActionReadCard.java:80)
at nc.ui.hi.psndoc.action.CardImportAction.getDefaultValue(CardImportAction.java:210)
at nc.ui.hi.psndoc.action.CardImportAction.doAction(CardImportAction.java:88)
at nc.ui.uif2.NCAction.actionPerformed(NCAction.java:85)
at nc.funcnode.ui.action.ActionDelegate.actionPerformed(ActionDelegate.java:296)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at nc.ui.plaf.basic.UIMenuItemUI.doClick(UIMenuItemUI.java:1191)
at nc.ui.plaf.basic.UIMenuItemUI$MouseInputHandler.mouseReleased(UIMenuItemUI.java:1016)
at java.awt.Component.processMouseEvent(Unknown Source)
多番测试找出原因是因为使用64位jdk运行客户端无法解析以上代码,在UClient设置为32位jdk运行;问题的具体原因没有进一步研究,猜测是该jna类库不支持64位jdk
========再次遇到了这类问题,原因是dll有32位与64位之分,即32位dll需要32jdk解析。如果是java调用,可以通过vs查询dll的版本:
================= 已经上传了demo(资源未审批通过。。。。。有需要的留言revin1220@sina.com吧)