Android Jetpack CameraX 方法 createSurfaceProvider() 未找到

2022-09-02 20:17:36

在Android文档中的教程中使用CameraX时,我遇到了一个问题。我找不到方法createSurfaceProvider()

我正在使用这些库:camerax_version = "1.0.0-beta10"

验证码

@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()));
            
                            ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
    }

答案 1

只是按照最新的教程,面对同样的问题。但是,如果您只是调用预览的属性表面提供程序,则一切都将正常工作。

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


答案 2

只需更换

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

preview.setSurfaceProvider(previewView.getSurfaceProvider());

推荐