偶然的你
基于提出的问题,这已经在第一个答案中得到了解释,即如何通过jobjectArray传递int []。但是这是一个示例,我们如何返回包含数据列表的jobjectArray。例如,这在某些情况下很有用:当某人需要返回2D格式的数据以绘制带有x和y点的线时。以下示例显示jobjectArray如何以以下格式的形式返回数据:Java输入到JNI:Array [ Arraylistof x float points] [ Arraylistof y float points]JNI输出到Java:jobjectArray[ Arraylist个x浮点数] [ Arraylist个y浮点数] 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;