java - Android相机应用程序使用CameraX将图像保存为YUV_420_888格式

标签 java android android-camera android-camerax

我想将图像流(最好是 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();
            }
        });
    }
});

最佳答案

How do I dump images in YUV_420_888 format using cameraX without encoding it as JPEG?

您可以使用 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
   }
})

I'll be glad I could any direction in saving burst of images.

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

关于java - Android相机应用程序使用CameraX将图像保存为YUV_420_888格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62373908/

相关文章:

java - Heroku - 致命 : password authentication failed for user <>

java - 我有一个包含 14 个值的 ArrayList,但出现 IndexOutOfBounds 异常。我检查它们是否为空并打印了值

android - 从android中的10,000个项目列表中选择任何项目

android - 为什么 TextView.setText 会导致封闭的 ScrollView 滚动?

android - Mobile Vision API 的面部跟踪(而不是检测)可以独立于其 CameraSource 使用吗?

Android 相机 API 应用程序在 onResume 时崩溃

java - 创建视频文件的可播放预览

java - Mpandroidchart(水平图表)设置每个条形图不同的颜色

android - 为什么 Android BluetoothDevice.conenctGatt 在不使用时需要上下文

android - 检测另一个应用程序何时尝试使用相机