Frage

Ich versuche, die Android NDK zu verwenden.

Gibt es eine Möglichkeit, ein Array zurück (in meinem Fall ein int[]) in JNI Java erstellt? Wenn ja, bitte ein kurzes Beispiel der JNI-Funktion zur Verfügung stellen, die dies tun würden.

-Danke

War es hilfreich?

Lösung

Wenn Sie die Dokumentation untersucht haben und noch Fragen haben, die Teil Ihrer ersten Frage sein sollte. In diesem Fall erzeugt die JNI-Funktion in dem Beispiel eine Anzahl von Arrays. Die äußere Anordnung ist aus einem ‚Objekt‘ Array besteht mit der JNI NewObjectArray() Funktion zu schaffen. Aus der Perspektive der JNI, das ist eine ganze zweidimensionale Anordnung ist, ein Objekt-Array eine Anzahl von anderen inneren Arrays enthält.

Die folgende for-Schleife erzeugt, die innere Arrays, die vom Typ sind int [] die Funktion JNI NewIntArray() verwenden. Wenn Sie nur ein eindimensionales Array von ints zurückkehren wollte, dann ist die NewIntArray() Funktion, was würden Sie den Rückgabewert erstellen können. Wenn Sie eine einzelne zweidimensionale Anordnung von Strings schaffen wollen, dann würde man die NewObjectArray() Funktion verwenden, aber mit einem anderen Parameter für die Klasse.

Da Sie ein int-Array zurückgeben möchten, dann ist Ihr Code wird so etwas wie folgt aussehen:

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;
}

Andere Tipps

, wenn jemand würde gerne wissen, wie String [] Array zurück:

Java-Code

private native String[] data();

nativer Export

JNIEXPORT jobjectArray JNICALL Java_example_data() (JNIEnv *, jobject);

nativen Code

  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);  
  }  

von Link: http://www.coderanch.com/t/326467/java/ java / Return-String-Array-Programm-Java

auf die gestellte Frage Basierend dies bereits in der ersten Antwort erklärte, wie können wir int [] über jobjectArray passieren. Aber hier ist ein Beispiel dafür, wie wir können eine jobjectArray zurück, die Listen von Daten enthält. Dies kann für Situationen, zum Beispiel hilfreich sein: Wenn jemand, um Daten in 2D-Format benötigt einige Zeilen mit x und y Punkten zu zeichnen. Das folgende Beispiel zeigt, wie ein jobjectArray Daten in Form von folgendem Format zurückkehren kann:

Java Eingang mit dem JNI:
Array [Arraylist von x float Punkte] [Arraylist von y float Punkte]

JNI Ausgabe java:
jobjectArray [Arraylist von x float Punkte] [Arraylist von y float Punkte]

    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;

Einfache Lösung ist, dass die Array-Daten in einer Datei von C schreiben, und dann Zugriff auf die Datei von Java

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top