Android-将ARGB_8888位图转换为3BYTE_BGR [英] Android- convert ARGB_8888 bitmap to 3BYTE_BGR

查看:92
本文介绍了Android-将ARGB_8888位图转换为3BYTE_BGR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过执行以下操作,我获得了ARGB_8888位图的像素数据:

I get the pixel data of my ARGB_8888 bitmap by doing this:

public void getImagePixels(byte[] pixels, Bitmap image) {
    // calculate how many bytes our image consists of
    int bytes = image.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
    image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer

    pixels = buffer.array(); // Get the underlying array containing the data.
}

但是,我想将每个像素存储在四个字节(ARGB)上的数据转换为每个像素存储在3个字节( BGR )上的数据.
任何帮助表示赞赏!

But, I would like to convert this data, in which each pixel is stored on four bytes (ARGB), to where each pixel is stored on 3 bytes (BGR).
Any help is appreciated!

推荐答案

免责声明:使用Android Bitmap API可能会有更好/更便捷的方法,但是我对此并不熟悉.如果您想沿着开始的方向前进,请修改以下代码,将4字节ARGB转换为3字节BGR

Disclaimer: There could be better/easier/faster ways of doing this, using the Android Bitmap API, but I'm not familiar with it. If you want to go down the direction you started, here's your code modified to convert 4 byte ARGB to 3 byte BGR

public byte[] getImagePixels(Bitmap image) {
    // calculate how many bytes our image consists of
    int bytes = image.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
    image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer

    byte[] temp = buffer.array(); // Get the underlying array containing the data.

    byte[] pixels = new byte[(temp.length / 4) * 3]; // Allocate for 3 byte BGR

    // Copy pixels into place
    for (int i = 0; i < (temp.length / 4); i++) {
       pixels[i * 3] = temp[i * 4 + 3];     // B
       pixels[i * 3 + 1] = temp[i * 4 + 2]; // G
       pixels[i * 3 + 2] = temp[i * 4 + 1]; // R

       // Alpha is discarded
    }

    return pixels;
}

这篇关于Android-将ARGB_8888位图转换为3BYTE_BGR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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