Android 相机 2 API 图像颜色 space

Android Camera2 API Image Color space

我使用 this Tutorial 学习并尝试了解如何使用 Camera2 API 制作简单的拍照 android 应用程序。我从代码中添加了一些片段,看看你们是否能帮助我理解我的一些问题。

我想知道图像是如何保存的。是 RGB 还是 BGR? 它存储在变量字节中吗?

ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG, 1);


@Override
public void onImageAvailable(ImageReader reader) {
      Image image = null;
      try {
            image = reader.acquireLatestImage();
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.capacity()];
            buffer.get(bytes);
            save(bytes);
      }

图像以 JPEG 格式接收(如第一行中指定)。 Android 对 JPEG 使用 YUV(更准确地说,YCbCr)颜色 space。 Jpeg 大小是可变的,它使用有损压缩进行压缩,您几乎无法控制压缩级别。

通常,您会收到 onImageAvailable()decode this JPEG to receive a Bitmap. You can get pixels of this Bitmap as an int array of packed SRGB 像素的 JPEG 缓冲区。此数组的 格式 将为 ARGB_8888。 您不需要 JNI 即可将其转换为 BGR,请参见 answer.

您可以从 C++ 访问位图对象,请参阅此位图的 ndk/reference/group/bitmap. There you can find the pixel format。如果它是从 JPEG 解码的,您应该期望它是 ANDROID_BITMAP_FORMAT_RGBA_8888.

变量 bytes 包含一个完整的压缩 JPEG 文件。你需要解压缩它才能用它做很多事情,比如 BitmapFactory.decodeByteArray or ImageDecoder(较新的 API 级别)。

它在任何意义上都不是未压缩的 RGB 值数组。如果你想要未压缩的数据,相机API支持YUV_420_888格式,它会给你未压缩的4:2:0 YUV数据;不过仍然不是 RGB。