Android 为什么';t CameraX.bindToLifecycle支持1.0.0-alpha05中的三种情况?

Android 为什么';t CameraX.bindToLifecycle支持1.0.0-alpha05中的三种情况?,android,android-camerax,Android,Android Camerax,我正在学习CameraX API,CameraXBasic是一个office示例代码 代码是基于 我添加了videoCaptureConfig,并使用CameraX.bindToLifecycle(viewLifecycleOwner,preview,imageCapture,videoCapture)将其绑定到生命周期。 但我得到以下错误,为什么 java.lang.IllegalArgumentException:找不到摄影机设备支持的曲面组合-Id:0。可能试图绑定过多的用例 代码A @S

我正在学习CameraX API,CameraXBasic是一个office示例代码

代码是基于

我添加了
videoCaptureConfig
,并使用
CameraX.bindToLifecycle(viewLifecycleOwner,preview,imageCapture,videoCapture)将其绑定到生命周期。

但我得到以下错误,为什么

java.lang.IllegalArgumentException:找不到摄影机设备支持的曲面组合-Id:0。可能试图绑定过多的用例

代码A

@SuppressLint("RestrictedApi")
private fun bindCameraUseCases() {

    // Get screen metrics used to setup camera for full screen resolution
    val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
    val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)

    // Set up the view finder use case to display camera preview
    val viewFinderConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        // We request aspect ratio but no resolution to let CameraX optimize our use cases
        setTargetAspectRatio(screenAspectRatio)
        // Set initial target rotation, we will have to call this again if rotation changes
        // during the lifecycle of this use case
        setTargetRotation(viewFinder.display.rotation)
    }.build()

    // Use the auto-fit preview builder to automatically handle size and orientation changes
    preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)


    // Set up the capture use case to allow users to take photos
    val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
        setLensFacing(lensFacing)
        setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
        // We request aspect ratio but no resolution to match preview config but letting
        // CameraX optimize for whatever specific resolution best fits requested capture mode
        setTargetAspectRatio(screenAspectRatio)
        // Set initial target rotation, we will have to call this again if rotation changes
        // during the lifecycle of this use case
        setTargetRotation(viewFinder.display.rotation)
    }.build()

    imageCapture = ImageCapture(imageCaptureConfig)


    // Create a configuration object for the video use case
    val videoCaptureConfig = VideoCaptureConfig.Builder().apply {
        setTargetRotation(viewFinder.display.rotation)
        setTargetAspectRatio(screenAspectRatio)
        setLensFacing(lensFacing)
    }.build()
    videoCapture = VideoCapture(videoCaptureConfig)


    CameraX.bindToLifecycle(viewLifecycleOwner, preview, imageCapture,videoCapture)
}

您正试图向同一摄像机会话添加太多用例。你可以检查一下。要了解摄像头会话的限制,您可以查看。

在切换到视频之前,您可以尝试解除所有用例的绑定(我注意到在仅解除imageCapture用例绑定时,旧手机会出现一些崩溃):

camerax_version=“1.0.0-alpha06”(JAVA)示例

camerax_version=“1.0.0-alpha07”(JAVA)示例


