引言:
JNI是Java可以调用高效的c++代码,但是在JNI函数中传递复杂数据类型十分的繁琐,一般情况下需要jobject作为中转,一维数组int[],float[]以及二维数组String[]可以实现方便的参数传入/传出。其他复杂的二、三维数组理论上可以使用jobjectarray实现参数传入/传出,可是实现过程中出现问题,可能是内存上的错误。
下面介绍一种通用方法,在JNI函数中引用Java类,将cxx复杂数据转换成jobjectArray,实现从JNI到Java的数据传输。
Step1:定义Java类接口
这里定义了一个目标检测的通用接口类。创建”OneDetection.java”如下:
package com.example.cuizhou.test01;
import android.graphics.Bitmap;
/**
* Created by cuizhou on 18-1-11.
*/
public class OneDetection {
private String mLabel;
private float mConfidence;
private int mLeft;
private int mTop;
private int mRight;
private int mBottom;
public OneDetection() {
}
/**
* @param label Label name
* @param confidence A confidence factor between 0 and 1. This indicates how certain what has been found is actually the label.
* @param l The X coordinate of the left side of the result
* @param t The Y coordinate of the top of the result
* @param r The X coordinate of the right side of the result
* @param b The Y coordinate of the bottom of the result
*/
public OneDetection(String label, float confidence, int l, int t, int r, int b) {
mLabel = label;
mLeft = l;
mTop = t;
mRight = r;
mBottom = b;
mConfidence = confidence;
}
/**
* @return The X coordinate of the left side of the result
*/
public int getLeft() {
return mLeft;
}
/**
* @return The Y coordinate of the top of the result
*/