标签:void local port strong jnienv ase oid har bool
学习Android Jni时,一个二维 整数 数组的传递花了我好长时间,在网上查的资料都不全,当然最后是成功了,写在这里是为了自己记住,当然有人搜索到并利用了我会很高兴。
in Android Java file
public native boolean add(int[][] qizidata,.....)
用JDK工具生产jni 的*.h文件
your project path> ..Javajdk1.6.0_10binjavah -classpath ./bin JAVA Class name
//**************************************************************************
//二维数组java -》Jni
JNIEXPORT jint JNICALL Java_com_Jung_add
(JNIEnv * env, jobject
obj, jobjectArray qizidata,jint value1, jint
value2)
{
jint
i,j;
jint
qizi[1024][1024]; //固定数组,测试用
jarray
myarray;
int
size = (*env)->GetArrayLength(env, qizidata);
for (i
= 0; i < size &&
i<1024 ; i++)
{
myarray =
((*env)->GetObjectArrayElement(env, qizidata,
i));
int col =
(*env)->GetArrayLength(env,
myarray);
jint *coldata =
(*env)->GetIntArrayElements(env, (jintArray)myarray,
0 );
for (j=0; j<col
&& j <1024;
j++)
{
qizi[i][j]
= coldata[j];
}
(*env)->ReleaseIntArrayElements(env,
(jintArray)myarray, coldata,0 );
}
return
(qizi[0][0]+qizi[9][0]);
}
//**************************************************************************
//二维数组Jni -》Java
JNIEXPORT jobjectArray JNICALL
Java_com_marakana_NativeLib_GetIntArray
(JNIEnv *env, jobject obj, jint
x, jint y)
{
jobjectArray ret;
int i =
0;
int j =
0;
jclass
intArrCls = (*env)->FindClass(env, "[I");
if (
NULL== intArrCls)
return NULL;
ret =
(*env)->NewObjectArray(env, x * y, intArrCls,
NULL);
jint
tmp[1024];//固定数组,测试用,注意别溢出
for (i
= 0; i < x; i++) {
jintArray intArr =
(*env)->NewIntArray(env,y);
for (j = 0; j <
y; j++) {
tmp[j] = i + j;
}
(*env)->SetIntArrayRegion(env,intArr, 0, y,
tmp);
(*env)->SetObjectArrayElement(env,ret, i,
intArr);
(*env)->DeleteLocalRef(env,intArr);
}
return
ret;
}
//**************************************************************************
//下面代码是网上搜的,没验证,忘记出处了,请原作者见谅
// 二维String数组 JNI 提取
c_languge syntax
JNIEXPORT void JNICALL Java_TestArray_PrintArrays
(JNIEnv *env, jclass class, jobjectArray array)
{
int
size = (*env)->GetArrayLength(env, array);
for
(int i = 0; i < size; i++)
{
jarray myarray =
((*env)->GetObjectArrayElement(env, array,
i));
int leng =
(*env)->GetArrayLength(env, myarray);
printf("length: %dn",
leng);
for (int j = 0; j
< leng; j++)
{
jstring string =
((*env)->GetObjectArrayElement(env, myarray,
i));
const char * chars =
(*env)->GetStringUTFChars(env, string, 0);
printf("%s n", chars);
(*env)->ReleaseStringUTFChars(env,
string, chars);
}
}
}
Android jni 二维数组 传递
标签:void local port strong jnienv ase oid har bool
原文地址:http://www.cnblogs.com/Free-Thinker/p/6829210.html