java-JNA调用

pom

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.5.0</version>
</dependency>

工具

java -jar jnaerator-0.12-shaded.jar //(按钮在左上角)

jnaerator使用方法

基本类型

int commonTypeTest(int ival0, int* ival1, float fval0, float* fval1, double dval0, double* dval1,int exe=10);
int commonTypeTest(int ival0, IntByReference ival1, float fval0, FloatByReference fval1, double dval0, DoubleByReference dval1,int ext);

数组类型

int floatArrayTest(float* arry0,int arry0Len,float* arry1,int* arry1Len);
int floatArrayTest(float[] arry0, int arry0Len, float[] arry1, FloatByReference arry1Len);

//demo
float [] ayyao=new float[10];
float [] ayya1=new float[10];
FloatByReference ayya1Len=new FloatByReference();
MyjnacLibrary.INSTANCE.floatArrayTest(ayyao,10,ayya1,ayya1Len);

字符串类型

int stringTypeTest(const char* str0, char* str1, int *str1Len);
int stringTypeTest(String str0, Pointer pstr1, IntByReference pstr1Len);

//demo
String str0 = "this is stringTypeTest test.";
Pointer pstr1 = new Memory(256);
IntByReference pstr1Len = new IntByReference();
ret = MyjnacLibrary.INSTANCE.stringTypeTest(str0, pstr1, pstr1Len);
byte pstr1bytes[] = pstr1.getByteArray(0, pstr1Len.getValue());
String str1 = new String(pstr1bytes);

回调类型

typedef void(*FUNP)();
int callbackTest(FUNP p);

public interface FUNP extends Callback {
    void apply();
};
int callbackTest(MyjnacLibrary.FUNP p);

//demo
MyjnacLibrary.FUNP callback= new MyjnacLibrary.FUNP() {
    public void apply() {
        System.out.println("this is a jna callback test");
    }
};
ret= MyjnacLibrary.INSTANCE.callbackTest(callback);

结构体和结构体指针

int simpleStructTest1(Personal per0, Personal *per1);
int simpleStructTest1(Personal.ByValue per0, Personal per1);

IrsFaceRecogResultAll IrsFace=new IrsFaceRecogResultAll();
Pointer IrsFacepp1=IrsFace.getPointer();
IrsFace.age=111;
IrsFace.write();

结构体数组

int simpleStructTest3(Personal* per0, int per0Len, Personal *per1, int *per1Len);
int simpleStructTest3(Personal[] per0, int per0Len, Personal[] per1, IntByReference per1Len);

//demo
int per0Len=5;
Personal[] per0 = (Personal[])new Personal().toArray(per0Len);
PointerByReference irsHandle = new PointerByReference(Pointer.NULL);


——————————————————————————————————————————
注意
对于JAVA来说
纯c接口
一个不确定长度的数组可以用字符串代替
const char* 用String代替
char* 用byte[] 代替

pointer转byte

byte[] bytes = new byte[length];
pointer.read(0, bytes, 0, length);

——————————————————————————————————————————
下面贴一个参考实例

c的接口

#ifndef MYJNAC_H
#define MYJNAC_H
#include <string.h>
#include <string>
using std::string;
#include <iostream>
using std::cout;

#if defined(WIN32) || defined(_WIN32)
#ifdef MYDLLEXPORT
#define API_DLLEXPORT    extern "C"  __declspec(dllexport)
#else
#define API_DLLEXPORT    extern "C"  __declspec(dllimport)
#endif
#else
#define API_DLLEXPORT    extern "C"  __attribute__((visibility("default")))
#endif



struct Personal
{
	char *name;
	int nameLen;
	int sex;
	int age;
	float socre;
	double avage;
	float redu[4];
	Personal()
	{
		name = nullptr;
		nameLen = 0;
		sex = 0;
		age = 0;
		socre = 0;
		avage = 0;
	}

	int getSize()
	{
		return  sizeof(Personal);
	}

	Personal& operator=(const Personal&per)
	{
		this->sex = per.sex;
		this->age = per.age;
		this->socre = per.socre;
		this->avage = per.avage;
		this->nameLen = per.nameLen;
		for (size_t i = 0; i < 4; i++)
			this->redu[i] = per.redu[i] * 100;
		if (this->name != nullptr&&per.name != nullptr)
			memcpy(this->name, per.name, per.nameLen);

		return *this;
	}

};



