Simplest way to pass float[][] to C++ via JNI

前端 未结 2 1702
陌清茗
陌清茗 2020-12-09 17:37

In my Java code I have a 2D float array float[x][4] floatArray. Here x can be between 1 and 25. I have to pass this 2D float array to a C++<

2条回答
  •  半阙折子戏
    2020-12-09 18:41

    Something like this should work:

    jboolean MyJNIMethod(JNIEnv * env, jobject obj, jobjectArray myArray)
    {
      int len1 = env -> GetArrayLength(myArray);
      jfloatArray dim=  (jfloatArray)env->GetObjectArrayElement(myArray, 0);
      int len2 = env -> GetArrayLength(dim);
      float **localArray;
      // allocate localArray using len1
      localArray = new float*[len1];
      for(int i=0; iGetObjectArrayElement(myArray, i);
         jfloat *element=env->GetFloatArrayElements(oneDim, 0);
         //allocate localArray[i] using len2
         localArray[i] = new float[len2];
         for(int j=0; j

    Note that this is outline. It won't compile ;) (I wrote it in this overstacks' editor)

    In your class you should declare native method:

     public native void myJNIMethod(float[][] m);
    

    and in your c code corresponding:

    JNIEXPORT jboolean JNICALL Java_ClassName_methodName (JNIEnv *, jobject, jobjectArray);
    

    Here is JNI array operations documentation.

提交回复
热议问题