将原始数组从java传递给c [英] passing array of primitives from java to c
问题描述
我有一个int数组,比如
I have a int array like
int[][] coordinates = {{346, 640, 499, 666},{555, 640, 675, 666}};
我试图使用JNI将此数组发送到C.我可以找到一维数组的明确解决方案,但不能找到多维数组。
and I am trying to send this array to C with using JNI. I could find clear solution for one dimensional array but not for multidimensional array.
<br />
public class PopulateCoordinates{<br />
public native void Coordinates(int[][] coordinates);<br />
<br />
static {<br />
System.loadLibrary("libPopulateCoordinates");<br />
}<br />
public static void main(String[] args) {<br />
<br />
PopulateCoordinates h = new PopulateCoordinates();<br />
int[][] coordinates = {{346, 640, 499, 666},{555, 640, 675, 666}};<br />
h.Coordinates(coordinates);<br />
}<br />
}<br />
我的C代码有一个方法,它将填充从java传递的数组,如下所示
My C code has method which will populate its array that is passed from java like this
rect skip_rectarr[2] = { {346, 640, 499, 666}, { 555, 640, 675, 666 }};
<br />
typedef struct {<br />
int left;<br />
int top;<br />
int right;<br />
int bot;<br />
} rect;<br />
JNIEXPORT void JNICALL PopulateCoordinates_Coordinates(JNIEnv *env, jobject obj,jobjectArray inJNIArray)<br />
{<br />
//rect skip_rectarr[2] = { {346, 640, 499, 666}, { 555, 640, 675, 666 }};<br />
}<br />
我很难将jobject正确转换为int [] []到期缺乏本土发展知识。有人能告诉我最简单,最安全的方法吗?谢谢
I am having a hard time in properly converting jobject to int[][] due to lack of native development knowledge. Can anyone tell me the simplest and safest possible way? Thanks
推荐答案
说实话:最简单和最简单的方法是使用具有本机数据类型和AVOID结构的一维数组。 Java和C具有不同的内存布局。微软用.net语言解决了这个问题。
我会扩展rhe func的接口和大小信息:
To be honest: The simplest and savest way is to use one dimension arrays with native data types and AVOID structs. Java and C have different memory layouts. Microsoft has solved this mess on its .net-languages.
I would extend the interfaces of rhe func with size information:
JNIEXPORT void JNICALL PopulateCoordinates_CoordinatesRect(JNIEnv *env, jobject obj,jobjectArray inJNIArray, int countObjectes)
你不应该在接口上做很多事情,但更好地传输简单和原生数据。或者你可能会浪费很多时间。
You shouldnt fizzle to much on the interfaces, but better transfer simple and native data. Or you may waiste a lot of time.
这篇关于将原始数组从java传递给c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!