我正在尝试使用android NDK。
有没有办法将在JNI中创建的数组(在我的情况下为int[]
)返回给Java?如果是这样,请提供一个JNI函数的快速示例。
-Thanks
答案 0 :(得分:107)
如果您已经检查了文档,但仍然有问题应该是您的初始问题的一部分。在这种情况下,示例中的JNI函数会创建许多数组。外部数组由使用JNI函数NewObjectArray()
创建的“对象”数组组成。从JNI的角度来看,这是一个二维数组,是一个包含许多其他内部数组的对象数组。
以下for循环使用JNI函数NewIntArray()
创建int []类型的内部数组。如果您只想返回一维的单维数组,那么NewIntArray()
函数就是您用来创建返回值的函数。如果您想创建一个字符串的单维数组,那么您将使用NewObjectArray()
函数,但该类具有不同的参数。
因为你想返回一个int数组,所以你的代码看起来像这样:
JNIEXPORT jintArray JNICALL Java_ArrayTest_initIntArray(JNIEnv *env, jclass cls, int size)
{
jintArray result;
result = (*env)->NewIntArray(env, size);
if (result == NULL) {
return NULL; /* out of memory error thrown */
}
int i;
// fill a temp structure to use to populate the java int array
jint fill[size];
for (i = 0; i < size; i++) {
fill[i] = 0; // put whatever logic you want to populate the values here.
}
// move from the temp structure to the java structure
(*env)->SetIntArrayRegion(env, result, 0, size, fill);
return result;
}
答案 1 :(得分:34)
如果有人想知道如何返回String []数组:
java代码
private native String[] data();
原生出口
JNIEXPORT jobjectArray JNICALL Java_example_data() (JNIEnv *, jobject);
原生代码
JNIEXPORT jobjectArray JNICALL
Java_example_data
(JNIEnv *env, jobject jobj){
jobjectArray ret;
int i;
char *message[5]= {"first",
"second",
"third",
"fourth",
"fifth"};
ret= (jobjectArray)env->NewObjectArray(5,
env->FindClass("java/lang/String"),
env->NewStringUTF(""));
for(i=0;i<5;i++) {
env->SetObjectArrayElement(
ret,i,env->NewStringUTF(message[i]));
}
return(ret);
}
来自链接: http://www.coderanch.com/t/326467/java/java/Returning-String-array-program-Java
答案 2 :(得分:0)
基于提出的问题,这已经在第一个答案中得到了解释,即我们如何通过jobjectArray传递int []。但是这是一个示例,我们如何返回包含数据列表的jobjectArray。例如,这在某些情况下很有用:当某人需要返回2D格式的数据以绘制带有x和y点的线时。以下示例显示jobjectArray如何以以下格式返回数据:
JNI的Java输入:
数组[{Arraylist
个x个浮点数] [Arraylist
个y个浮点数]
JNI输出到Java:
x个浮点中的jobjectArray
[Arraylist
个,y个浮点中的[Arraylist
个]
extern "C" JNIEXPORT jobjectArray JNICALL
_MainActivity_callOpenCVFn(
JNIEnv *env, jobject /* this */,
jobjectArray list) {
//Finding arrayList class and float class(2 lists , one x and another is y)
static jclass arrayListCls = static_cast<jclass>(env->NewGlobalRef(env->FindClass("java/util/ArrayList")));
jclass floatCls = env->FindClass("java/lang/Float");
//env initialization of list object and float
static jmethodID listConstructor = env->GetMethodID(arrayListCls, "<init>", "(I)V");
jmethodID alGetId = env->GetMethodID(arrayListCls, "get", "(I)Ljava/lang/Object;");
jmethodID alSizeId = env->GetMethodID(arrayListCls, "size", "()I");
static jmethodID addElementToList = env->GetMethodID(arrayListCls, "add", "(Ljava/lang/Object;)Z");
jmethodID floatConstructor = env->GetMethodID( floatCls, "<init>", "(F)V");
jmethodID floatId = env->GetMethodID(floatCls,"floatValue", "()F");
//null check(if null then return)
if (arrayListCls == nullptr || floatCls == nullptr) {
return 0;
}
// Get the value of each Float list object in the array
jsize length = env->GetArrayLength(list);
//If empty
if (length < 1) {
env->DeleteLocalRef(arrayListCls);
env->DeleteLocalRef(floatCls);
return 0;
}
// Creating an output jObjectArray
jobjectArray outJNIArray = env->NewObjectArray(length, arrayListCls, 0);
//taking list of X and Y points object at the time of return
jobject xPoint,yPoint,xReturnObject,yReturnObject;
//getting the xList,yList object from the array
jobject xObjFloatList = env->GetObjectArrayElement(list, 0);
jobject yObjFloatList = env->GetObjectArrayElement(list, 1);
// number of elements present in the array object
int xPointCounts = static_cast<int>(env->CallIntMethod(xObjFloatList, alSizeId));
static jfloat xReturn, yReturn;
jobject xReturnArrayList = env->NewObject(arrayListCls,listConstructor,0);
jobject yReturnArrayList = env->NewObject(arrayListCls,listConstructor,0);
for (int j = 0; j < xPointCounts; j++) {
//Getting the x points from the x object list in the array
xPoint = env->CallObjectMethod(xObjFloatList, alGetId, j);
//Getting the y points from the y object list in the array
yPoint = env->CallObjectMethod(yObjFloatList, alGetId, j);
//Returning jobjectArray(Here I am returning the same x and points I am receiving from java side, just to show how to make the returning `jobjectArray`)
//float x and y values
xReturn =static_cast<jfloat >(env->CallFloatMethod(xPoint, floatId,j));
yReturn =static_cast<jfloat >(env->CallFloatMethod(yPoint, floatId,j));
xReturnObject = env->NewObject(floatCls,floatConstructor,xReturn);
yReturnObject = env->NewObject(floatCls,floatConstructor,yReturn);
env->CallBooleanMethod(xReturnArrayList,addElementToList,xReturnObject);
env->CallBooleanMethod(yReturnArrayList,addElementToList,yReturnObject);
env->SetObjectArrayElement(outJNIArray,0,xReturnArrayList);
env->SetObjectArrayElement(outJNIArray,1,yReturnArrayList);
__android_log_print(ANDROID_LOG_ERROR, "List of X and Y are saved in the array","%d", 3);
}
return outJNIArray;
答案 3 :(得分:-1)
简单的解决方案是将数组数据从C中写入文件,然后从Java中访问文件