在本机方法中更改数组的值

时间:2018-10-17 21:04:34

标签: java java-native-interface

我想在本机方法(jobjectArray)中更改2维数组的每个值。当我通过代码进行调试时,调试器显示此指针的*body值正在更改。但是,返回到Java文件后,数组将保持不变。我在做什么错了?

C ++类:

JNIEXPORT void JNICALL Java_model_CppDllLibrary_convertToNegative
(JNIEnv *env, jobject obj, jobjectArray image, jint width, jint height) {
printf("Java_model_CppDllLibrary_convertToNegative");

for (int i = 0; i<height; i++) {
    jintArray oneDim = (jintArray)env->GetObjectArrayElement(image,i);
    for (int j = 0; j<width; j++) {
        jint *body = env->GetIntArrayElements(oneDim, false);
        int p = body[j];
        int a = (p >> 24) & 0xff;
        int r = (p >> 16) & 0xff;
        int g = (p >> 8) & 0xff;
        int b = p & 0xff;
        r = 255 - r;
        g = 255 - g;
        b = 255 - b;
        p = (a << 24) | (r << 16) | (g << 8) | b;
        *body = p;
        //env->ReleaseIntArrayElements(oneDim, body, 0);
    }
}
}

Java类:

int pixels[][] = ImageConverter.converToArrayOfPixels(bufferedImage);
    CppDllLibrary tst = new CppDllLibrary();
    tst.convertToNegative(pixels,pixels[0].length,pixels.length);

0 个答案:

没有答案