Java判断不同平台操作系统调用相应的接口动态库读写IC卡

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.1d292c1btRvdS1&ft=t&id=615391857885

使用Jni调用

public class icreadertest {
    public native static byte pcdbeep(int xms);    //让设备发出声音
    public native static byte pcdgetdevicenumber(byte[] devicenumber);     //读取设备编号
    public native static byte piccreadex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] picckey,byte[] piccdata0_2);     //轻松读卡
    public native static byte piccwriteex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] picckey,byte[] piccdata0_2);     //轻松写卡
    public native static byte piccchangesinglekey(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] piccoldkey,byte[] piccnewkey);  //改单区密码
    public native static byte piccchangesinglekeyex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] piccoldkey,byte[] piccdata); //改单区密码,可同时修改A密码,或密码权限访问字(密码权限编码)或B密码
    public native static byte piccauthkey1(byte[] serial,byte area,byte keyA1B0,byte[] picckey);  //认证卡片密码
    public native static byte piccrequest(byte[] serial);       //寻卡
    public native static byte piccread(byte block,byte[] blockdate);        //读指定块信息
    public native static byte piccwrite(byte block,byte[] blockdate);       //写指定块信息
    public native static byte piccwriteserial(byte ctrlword,byte[] serial,byte keyA1B0,byte[] piccoldkey,byte[] piccdata);      //写指定块信息
    public native static byte sfzrequest(byte[] serial);        //读身份证UID
    public native static byte iso15693iclassreadcsn(byte[] serial);     //读iCLASS卡CSN
    public native static byte picccmd(byte BitFramingVal, byte ChannelRedundancyVal, byte[] sendbuf, int sendbitlen, byte[] revbuf, byte[] revbitlen, byte TimerFlagval);       //透传读写M1调试指令

    static{
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("windows")) {
            System.load( System.getProperty("user.dir") +"\\OUR_MIFARE.dll");
        } else if (osName.contains("nix") || osName.contains("nux")) {
            System.load(System.getProperty("user.dir") + "/libOURMIFARE.so");
        } else if (osName.contains("mac")) {
            System.load(System.getProperty("user.dir") + "/libOURMIFARE.dylib");
        }
    }

}

使用Jna调用

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

interface CLibrary extends Library {
    //不同版本的读写器,接口DLL文件名称、函数名称是一样的,但内核代码不一样,请选用与读写器、操作系统一致的OUR_MIFARE.dll
    CLibrary INSTANCE = loadLibrary();
    static CLibrary loadLibrary() {
        if (Platform.isWindows()) {
            return Native.loadLibrary(System.getProperty("user.dir") +"\\OUR_MIFARE.dll", CLibrary.class);
        } else if (Platform.isLinux()) {
            return Native.loadLibrary(System.getProperty("user.dir") + "/libOURMIFARE.so", CLibrary.class);
        } else if (Platform.isMac()) {
            return Native.loadLibrary(System.getProperty("user.dir") + "/libOURMIFARE.dylib", CLibrary.class);
        } else {
            throw new RuntimeException("Unsupported operating system");
        }
    }

    //动态链接库中的方法
    byte pcdbeep(int xms);                         //让设备发出声音
    byte pcdgetdevicenumber(byte[] devicenumber);  //读取设备编号
    byte piccreadex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] picckey,byte[] piccdata0_2);             //轻松读卡
    byte piccwriteex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] picckey,byte[] piccdata0_2);            //轻松写卡
    byte piccchangesinglekey(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] piccoldkey,byte[] piccnewkey);  //改单区密码
    byte piccchangesinglekeyex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] piccoldkey,byte[] piccdata);  //改单区密码,可同时修改A密码,或密码权限访问字(密码权限编码)或B密码
    byte piccrequest(byte[] serial);              //寻卡
    byte piccauthkey1(byte[] serial,byte area,byte keyA1B0,byte[] picckey);    //认证卡片密码
    byte piccread(byte block,byte[] blockdate);   //读指定块信息
    byte piccwrite(byte block,byte[] blockdate);   //写指定块信息
    byte piccwriteserial(byte ctrlword,byte[] serial,byte keyA1B0,byte[] piccoldkey,byte[] piccdata);  //写UID
    byte sfzrequest(byte[] serial);              //读身份证UID
    byte iso15693iclassreadcsn(byte[] serial);   //读iCLASS卡CSN
    byte picccmd(byte BitFramingVal, byte ChannelRedundancyVal, byte[] sendbuf, int sendbitlen, byte[] revbuf, byte[] revbitlen, byte TimerFlagval);  //透传读写M1调试指令

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值