API_DLLEXPORT int commonTypeTest(int ival0, int* ival1, float fval0, float* fval1, double dval0, double* dval1,int exe=10);
API_DLLEXPORT int stringTypeTest(const char* str0, char* str1, int *str1Len);
API_DLLEXPORT int pointPointTest(const char* name, void** pointpoint, int *pointpointLen);

API_DLLEXPORT int array1Test(int *a0, int a0Len, int *al, int*a1Len);
API_DLLEXPORT int array2Test(int *a0, int a0row, int a0col, int *a1, int*a1row, int*a1col);
API_DLLEXPORT int array3Test(int *a0, int a0i, int a0j, int a0k, int *a1, int *a1i, int *a1j, int *a1k);

API_DLLEXPORT int getStructSize();
API_DLLEXPORT int simpleStructTest1(Personal per0, Personal *per1);
API_DLLEXPORT int simpleStructTest2(Personal* per0, Personal *per1);
API_DLLEXPORT int simpleStructTest3(Personal* per0, int per0Len, Personal *per1, int *per1Len);


typedef void(*FUNP)();
API_DLLEXPORT int callbackTest(FUNP p);



API_DLLEXPORT int floatArrayTest(float* arry0,int arry0Len,float* arry1,int* arry1Len);
#endif

java的对应接口

//package myjnac;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import java.util.Arrays;
import java.util.List;
/**
 * <i>native declaration : line 2</i><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
 */
public class Personal extends Structure {
    /** C type : char* */
    public Pointer name;
    public int nameLen;
    public int sex;
    public int age;
    public float socre;
    public double avage;
    /** C type : float[4] */
    public float[] redu = new float[4];
    public Personal() {
        super();
    }
    protected List<String > getFieldOrder() {
        return Arrays.asList("name", "nameLen", "sex", "age", "socre", "avage", "redu");
    }
    /**
     * @param name C type : char*<br>
     * @param redu C type : float[4]
     */
    public Personal(Pointer name, int nameLen, int sex, int age, float socre, double avage, float redu[]) {
        super();
        this.name = name;
        this.nameLen = nameLen;
        this.sex = sex;
        this.age = age;
        this.socre = socre;
        this.avage = avage;
        if ((redu.length != this.redu.length))
            throw new IllegalArgumentException("Wrong array size !");
        this.redu = redu;
    }
    public Personal(Pointer peer) {
        super(peer);
    }
    public static class ByReference extends Personal implements Structure.ByReference {

    };
    public static class ByValue extends Personal implements Structure.ByValue {

    };
}


//package myjnac;
import com.sun.jna.*;
import com.sun.jna.ptr.DoubleByReference;
import com.sun.jna.ptr.FloatByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
//import myjnac.Personal.ByValue;
/**
 * JNA Wrapper for library <b>myjnac</b><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
 */
public interface MyjnacLibrary extends Library {
    public static final String JNA_LIBRARY_NAME = "myjna";
    public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(MyjnacLibrary.JNA_LIBRARY_NAME);
    public static final MyjnacLibrary INSTANCE = (MyjnacLibrary)Native.loadLibrary(MyjnacLibrary.JNA_LIBRARY_NAME, MyjnacLibrary.class);
    public static final int NAMELEN = (int)32;


    int commonTypeTest(int ival0, IntByReference ival1, float fval0, FloatByReference fval1, double dval0, DoubleByReference dval1,int ext);
    int stringTypeTest(String str0, Pointer pstr1, IntByReference pstr1Len);
    int pointPointTest(String name, PointerByReference pointPoint, IntByReference pointPointLen);

    public interface FUNP extends Callback {
        void apply();
    };
    int callbackTest(MyjnacLibrary.FUNP p);


    int array1Test(int[] arry0, int arry0Len, int[] arry1, IntByReference arry1len);
    int array2Test(int[] a0, int a0row, int a0col, int[] a1, IntByReference a1row, IntByReference a1col);
    int array3Test(int[] a0, int a0i, int a0j, int a0k, int[] a1, IntByReference a1i, IntByReference a1j, IntByReference a1k);


    int getStructSize();
    int simpleStructTest1(Personal.ByValue per0, Personal per1);
    int simpleStructTest2(Personal per0, Personal per1);
    int simpleStructTest3(Personal[] per0, int per0Len, Personal[] per1, IntByReference per1Len);

     int floatArrayTest(float[] arry0, int arry0Len, float[] arry1, FloatByReference arry1Len);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值