Java中的第一個「字節」是8位(有符號)而不是32位。 我不使用nio.ByteBufffer(雖然可以)。 byte []數組的工作更好,更簡單。
在AndroidBitmap.java
public class AndroidBitmap {
public static native void updateBitmap(android.graphics.Bitmap bitmap, byte[] data, int w, int h, int bpp);
}
在AndroidBitmap.c
jboolean Java_jni_AndroidBitmap_updateBitmap(JNIEnv* env, jobject that, jobject bitmap, jbyteArray data, jint w, jint h, jint bpp) {
jbyte* a = (*env)->GetByteArrayElements(env, data, NULL);
jsize bytes = (*env)->GetArrayLength(env, data);
AndroidBitmapInfo info = {0};
int r = AndroidBitmap_getInfo(env, bitmap, &info);
if (r != 0) {
// … "AndroidBitmap_getInfo() failed ! error=%d", r
return false;
}
int width = info.width;
int height = info.height;
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888 && info.format != ANDROID_BITMAP_FORMAT_A_8) {
// "Bitmap format is not RGBA_8888 or A_8"
return false;
}
int bytesPerPixel = info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ? 4 : 1;
void* pixels = null;
r = AndroidBitmap_lockPixels(env, bitmap, &pixels);
if (r != 0) {
// ..."AndroidBitmap_lockPixels() failed ! error=%d", r
return false;
}
if (w == width && h == height && bytesPerPixel == bpp) {
memcpy(pixels, a, width * height * bytesPerPixel);
} else if (bytesPerPixel == 4 && bpp == 1) {
grayscaleToRGBA(pixels, &info, data, w, h);
} else {
assertion(bytesPerPixel == 4 && bpp == 1, "only grayscale -> RGBA is supported bytesPerPixel=%d bpp=%d", bytesPerPixel, bpp);
}
AndroidBitmap_unlockPixels(env, bitmap);
(*env)->ReleaseByteArrayElements(env, data, a, 0);
return true;
}
希望這有助於。
來源
2014-11-02 23:19:59
Leo
從您的本機代碼執行十六進制緩衝區轉儲以確保像素數據實際到達那裏。 – fadden 2014-11-03 16:12:20