Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CameraX with PreviewView?

I can't find out how to use camerax with previewview.

like image 336
Mohamed Ben Romdhane Avatar asked Feb 19 '20 13:02

Mohamed Ben Romdhane


People also ask

How to CAPTURE image in CameraX in android?

To save the captured image to a file, use ImageCapture. takePicture(outputFileOptions, executor, OnImageSavedCallback) . The OutputFileOptions define where to store the captured image and what metadata to save with it. ImageCapture can write the captured image to a File , an OutputStream or MediaStore .

What is PreviewView?

This class manages the preview Surface 's lifecycle. It internally uses either a TextureView or SurfaceView to display the camera feed, and applies required transformations on them to correctly display the preview, this involves correcting their aspect ratio, scale and rotation.

How do you pause Camerax?

stopRepeating() and the TextureView will stop getting input from the camera.


2 Answers

Here is an example of how to use Camerax with PreviewView, the code uses the latest versions of CameraX, meaning camera-camera2 version 1.1.0-beta01, and camera-view version 1.1.0-beta01.

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        previewView = view.findViewById(R.id.preview_view);
  

        setCameraProviderListener();

    }
private void setCameraProviderListener() {
        ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
                ProcessCameraProvider.getInstance(requireContext());
        cameraProviderFuture.addListener(() -> {

            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindPreview(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {
                // No errors need to be handled for this Future
                // This should never be reached
                e.printStackTrace();
            }
        }, ContextCompat.getMainExecutor(requireContext()));
    }
 private void bindPreview(ProcessCameraProvider cameraProvider) {
        
        imageAnalyser();
        imageCapture();

        CameraSelector cameraSelector =
                new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();


        Preview preview = new Preview.Builder().build();

        preview.setSurfaceProvider(previewView.getSurfaceProvider());

        ViewPort viewPort = previewView.getViewPort();

        if (viewPort != null) {
            UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
                    .addUseCase(preview)
                    .addUseCase(imageAnalyzer)
                    .addUseCase(imageCapture)
                    .setViewPort(viewPort)
                    .build();

            cameraProvider.unbindAll();
            Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, useCaseGroup);
            CameraControl cameraControl = camera.getCameraControl();
            cameraControl.setLinearZoom((float)0.3);
        }


    }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.camera.view.PreviewView
        android:id="@+id/preview_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/Yellow"
        android:layout_gravity="center" />

</LinearLayout>
like image 51
Mohamed Ben Romdhane Avatar answered Oct 17 '22 13:10

Mohamed Ben Romdhane


Here is an article that explains PreviewView and how to use it. It's valid up to version 1.0.0-alpha10 of camera-view, I'll try to keep it up to date.

like image 24
Husayn Hakeem Avatar answered Oct 17 '22 12:10

Husayn Hakeem