【问题标题】:Android ARGB_8888 to RGBAndroid ARGB_8888 转 RGB
【发布时间】:2021-09-18 01:10:14
【问题描述】:

我正在使用cameraX 来分析来自相机的图像。

我得到了 YUV_420_888 格式的图像,并设法将其转换为 ARGB_8888

我需要每个像素都在 3 个字节上,精度为 8 位,值从 0...255

这就是我创建位图的方式。

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

有没有办法从 ARGB_8888 中删除 Alpha 通道?

【问题讨论】:

标签: android android-camerax rgba argb


【解决方案1】:

有多种方法可以做到这一点,我认为使用 OpenCV 可能会更有效/更快。 这是Java方式:

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 + 2]; // B 
    pixels[i * 3 + 1] = temp[i * 4 + 1]; // G 
    pixels[i * 3 + 2] = temp[i * 4 + 0]; //R

   // Alpha is discarded
}

return pixels;
}

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多