从JNI / NDK返回从C二维基本数组到Java [英] Return a 2D primitive array from C to Java from JNI/NDK
问题描述
我已经找到了如何生成JNI二维基本数组并返回给Java的大量文档。但是这些信息没有描述如何通过一个现有 2D float数组(浮动**)给出了上下文 C
I have found large amounts of documentation on how to generate a 2D primitive array in JNI and returning it to Java. But these pieces of information fail to describe how to pass an already existing 2D float array (float**) given a context in C.
要形容我的问题明确,我将添加一些 C 我想实现什么伪code:
To describe my issue explicitly, I'll add some C pseudo code of what I would like to implement:
// 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;
}
我猜想这是不是直线前进,因为我一直没能找到任何描述这个场景。
I'm assuming this is not straight forward, given I haven't been able to find anything describing this scenario.
感谢您的任何有用的信息。
Thanks for any helpful information.
推荐答案
蒂莫感谢您的帮助和纽带。对于后人,我加入了完整的code组将通过生成在Java二维基本数组消费品,从现有的C基本2D阵列的过程。
Thanks Timo for your help and link. For posterity, I'm adding a complete code set that would go through the process of generating a 2D primitive array consumable by Java, from an existing C 2D primitive array.
// 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;
}
这篇关于从JNI / NDK返回从C二维基本数组到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!