CameraX - Не найдено поддерживаемых комбинаций поверхностей для устройства камеры - Id: null.Может пытаться связать слишком много вариантов использования - PullRequest
0 голосов
/ 27 сентября 2019

Я использую 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)
...