我在使用带有 CameraX 的 PreviewView 时遇到了问题?

I got a probelm using PreviewView with CameraX?

即使我将 AspectRatioAspectRatio.RATIO_16_9 更改为 AspectRatio.RATIO_4_3,PreviewView 也会缩小到屏幕的一半!!

@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) {
        
 previewView.setPreferredImplementationMode(PreviewView.ImplementationMode.SURFACE_VIEW);

        Preview preview = new Preview.Builder().setTargetAspectRatio(AspectRatio.RATIO_4_3).
                setTargetRotation(previewView.getDisplay().getRotation())
                .build();


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

        preview.setSurfaceProvider(previewView.createSurfaceProvider());

        cameraProvider.bindToLifecycle(this, cameraSelector,preview);

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

这是我的代码 我已经面临这个问题 2 个月了!! PreviewView 包裹在 LinearLayout 中,黄色作为 background 颜色,但是当相机启动时它只显示在 PreviewView 的上半部分和 PreviewView变黑

下面是我为测试您描述的问题而编写的片段,但我无法重现它。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
        cameraProviderFuture.addListener(Runnable {
        val cameraProvider = cameraProviderFuture.get()
        bindPreview(cameraProvider, unbindAll = false)
        previewView.setOnClickListener {
            bindPreview(cameraProvider)
        }
    }, ContextCompat.getMainExecutor(requireContext()))
}

private var aspectRatio = AspectRatio.RATIO_4_3
private fun getAndUpdateAspectRatio(): Int {
    val currentAspectRation = aspectRatio
    aspectRatio = if (aspectRatio == AspectRatio.RATIO_4_3) {
        AspectRatio.RATIO_16_9
    } else {
        AspectRatio.RATIO_4_3
    }
    return currentAspectRation
}

private fun bindPreview(cameraProvider: ProcessCameraProvider, unbindAll: Boolean = true) {
    val preview = Preview.Builder()
            .setTargetAspectRatio(getAndUpdateAspectRatio())
            .setTargetRotation(previewView.display.rotation)
            .build()

    previewView.preferredImplementationMode = PreviewView.ImplementationMode.SURFACE_VIEW
    preview.setSurfaceProvider(previewView.createSurfaceProvider())

    if (unbindAll) {
        cameraProvider.unbindAll()
    }
    cameraProvider.bindToLifecycle(viewLifecycleOwner, CameraSelector.DEFAULT_BACK_CAMERA, preview)
}

此代码位于 Fragment 中,其布局包含包裹在 LinearLayout 中的 PreviewView。我在 Pixel 3 XL 和 Nokia 1 上测试了这个。你能测试一下,看看它在你的设备上是否失败了吗?

此外,代码使用了最新版本的 CameraX,即 camera-camera2 版本 1.0.0-beta07,以及 camera-view 版本 1.0.0-alpha14。

终于找到了有点棘手的答案

android:hardwareAccelerated="true"