上層Java呼叫C/C++的函式
public native void 2DArrayInput(int[][] inputData); public native int[] returnArray(int[] inputData2);
二維int陣列傳值:
public final static int[][] inputData = { {0x0226,0x00} ,{0x0227,0x40} ,{0x0228,0x00} }; JniTest jnitest = new JniTest(); jnitest.2DArrayInput(inputData);
下層C/C++撰寫,轉換成JNI函式這裡我就不介紹,請參考-[Android]如何在AOSP下編譯JNI
JNIEXPORT void JNICALL Java_com_example_jni_JniTest_2DArrayInput (JNIEnv *env, jobject, jobjectArray inputData) { int len = env->GetArrayLength(inputData); jint qizi[len][2]; jobject datas; for (int i = 0; i < len; i++) { datas = (env->GetObjectArrayElement(inputData, i)); int num = env->GetArrayLength((jarray)datas); jint *data = env->GetIntArrayElements((jintArray)datas, 0); for (int j = 0; j < num; j++) { qizi[i][j] = data[j]; ALOGD("Show data:%d", qizi[i][j]); } env->ReleaseIntArrayElements((jintArray)datas, data, 0); } }
一維int陣列傳值與陣列回傳:
int[] inputData2 = {0, 1, 2, 3, 4}; ...(略) jnitest.arrayInput(inputData2);
JNI層:
JNIEXPORT void JNICALL Java_com_arcadyan_jni_JniTest_returnArray (JNIEnv *env, jobject, jintArray array) { jsize size = env->GetArrayLength(array); jint *intArray= env->GetIntArrayElements(array, 0); for (int i = 0; i < size; i++) { intArray[i] += 1; } jintArray newIntArray = env->NewIntArray(size); //把jint指標中的元素設定到jintArray物件中 env->SetIntArrayRegion(newIntArray, 0, size, intArray); return newIntArray; }
Place your comment