Is Android's ARGB_8888 Bitmap internal format always RGBA?

帅比萌擦擦* 提交于 2019-12-12 12:48:51

问题


I am trying to create a Bitmap in Android using the Bitmap.Config.ARGB_8888 after I have received the bytes from an external source. As I understand the fastest way to set raw bytes in a Bitmap (without using JNI) is by using the copyPixelsFromBuffer() method, however the question arises regarding the correct order of the bytes in that buffer.

After some trial and error, and despite the fact that Config.ARGB_8888 suggests a correct order of ARGB it seems that the internal format used by Bitmap is RGBA. You can test this behavior using the following method inside an Activity i.e. in onCreate() (I have tested it in Android 4.4.4, it is true that the method tests copyPixelsToBuffer() but according to my tests copyPixelsFromBuffer() behaves the same):

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();
}

My question is: Is this internal format documented anywhere? If not, then how do we know if the above code won't break in future versions of Android? Or perhaps there is some other suggested approach to copy raw bytes in a Bitmap?


回答1:


Regarding the documentation:

https://developer.android.com/reference/android/graphics/Color.html

There are several usages of namely RGBA model.

For instance pack(int color) method. The specified ARGB color int to an RGBA color long in the sRGB color space.

You can try to find 'RGBA' word in the page I gave. Most of base concepts are used RGBA model.



来源:https://stackoverflow.com/questions/44500726/is-androids-argb-8888-bitmap-internal-format-always-rgba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!