java 通过 JNI 将 float[][] 传递给 C++ 的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6751825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:07:46  来源:igfitidea点击:

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

javac++java-native-interface

提问by Haris Hasan

In my Java code I have a 2D float array float[x][4] floatArray. Here xcan be between 1 and 25. I have to pass this 2D float array to a C++method via JNI. My JNImethod is

在我的 Java 代码中,我有一个 2D 浮点数组float[x][4] floatArray。这里x可以在 1 到 25 之间。我必须通过 将这个二维浮点数组传递给一个C++方法JNI。我的JNI方法是

jboolean MyJNIMethod(JNIEnv * env, jobject obj, jobjectArray myArray)
{
    //how to convert this myArray to something that can be safely passed to C++ method below
}

Inside MyJNIMethodI have to call a C++method and pass 2D float array taken from Java to this method

在内部MyJNIMethod我必须调用一个C++方法并将从 Java 获取的二维浮点数组传递给这个方法

bool MyCplusPlusMethod(float coordinates[][4])
    {

    }

I am having a hard time in properly converting jobject to float[][] due to lack of native development knowledge. Can anyone tell me the simplest and safest possible way? Thanks

由于缺乏本地开发知识,我很难将 jobject 正确转换为 float[][]。谁能告诉我最简单和最安全的方法?谢谢

回答by zacheusz

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; i<len1; ++i){
     jfloatArray oneDim= (jfloatArray)env->GetObjectArrayElement(myArray, i);
     jfloat *element=env->GetFloatArrayElements(oneDim, 0);
     //allocate localArray[i] using len2
     localArray[i] = new float[len2];
     for(int j=0; j<len2; ++j) {
        localArray[i][j]= element[j];
     }
  }
  //TODO play with localArray, don't forget to release memory ;)
}

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

请注意,这是大纲。它不会编译 ;) (我在这个 overstacks 的编辑器中写的)

In your class you should declare native method:

在您的课程中,您应该声明本机方法:

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

and in your c code corresponding:

并在您的 c 代码中对应:

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

Here is JNI array operations documentation.

这是JNI 数组操作文档

回答by Arpan Sarkar

For releasing the allocated memory you can do something like this:

要释放分配的内存,您可以执行以下操作:

static void releaseMatrixArray(JNIEnv *env, jobjectArray matrix) {
int size = (*env)->GetArrayLength(env, matrix);
for (int i = 0; i < size; i++) {
    jfloatArray oneDim = (jfloatArray) (*env)->GetObjectArrayElement(env, matrix, i);
    jfloat *elements = (*env)->GetFloatArrayElements(env, oneDim, 0);

    (*env)->ReleaseFloatArrayElements(env, oneDim, elements, 0);
    (*env)->DeleteLocalRef(env, oneDim);
  }
}

release local Array reference:

释放本地数组参考:

free(localArray);