Cla*_*dia 9 c java java-native-interface multidimensional-array android-ndk
我已经找到了大量关于如何在JNI中生成2D原始数组并将其返回给Java的文档.但是这些信息无法描述如何在C中给出上下文传递已经存在的 2D浮点数组(float**).
为了明确地描述我的问题,我将添加一些我想要实现的C伪代码:
// Returns a 2D float array from C to Java
jfloatArray ndk_test_getMy2DArray(JNIEnv* env, jobject thiz, jlong context)
{
// Cast my context reference
MyContextRef contextRef = (MyContextRef) context;
// In case we need it below
unsigned int length = MyContextGet1DLength(contextRef);
// Get the 2D Array we want to "Cast"
float** primitive2DArray = MyContextGet2DArray(contextRef);
// Hokus pokus...
// We do something to create the returnable data to Java
//
// Below is the missing piece that would convert the primitive
// 2D array into something that can be returned consumed and consumed
// by Java
jfloatArray myReturnable2DArray
return myReturnable2DArray;
}
Run Code Online (Sandbox Code Playgroud)
我假设这不是直截了当的,因为我无法找到描述这种情况的任何内容.
感谢您提供有用的信息.
Cla*_*dia 17
感谢Timo的帮助和链接.对于后人,我正在添加一个完整的代码集,该代码集将通过Java从现有的C 2D原始数组生成可以使用的2D原始数组.
// Returns a 2D float array from C to Java
jobjectArray ndk_test_getMy2DArray(JNIEnv* env, jobject thiz, jlong context)
{
// Cast my context reference
MyContextRef contextRef = (MyContextRef) context;
// Get the length for the first and second dimensions
unsigned int length1D = MyContextGet1DLength(contextRef);
unsigned int length2D = MyContextGet2DLength(contextRef);
// Get the 2D float array we want to "Cast"
float** primitive2DArray = MyContextGet2DArray(contextRef);
// Get the float array class
jclass floatArrayClass = (*env)->FindClass(env, "[F");
// Check if we properly got the float array class
if (floatArrayClass == NULL)
{
// Ooops
return NULL;
}
// Create the returnable 2D array
jobjectArray myReturnable2DArray = (*env)->NewObjectArray(env, (jsize) length1D, floatArrayClass, NULL);
// Go through the firs dimension and add the second dimension arrays
for (unsigned int i = 0; i < length1D; i++)
{
jfloatArray floatArray = (*env)->NewFloatArray(env, length2D);
(*env)->SetFloatArrayRegion(env, floatArray, (jsize) 0, (jsize) length2D, (jfloat*) primitive2DArray[i]);
(*env)->SetObjectArrayElement(env, myReturnable2DArray, (jsize) i, floatArray);
(*env)->DeleteLocalRef(env, floatArray);
}
// Return a Java consumable 2D float array
return myReturnable2DArray;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7552 次 |
最近记录: |