java - Android- 将 ARGB_8888 位图转换为 3BYTE_BGR

标签 java android image bitmap bytebuffer

我通过这样做得到我的 ARGB_8888 位图的像素数据:

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) 中的数据转换为每个像素存储在三个字节 (BGR) 中的数据。
感谢您的帮助!

最佳答案

免责声明:使用 Android 位图 API 可能有更好/更简单/更快的方法,但我不熟悉它。如果您想沿着开始的方向前进,请修改以下代码以将 4 字节 ARGB 转换为 3 字节 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;
}

关于java - Android- 将 ARGB_8888 位图转换为 3BYTE_BGR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086568/

相关文章:

android - 制作只有图像而没有背景的 Android 图标?

c# - 动态创建图像 - 当前上下文中不存在

iphone - 如何将图像放入此 UIPickerView 中?

c# - C++ 模板和 Java/C# 泛型之间的区别是什么?限制是什么?

java - 使用 boolean 值从用户字符串 java 输入验证

java - 我的应用程序打开后一直崩溃

android - 从 contentprovider 中删除多行

java - onListitemClick 从未使用过

java - Spring Security 国际化消息不会被覆盖

android - 如何在从 json 解析的 ListView 上使用 onItemclick