Android Jetpack CameraX 方法 createSurfaceProvider() 找不到。

huangapple go评论116阅读模式
英文:

Android Jetpack CameraX method createSurfaceProvider() not found

问题

@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()));
}
英文:

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 = &quot;1.0.0-beta10&quot;

CODE:

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

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

        ListenableFuture&lt;ProcessCameraProvider&gt; cameraProviderFuture =
                ProcessCameraProvider.getInstance(requireContext());

        cameraProviderFuture.addListener(() -&gt; {
            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

得分: 35

刚刚按照最新的教程进行操作,遇到了相同的问题。
但是,如果你只是简单地调用预览 preview 的 surfaceProvider 属性,一切都会正常工作。

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

英文:

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)
> }

答案2

得分: 8

仅替换

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

preview.setSurfaceProvider(previewView.getSurfaceProvider());
英文:

Just replace

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

with

preview.setSurfaceProvider(previewView.getSurfaceProvider());

答案3

得分: 0

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

英文:

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

答案4

得分: -2

将 Gradle 依赖项更改为:
implementation "androidx.camera:camera-camera2:1.0.0-rc01"
应该可以正常工作。

英文:

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

huangapple
  • 本文由 发表于 2020年10月6日 23:43:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64229274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定