【问题标题】:Android Camera App using CameraX to save images in YUV_420_888 formatAndroid Camera App 使用 CameraX 将图像保存为 YUV_420_888 格式
【发布时间】:2020-10-04 00:28:59
【问题描述】:

我想将图像流(最好是 30fps)保存到设备。我遇到了这个post

如何使用cameraX 转储YUV_420_888 格式的图像而不用JPEG 编码?

另外,如果有人能建议任何保存突发图像的方向,我会很高兴。

到目前为止,我只是通过按钮的onclick 保存图像

findViewById(R.id.capture_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                "Image_" + System.currentTimeMillis() + ".jpg");

        imageCapture.takePicture(file, new ImageCapture.OnImageSavedListener() {
            @Override
            public void onImageSaved(@NonNull File file) {
                Toast.makeText(CameraCaptureActivity.this, "Photo saved as " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                Toast.makeText(CameraCaptureActivity.this, "Couldn't save photo: " + message, Toast.LENGTH_SHORT).show();
                if (cause != null)
                    cause.printStackTrace();
            }
        });
    }
});

【问题讨论】:

  • 请详细说明“保存图像流”和“突发图像”(图像数量有多大?)。您目前的方法有什么问题? (无法转储为 YUV_420_888 格式?还是只保存一张图片?)
  • @samabcde “保存图像流”我的意思主要是在磁盘上以 30fps 的速度保存图像。目前,我一次只将图像保存到磁盘,

标签: java android android-camera android-camerax


【解决方案1】:

如何在没有使用 cameraX 的情况下转储 YUV_420_888 格式的图像 将其编码为JPEG?

您可以使用另一个版本的 ImageCapture.takePicture(),它返回 YUV_420_888 格式的 im-memory 图像,ImageCapture.takePicture(Executor, OnImageCaptureCallback)

imageCapture.takePicture(executor, new ImageCapture.OnImageCapturedCallback() {

   @Override
   public void onCaptureSuccess (ImageProxy imageProxy) {
      // Use the image, then make sure to close it before returning from the method
      imageProxy.close()
   }

   @Override
   public void onError (ImageCaptureException exception) {
      // Handle the exception however you'd like
   }
})

我很高兴能在保存大量图像方面提供任何指导。

连拍图像是以一定的速率连续拍摄的图像。你如何在你的应用中实现它取决于你希望用户体验是什么样的,它可以通过单击单个按钮或长按来触发。主要是你必须多次调用ImageCapture.takePicture(),你可以使用Handler.postDelayed(Runnable, Long)等API控制图像捕获率。

【讨论】:

  • 在 onCaptureSuccess 方法中如何将图像保存到磁盘? stackoverflow.com/questions/31984622/… 这样做对吗?
  • 这里的 imageProxy 对象是 JPEG 而不是 YUV_420_888(与 ImageAnalysis 一样)用例。我在 onCaptureSuccess 中运行一个内联调试器,并且“image.format”返回“256”而不是“35”。我发现这个的原因是我不能再使用 [YuvToRgbConverter=(github.com/android/camera-samples/blob/…) 因为它在 imageToByteBuffer() 方法中断言 YUV_420_888
  • 我也没有看到任何构建器功能来设置图像格式。
猜你喜欢
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
相关资源
最近更新 更多