我正在尝试创建一个Bitmap
在 Android 中使用Bitmap.Config.ARGB_8888
在我从外部源收到字节后。据我所知,在 a 中设置原始字节的最快方法Bitmap
(不使用JNI
)是通过使用copyPixelsFromBuffer()
方法,但是出现了关于该缓冲区中字节的正确顺序的问题。
经过一番尝试和错误,尽管事实是Config.ARGB_8888
建议正确的顺序ARGB
看来使用的内部格式Bitmap
is RGBA
。您可以使用以下方法在Activity
即在onCreate()
(我在Android 4.4.4中测试过,确实该方法测试copyPixelsToBuffer()
但根据我的测试copyPixelsFromBuffer()
行为相同):
private void testBitmap() {
// one pixel bitmap
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
// as per javadoc, the int value is in ARGB format, so A=0xFF, R=0x11, G=0x22, B=0x33
bitmap.setPixel(0, 0, 0xFF112233);
ByteBuffer buffer = ByteBuffer.allocateDirect(4); // 4 bytes for the single pixel
bitmap.copyPixelsToBuffer(buffer);
buffer.position(0);
// prints "Bytes: 0x11 0x22 0x33 0xFF" (RGBA)
System.out.println(String.format(Locale.ENGLISH, "Bytes: %s %s %s %s",
toHexString(buffer.get()),
toHexString(buffer.get()),
toHexString(buffer.get()),
toHexString(buffer.get())
));
}
private static String toHexString(byte b) {
return Integer.toHexString(b & 0xFF).toUpperCase();
}
我的问题是:这种内部格式是否记录在任何地方?如果不是,那么我们如何知道上面的代码在未来的 Android 版本中是否不会被破坏?或者也许还有其他建议的方法来复制原始字节Bitmap
?