5

我在我的项目中使用 CameraX,然后我会看到这个奇怪的错误弹出并导致我的应用程序崩溃。这发生在某些设备上 - 不是全部。

我像这样启动我的相机:

private fun startCamera() {
    if (!isAdded) {
        return
    }

    val textureView = rootView?.findViewById<TextureView>(R.id.textureView)

    if (textureView != null) {
        val display = (requireActivity().getSystemService(WINDOW_SERVICE) as WindowManager).defaultDisplay

        if (metrics == null) {
            metrics = DisplayMetrics().also { display.getRealMetrics(it) }
        }

        config = CameraConfiguration(
            aspectRatio = Rational(metrics!!.widthPixels, metrics!!.heightPixels),
            resolution = Size(metrics!!.widthPixels, metrics!!.heightPixels)
        )

        CameraX.unbindAll()
        CameraX.bindToLifecycle(
            this,
            buildPreviewUseCase(),
            buildImageAnalysisUseCase(),
            buildImageCaptureUseCase()
        )
    }
}

用例是这样构建的:

private fun buildPreviewUseCase(): Preview {
    val previewConfig = PreviewConfig.Builder()
        .setTargetAspectRatio(config.aspectRatio)
        .setTargetResolution(config.resolution)
        .setTargetRotation(Surface.ROTATION_0)
        .setLensFacing(config.lensFacing)
        .build()

    return AutoFitPreviewBuilder.build(previewConfig, cameraTextureView)
}

private fun buildImageCaptureUseCase(): ImageCapture {
    val captureConfig = ImageCaptureConfig.Builder()
        .setTargetAspectRatio(config.aspectRatio)
        .setTargetRotation(Surface.ROTATION_0)
        .setTargetResolution(config.resolution)
        .setCaptureMode(config.captureMode)
        .setLensFacing(config.lensFacing)
        .build()

    val capture = ImageCapture(captureConfig)

    takePhotoButton.setOnClickListener {

        capture.takePicture(object : ImageCapture.OnImageCapturedListener() {
            override fun onCaptureSuccess(imageProxy: ImageProxy, rotationDegrees: Int) {
                ...
            }

            override fun onError(useCaseError: ImageCapture.UseCaseError?, message: String?, cause: Throwable?) {
                ...
            }
        })
    }

    return capture
}

private fun buildImageAnalysisUseCase(): ImageAnalysis {
    val analysisConfig = ImageAnalysisConfig.Builder().apply {
        val analyzerThread = HandlerThread("Analyzer").apply { start() }
        analyzerHandler = Handler(analyzerThread.looper)

        setCallbackHandler(analyzerHandler!!)
        setTargetAspectRatio(config.aspectRatio)
        setTargetRotation(Surface.ROTATION_0)
        setLensFacing(config.lensFacing)
        setTargetResolution(config.resolution)
        setImageReaderMode(config.readerMode)
        setImageQueueDepth(config.queueDepth)
    }.build()

    val analysis = ImageAnalysis(analysisConfig)
    analysis.analyzer = ImageRecognitionAnalyzer(viewModel)

    return analysis
}

有没有办法解决这个问题,有人可以解释到底发生了什么吗?提前致谢。

错误:

java.lang.IllegalArgumentException: No supported surface combination is found for camera device - Id : null.  May be attempting to bind too many use cases.
    at androidx.camera.camera2.impl.Camera2DeviceSurfaceManager.getSuggestedResolutions(Camera2DeviceSurfaceManager.java:180)
    at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:449)
    at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:144)
    at com.s.v.f.i.views.CameraFragment.startCamera(CameraFragment.kt:554)
4

0 回答 0