Android Jetpack CameraX method createSurfaceProvider() not found
Asked Answered
P

4

19

I ran into a problem when working with CameraX on the tutorial from the android documentation. I can't find a method createSurfaceProvider()

I am using these libraries: camerax_version = "1.0.0-beta10"

CODE:

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

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

        ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
                ProcessCameraProvider.getInstance(requireContext());

        cameraProviderFuture.addListener(() -> {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindPreview(cameraProvider);

            } catch (InterruptedException | ExecutionException e) {

            }
        }, ContextCompat.getMainExecutor(requireContext()));
    }

    private void bindPreview(ProcessCameraProvider cameraProvider) {
        Preview preview = new Preview.Builder().build();

        imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .build();

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

        Camera camera = cameraProvider.bindToLifecycle(
                ((LifecycleOwner) this),
                cameraSelector,
                preview,
                imageCapture);

        preview.setSurfaceProvider(
                previewView.createSurfaceProvider(camera.getCameraInfo()));
            
                            ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
    }
Progestational answered 6/10, 2020 at 15:43 Comment(1)
it was changed to getSurfaceProvider, and you can call it like .surfaceProvider in Kotlin. For example, somePreviewView.surfaceProvider.Infrangible
B
35

Just followed the latest tutorial and faced with same issue. But if you simply call the property surfaceProvider of your preview everything will work.

val preview = Preview.Builder() .build() .also { it.setSurfaceProvider(viewFinder.surfaceProvider) }

Boon answered 7/10, 2020 at 9:45 Comment(0)
S
9

Just replace

preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.getCameraInfo()));

with

preview.setSurfaceProvider(previewView.getSurfaceProvider());
Salic answered 19/10, 2021 at 12:7 Comment(0)
S
0

preview.setSurfaceProvider(mBinding.cameraPreviewView.getSurfaceProvider());

Septennial answered 15/6, 2023 at 10:56 Comment(0)
D
-3

Change the gradle dependency to this : implementation "androidx.camera:camera-camera2:1.0.0-rc01" It should work

Delve answered 20/1, 2021 at 3:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.