I can't find out how to use camerax with previewview.
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 .
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.
stopRepeating() and the TextureView will stop getting input from the camera.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With