android bitmap rgba,graphics - Android: bitmap to RGBA and back - Stack Overflow

博客作者在尝试将Android Bitmap转换为RGBA字节数组并回转时遇到颜色显示错误的问题。他们已经编写了两个方法,但结果并不符合预期。作者怀疑可能是因为在处理颜色组件的顺序时出现了混淆,尤其是在从字节数组创建Bitmap时。他们还提到了Java中没有无符号数值可能导致的问题,并寻求帮助解决这个问题。
摘要由CSDN通过智能技术生成

I'm trying to write a couple of methods to convert an Android Bitmap to an RGBA byte array and then back to a Bitmap. The problem is that I don't seem to hit the formula, because the colors are always coming back wrong. I have tried with several different assumptions but to no avail.

So, this is the method to convert from Bitmap to RGBA that I think is fine:

public static byte[] bitmapToRgba(Bitmap bitmap) {

int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];

byte[] bytes = new byte[pixels.length * 4];

bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

int i = 0;

for (int pixel : pixels) {

// Get components assuming is ARGB

int A = (pixel >> 24) & 0xff;

int R = (pixel >> 16) & 0xff;

int G = (pixel >> 8) & 0xff;

int B = pixel & 0xff;

bytes[i++] = (byte) R;

bytes[i++] = (byte) G;

bytes[i++] = (byte) B;

bytes[i++] = (byte) A;

}

return bytes;

}

And this is the method aimed at creating back a bitmap from those bytes that is not working as expected:

public static Bitmap bitmapFromRgba(int width, int height, byte[] bytes) {

int[] pixels = new int[bytes.length / 4];

int j = 0;

// It turns out Bitmap.Config.ARGB_8888 is in reality RGBA_8888!

// Source: https://stackoverflow.com/a/47982505/1160360

// Now, according to my own experiments, it seems it is ABGR... this sucks.

// So we have to change the order of the components

for (int i = 0; i < pixels.length; i++) {

byte R = bytes[j++];

byte G = bytes[j++];

byte B = bytes[j++];

byte A = bytes[j++];

int pixel = (A << 24) | (B << 16) | (G << 8) | R;

pixels[i] = pixel;

}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixels));

return bitmap;

}

That's my last implementation, though I have tried several different ones without success. I'm assuming createBitmap expects ABGR in spite of specifying ARGB_8888 because I have done experiments hardcoding all the pixels to things like:

0xff_ff_00_00 -> got blue

0xff_00_ff_00 -> got green

0xff_00_00_ff -> got red

Anyway maybe that assumption is wrong and a consequence of some other mistaken one before.

I think the main problem may be related to the use of signed numeric values, since there are no unsigned ones in Java (well, there's something in Java 8+ but on one hand I don't think it should be necessary to use these, and on the other it is not supported by older Android versions that I need to support).

Any help will be very appreciated.

Thanks a lot in advance!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值