其他分享
首页 > 其他分享> > 使用JNA读取dll文件

使用JNA读取dll文件

作者:互联网

由于项目需要进行读卡操作,需要使用java进行读取dll文件
设备:德卡T10
1.    引入POM文件
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.6.0</version>
</dependency>

2.写入接口

import com.sun.jna.Library;import com.sun.jna.Native;
import com.sun.jna.NativeLong;


public interface WINAPI extends Library{

WINAPI INSTANCE = Native.loadLibrary("SSCard",WINAPI.class);
//初始化
NativeLong Init(String s1, String s2);
//读取社保卡
NativeLong ReadCardBas(byte[] s1,int s2,byte[] s3,int s4);
//校验PIN码
NativeLong VerifyPIN(byte[] s1,int s2);
//修改PIN码
NativeLong ChangePIN(byte[] s1,int s2);
//读身份证信息
NativeLong ReadSFZ(byte[] s1,int s2,byte[] s3,int s4);
//读取二维码信息
NativeLong GetQRBase(int s0,byte[] s1,int s2,byte[] s3,int s4);
//校验医保密码
NativeLong VerifyYBPZPass(byte[] s1,int s2);
//修改医保密码
NativeLong ChangeYBPZPass(byte[] s1,byte[] s2,byte[] s3,byte[] s4,byte[] s5);
}

3.写对应的方法类
PS:
1.byte[]不是传入参数,而是返回参数,进行字节转字符串后才能获取对应的参数
2.由于德卡为了匹配大多数电脑,必须使用32位JDK才能读取成功,否则报错找不到dll文件路径
3.解码类型为GBK

德卡T10文档参照附件

import com.sun.jna.NativeLong;
import io.renren.modules.back.service.WINAPI;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;

@Component
public class DKUtil {
public static String pUrl="http://10.126.105.66/hsa-hgs-adapt/api/card/initDll";

private static String test() throws UnsupportedEncodingException {
//jna linux相对路径调用c/c++接口时候,名称必须是lib开头,且第一个参数是lib后面的名称,如libtest.so,加载时候应该使用test来加载
//jna win相对路径调用c/c++接口时候,直接名称不加后缀调用,如test.dll,加载时候应该使用test来加载
//jna linux绝对路径调用c/c++接口时候,完整路径加上后缀,如test.so,加载时候应该使用/opt/test.so来加载
//jna win相对路径调用c/c++接口时候,完整路径可以不接后缀,如test.dll,加载时候应该使用D:\\test来加载
// Test lib = (Test) Native.loadLibrary("test_64", Test.class);
//Test lib = Native.loadLibrary(path, Test.class);
// 例1

NativeLong nativeLong = WINAPI.INSTANCE.Init(pUrl, "620100");
System.out.println("11111" + nativeLong);
byte[] chars1 = new byte[4096];
byte[] chars2 = new byte[4096];
NativeLong nativeLong1 = WINAPI.INSTANCE.ReadSFZ(chars1, 4096, chars2, 4096);
String result;
System.out.println("22222222:" + nativeLong1);
result = new String(chars1, "GBK");
result = result + "|" + new String(chars2, "GBK");
System.out.println(result);
return result;
}

/**
* 初始化
* @returnR
*/
public static R init() {
NativeLong nativeLong= WINAPI.INSTANCE.Init(pUrl,"620100");
if (!"1".equals(nativeLong)){
return R.ok().put("msg","初始化成功");
}else {
return R.error().put("msg","初始化失败");
}
}

/**
* 读取身份证信息
* @return
*/
public static R getSFZ() throws UnsupportedEncodingException {
R r=init();
if (!"0".equals(r.get("code").toString())){
return r;
}
byte[] chars1 = new byte[4096];
byte[] chars2 = new byte[4096];
NativeLong nativeLong=WINAPI.INSTANCE.ReadSFZ(chars1,4096,chars2,4096);
String result;
if ("0".equals(nativeLong.toString())){
result=new String(chars1,"GBK");
result=result+"|"+new String(chars2,"GBK");
System.out.println(result);
return R.ok().put("msg",result);
}else {
result=new String(chars1,"GBK");
result=result+"|"+new String(chars2,"GBK");
System.out.println(result);
return R.error().put("msg",result);
}
}

/**
* 读取社保卡信息
* @return
*/
public static R getYBCard() throws UnsupportedEncodingException {
init();

byte[] chars1 = new byte[4096];
byte[] chars2 = new byte[4096];
NativeLong nativeLong=WINAPI.INSTANCE.ReadCardBas(chars1,4096,chars2,4096);
String result;
if ("1".equals(nativeLong)){
result=new String(chars1,"GBK");
result=result+"|"+new String(chars2,"GBK");
return R.ok().put("msg",result);
}else {
result=new String(chars1,"GBK");
result=result+"|"+new String(chars2,"GBK");
return R.error().put("msg",result);
}
}

/**
* 读取二维码
* @return
*/
public static R getQRBase() throws UnsupportedEncodingException {
init();

byte[] chars1 = new byte[1024];
byte[] chars2 = new byte[1024];
NativeLong nativeLong=WINAPI.INSTANCE.GetQRBase(100,chars1,4096,chars2,4096);
String result;
if ("1".equals(nativeLong)){
result=new String(chars1,"GBK");
return R.ok().put("Content",result);
}else {
result=new String(chars1,"GBK");
return R.error().put("Content",result);
}
}

public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getSFZ());
}
}

 

标签:String,NativeLong,dll,JNA,result,new,byte,chars1,读取
来源: https://www.cnblogs.com/king-dom/p/15841807.html