使用CameraX在Android相机应用程序中保存YUV_420_888格式图像

3

我希望将图像流(最好是30fps)保存到设备上。我看到了这篇文章

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

同时,如果有人能建议任何保存一系列图像的方向,我将不胜感激。

目前为止,我只通过单击按钮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
@samabcde,“保存图像流”主要是指以30fps的速度将图像保存到磁盘上。目前,我只能一次保存一张图像到磁盘上。 - karatuno
你能找到任何解决方案吗? - momvart
1个回答

1
我该如何在不将其编码为JPEG的情况下使用cameraX转储YUV_420_888格式的图像? 您可以使用另一版本的ImageCapture.takePicture(),它返回一个内存中的YUV_420_888格式图像,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(),您可以使用 API(例如 Handler.postDelayed(Runnable, Long))控制图像捕获速率。


在 onCaptureSuccess 方法中,我该如何将图像保存到磁盘上?这个 https://dev59.com/Z47da4cB1Zd3GeqP7wu9 的方式正确吗? - karatuno
2
这里的imageProxy对象是JPEG格式,而不是像ImageAnalysis用例中的YUV_420_888。我在onCaptureSuccess内部运行了一个内联调试器,并且'image.format'返回'256'而不是'35'。我发现原因是我不能再使用[YuvToRgbConverter](https://github.com/android/camera-samples/blob/3730442b49189f76a1083a98f3acf3f5f09222a3/CameraUtils/lib/src/main/java/com/example/android/camera/utils/YuvToRgbConverter.kt),因为它在'imageToByteBuffer()'方法中断言了YUV_420_888。 - topher217
1
我没有看到任何构建器功能来设置图像格式。 - topher217

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接