由于项目需要,使用读卡机,C++没学好,一些动态库使用起来很困难,在网上搜了好久发现JNative是一个很好的选择,简单,方便,只需要简单的几步就能很好调用动态库,下面是我写一段测试代码:
- package JNative;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import org.xvolks.jnative.JNative;
- import org.xvolks.jnative.Type;
- import org.xvolks.jnative.exceptions.NativeException;
- import org.xvolks.jnative.pointers.Pointer;
- import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
- public class T2 {
- /**
- * @param args
- * @throws NativeException
- * @throws IllegalAccessException
- * @throws IOException
- */
- public static void main(String[] args) throws NativeException, IllegalAccessException, IOException {
- T2 t=new T2();
- t.Init();
- t.getName();
- //======CVR_ReadBaseMsg==================
- JNative n8 = new JNative("termb.dll", "CVR_ReadBaseMsg");
- Pointer pointer11 = new Pointer(MemoryBlockFactory.createMemoryBlock(1000));
- Pointer pointer12 = new Pointer(MemoryBlockFactory.createMemoryBlock(256));
- Pointer pointer13 = new Pointer(MemoryBlockFactory.createMemoryBlock(1024*1000));
- Pointer pointer14 = new Pointer(MemoryBlockFactory.createMemoryBlock(1024));
- int i=0;
- n8.setParameter(i++, pointer11);
- n8.setParameter(i++, pointer12);
- n8.setParameter(i++, pointer13);
- n8.setParameter(i++, pointer14);
- n8.setParameter(i++, 4);
- n8.setRetVal(Type.INT);
- n8.invoke();
- String msg=pointer11.getAsString();
- StringTokenizer st=new StringTokenizer(msg);
- /*while(st.hasMoreElements()){
- System.out.println(st.nextElement());
- }*/
- t.Close();
- }
- /**
- * 分配内存,并返回指针
- */
- public static Pointer createPointer() throws NativeException {
- Pointer pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
- return pointer;
- }
- /**
- * 内存大小
- */
- public static int getSizeOf(){
- return 2 * 2;
- }
- public String getName() throws NativeException, IllegalAccessException{
- //读姓名
- JNative n4 = new JNative("termb.dll", "GetPeopleName");
- Pointer pointer3 = new Pointer(MemoryBlockFactory.createMemoryBlock(10));
- Pointer pointer4 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n4.setParameter(0, pointer3);
- n4.setParameter(1,pointer4);
- n4.setRetVal(Type.INT);
- n4.invoke();
- String name=pointer3.getAsString();
- if(n4.getRetValAsInt()==1){
- //System.out.println(n4.getRetVal());
- System.out.println("读取完毕!");
- }
- //System.out.println("返回值:"+n4.getRetValAsInt());
- System.out.println("姓名是--长度:"+name.length()+" 姓名--"+name);
- return name;
- }
- public String getSex() throws NativeException, IllegalAccessException{
- //读性别
- JNative n2 = new JNative("termb.dll", "GetPeopleSex");
- Pointer pointer1 = new Pointer(MemoryBlockFactory.createMemoryBlock(8));
- Pointer pointer2 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n2.setParameter(0, pointer1);
- n2.setParameter(1,pointer2);
- n2.setRetVal(Type.INT);
- n2.invoke();
- String sex=pointer1.getAsString();
- if(n2.getRetValAsInt()==1){
- System.out.println(n2.getRetVal());
- System.out.println("读取完毕!");
- }
- System.out.println("返回值:"+n2.getRetValAsInt());
- System.out.println("性别是--长度:"+sex.length()+" 性别--"+sex);
- return sex;
- }
- public String getIDNo() throws NativeException, IllegalAccessException{
- //读身份证号
- JNative n5 = new JNative("termb.dll", "GetPeopleIDCode");
- Pointer pointer5 = new Pointer(MemoryBlockFactory.createMemoryBlock(18));
- Pointer pointer6 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n5.setParameter(0, pointer5);
- n5.setParameter(1,pointer6);
- n5.setRetVal(Type.INT);
- n5.invoke();
- String IDNo=pointer5.getAsString();
- if(n5.getRetValAsInt()==1){
- System.out.println(n5.getRetVal());
- System.out.println("读取完毕!");
- }
- System.out.println("返回值:"+n5.getRetValAsInt());
- System.out.println("身份证号是--长度:"+IDNo.length()+" 身份证号--"+IDNo);
- return IDNo;
- }
- public String getAddress() throws NativeException, IllegalAccessException{
- //读地址
- JNative n6 = new JNative("termb.dll", "GetPeopleAddress");
- Pointer pointer7 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
- Pointer pointer8 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n6.setParameter(0, pointer7);
- n6.setParameter(1,pointer8);
- n6.setRetVal(Type.INT);
- n6.invoke();
- String address=pointer7.getAsString();
- if(n6.getRetValAsInt()==1){
- System.out.println(n6.getRetVal());
- System.out.println("读取完毕!");
- }
- System.out.println("返回值:"+n6.getRetValAsInt());
- System.out.println("地址是--长度:"+address.length()+" 地址--"+address);
- return address;
- }
- public String getDepartment() throws NativeException, IllegalAccessException{
- //读发证机关
- JNative n7 = new JNative("termb.dll", "GetDepartment");
- Pointer pointer9 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
- Pointer pointer10 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n7.setParameter(0, pointer9);
- n7.setParameter(1,pointer10);
- n7.setRetVal(Type.INT);
- n7.invoke();
- String department=pointer9.getAsString();
- if(n7.getRetValAsInt()==1){
- System.out.println(n7.getRetVal());
- System.out.println("读取完毕!");
- }
- System.out.println("返回值:"+n7.getRetValAsInt());
- System.out.println("地址是--长度:"+department.length()+" 地址--"+department);
- return department;
- }
- public String getBirth() throws NativeException, IllegalAccessException{
- //读生日
- JNative n9 = new JNative("termb.dll", "GetPeopleBirthday");
- Pointer pointer15 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
- Pointer pointer16 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
- n9.setParameter(0, pointer15);
- n9.setParameter(1,pointer16);
- n9.setRetVal(Type.INT);
- n9.invoke();
- String birth=pointer15.getAsString();
- if(n9.getRetValAsInt()==1){
- System.out.println(n9.getRetVal());
- System.out.println("读取完毕!");
- }
- System.out.println("返回值:"+n9.getRetValAsInt());
- System.out.println("生日是--长度:"+birth.length()+" 生日--"+birth);
- return birth;
- }
- public void Init() throws NativeException, IllegalAccessException{
- JNative n = new JNative("termb.dll", "CVR_InitComm");
- n.setRetVal(Type.INT);
- n.setParameter(0,1001);
- n.invoke();
- System.out.println("请放置身份证!");
- int cnt=0;
- for(int i=0;;i++){
- //System.out.println("请正确放置身份证! ");
- cnt++;
- JNative n3= new JNative("termb.dll", "CVR_Authenticate");
- n3.setRetVal(Type.INT);
- n3.invoke();
- //System.out.println(n3.getRetVal());
- int r=n3.getRetValAsInt();
- if(r==1){
- break;
- }
- }
- System.out.println("正确放置,身份证初始化完成,卡放置正确!");
- JNative n1 = new JNative("termb.dll", "CVR_Read_Content");
- n1.setParameter(0,6);
- n1.setRetVal(Type.INT);
- n1.invoke();
- //System.out.println(n1.getRetVal());
- if(n1.getRetValAsInt()==1){
- System.out.println("读卡操作正确!");
- }
- }
- public void Close() throws NativeException, IllegalAccessException{
- JNative n1 = new JNative("termb.dll", "CVR_CloseComm");
- n1.invoke();
- //int t=n1.getRetValAsInt();
- }
- }