尝试不同的解决方案,它应该支持多达3@HelloCW如果在绑定视频用例之前先解除图像用例的绑定,是否有效?我也有同样的问题,谢谢!我怎样才能解决这些问题呢?我想你一定是被束缚住了。如果您想绑定视频捕获用例,您必须首先解除绑定图像捕获并绑定视频捕获。像相机应用,图像捕获模式,视频捕获模式分开。谢谢!你的意思是我不能同时绑定图像捕获模式和视频捕获模式吗?是的,至少目前是这样。视频用例目前在API中标记为隐藏,处于非常初步的状态,可能会发生更改。似乎cameraX的团队将在2020年初制作视频。最好是等待官方文件。
// Listener for button photo
btnPhoto.setOnClickListener( v -> bindCameraUseCases(0));
// Listener for button video
btnVideo.setOnClickListener( v -> bindCameraUseCases(1));
@SuppressLint("RestrictedApi")
private void bindCameraUseCases(int captureMode) {
    CameraX.unbindAll();

    AspectRatio screenAspectRatio = AspectRatio.RATIO_4_3;

    // Set up the preview use case to display camera preview
    PreviewConfig.Builder previewConfigBuilder = new PreviewConfig.Builder();
    previewConfigBuilder.setLensFacing(lensFacing);
    previewConfigBuilder.setTargetRotation(viewFinder.getDisplay().getRotation());
    previewConfigBuilder.setTargetAspectRatio(screenAspectRatio);
    Preview preview = new Preview(previewConfigBuilder.build());

    preview.setOnPreviewOutputUpdateListener(previewOutput -> {
        ViewGroup parent = (ViewGroup) viewFinder.getParent();
        parent.removeView(viewFinder);
        parent.addView(viewFinder,0);
        viewFinder.setSurfaceTexture(previewOutput.getSurfaceTexture());
    });

    if(captureMode == 0){ // IMAGE
        videoCapture = null;
        // Set up the capture use case to allow users to take photos
        ImageCaptureConfig.Builder imageCaptureConfigBuilder = new ImageCaptureConfig.Builder();
        imageCaptureConfigBuilder.setLensFacing(lensFacing);
        imageCaptureConfigBuilder.setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY);
        imageCaptureConfigBuilder.setTargetRotation(viewFinder.getDisplay().getRotation());
        imageCapture = new ImageCapture(imageCaptureConfigBuilder.build());

        // Apply declared configs to CameraX using the same lifecycle owner
        CameraX.bindToLifecycle(this, preview, imageCapture);
    }else{ // VIDEO
        imageCapture = null;
        // Set up the video capture use case to allow users to take videos
        VideoCaptureConfig.Builder videoCaptureConfigBuilder = new VideoCaptureConfig.Builder();
        videoCaptureConfigBuilder.setLensFacing(lensFacing);
        videoCaptureConfigBuilder.setTargetAspectRatio(screenAspectRatio);
        videoCaptureConfigBuilder.setTargetRotation(viewFinder.getDisplay().getRotation());
        videoCapture = new VideoCapture(videoCaptureConfigBuilder.build());

        // Apply declared configs to CameraX using the same lifecycle owner
        CameraX.bindToLifecycle(this, preview, videoCapture);
    }
}
@SuppressLint("RestrictedApi")
private void bindCameraUseCases(int captureMode) {
    if(cameraProvider != null) cameraProvider.unbindAll();

    // Set up the preview use case to display camera preview
    Preview preview = new Preview.Builder()
            .setTargetAspectRatio(AspectRatio.RATIO_4_3)
            .setTargetRotation(previewView.getDisplay().getRotation())
            .build();

    preview.setPreviewSurfaceProvider(previewView.getPreviewSurfaceProvider());

    if(captureMode == 0){ // IMAGE
        videoCapture = null;
        // Set up the capture use case to allow users to take photos
        imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CaptureMode.MINIMIZE_LATENCY)
                .setTargetRotation(previewView.getDisplay().getRotation())
                .build();

        // Apply declared configs using the same lifecycle owner
        cameraProvider.bindToLifecycle(this,cameraSelector, preview, imageCapture);
    }else{ // VIDEO
        imageCapture = null;
        // Set up the video capture use case to allow users to take videos
        VideoCaptureConfig.Builder videoCaptureConfigBuilder = new VideoCaptureConfig.Builder();
        videoCaptureConfigBuilder.setTargetAspectRatio(AspectRatio.RATIO_4_3);
        videoCaptureConfigBuilder.setTargetRotation(previewView.getDisplay().getRotation());
        videoCapture = new VideoCapture(videoCaptureConfigBuilder.getUseCaseConfig());

        // Apply declared configs using the same lifecycle owner
        cameraProvider.bindToLifecycle(this, cameraSelector,preview, videoCapture);
    }
}