用c代码填充java的字节数组

hof1towb  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(198)

我有一个需要从c运行的代码(使用c库,加上它的性能是关键的,涉及大量的循环迭代)。代码需要解码图像并对其运行一些操作。最终结果应该是android/java的位图。
由于高分辨率和需要显示图片而不需要任何缩放,因此会产生性能问题。
我已经预先分配了位图,我正在重用它,以避免过多的分配和垃圾收集。目前我已经完成了概念验证所需的所有工作,我是这样做的:

public static void fillNativeBitmap(String src, Bitmap bmp) {
    byte[] nativeBytes = getNativeBytesARGB8(src);
    bmp.copyPixelsFromBuffer(ByteBuffer.wrap(nativeBytes));
}

JNIEXPORT jbyteArray JNICALL
notImportantForQuestion_getNativeBytesARGB8(
  JNIEnv *env,
  jclass clazz,
  jstring src
) {
    char *src_file;
    unsigned char *imptr;
    src_file = (*env)->GetStringUTFChars(env, src, NULL);
    const int total_pixels = xxx;
    static unsigned char mptr[xxx];
    decodeOneStep(src_file, &imptr); //uses lodepng to decode bitmap file

    doStuffWithImage(imptr, mptr);

    jbyteArray result = (*env)->NewByteArray(env, total_pixels); //here - unnececeary allocation for GC?

    (*env)->SetByteArrayRegion(env, result, 0, total_pixels, mptr);
    (*env)->ReleaseStringUTFChars(env, src, src_file);
    free(imptr); //I can live with that

    return result;
}

我想重用java的位图缓冲区,把它传递给c并在那里填充。然而,从c访问java数组的方法需要指向“iscopy”的指针。访问java数组的副本对我来说根本不起作用,因为我不需要c上下文中的java数据,我只需要c上下文来填充java的数据。
在这种情况下我应该使用什么方法?如何以最佳方式实现它?我试图优化性能和稳定性,后者包括尽可能减少分配数量。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题