英文:
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 = "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()));
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论