Android的ARGB_8888位图内部格式是否始终为RGBA? [英] Is Android's ARGB_8888 Bitmap internal format always RGBA?

查看:739
本文介绍了Android的ARGB_8888位图内部格式是否始终为RGBA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从外部来源收到字节后,我试图使用Bitmap.Config.ARGB_8888在Android中创建Bitmap.据我了解,在Bitmap中设置原始字节(不使用JNI)的最快方法是使用copyPixelsFromBuffer()方法,但是出现了有关该缓冲区中字节正确顺序的问题.

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.

经过反复试验,尽管Config.ARGB_8888提示ARGB的顺序正确,但Bitmap使用的内部格式似乎是RGBA.您可以在Activity中使用以下方法测试此行为,即在onCreate()中(我已经在Android 4.4.4中对其进行了测试,确实该方法可以测试copyPixelsToBuffer(),但是根据我的测试copyPixelsFromBuffer()的行为相同):

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

我的问题是:此内部格式是否在任何地方都有记录?如果没有,那么我们如何知道上面的代码在将来的Android版本中不会中断?也许还有其他建议的方法将原始字节复制到Bitmap中?

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?

推荐答案

关于文档:

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

RGBA模型有几种用法.

There are several usages of namely RGBA model.

例如pack(int color)方法.在sRGB颜色空间中,将指定的ARGB颜色int转换为长于RGBA的颜色.

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

您可以尝试在我给的页面中找到"RGBA"字样.大多数基本概念都使用RGBA模型.

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

这篇关于Android的ARGB_8888位图内部格式是否始终为RGBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