Как переключить предварительный просмотр камеры реактивного ранца с задней на переднюю и наоборот?

Когда я пытаюсь переключить предварительный просмотр камеры с ЗАДНИЙ на ПЕРЕДНИЙ, мой экран зависает, и если я сворачиваю экран и перезапускаю то же самое, то предварительный просмотр камеры работает отлично. ниже код камеры.

private fun startCamera() {
    CameraX.unbindAll()
    val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
    val screenSize = Size(metrics.widthPixels, metrics.heightPixels)
    val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)

    val previewConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        setTargetResolution(screenSize)
        setTargetAspectRatio(screenAspectRatio)
        setTargetRotation(windowManager.defaultDisplay.rotation)
        setTargetRotation(viewFinder.display.rotation)
    }.build()

    preview = Preview(previewConfig)
    preview.setOnPreviewOutputUpdateListener {
        viewFinder.surfaceTexture = it.surfaceTexture
        updateTransform()
    }


    // Create configuration object for the image capture use case
    val imageCaptureConfig = ImageCaptureConfig.Builder()
        .apply {
            setLensFacing(lensFacing)
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(viewFinder.display.rotation)
            setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
        }.build()

    // Build the image capture use case and attach button click listener
    imageCapture = ImageCapture(imageCaptureConfig)

    //for recording the video
    val videoCaptureConfig = VideoCaptureConfig.Builder().apply {
        setLensFacing(lensFacing)
        setTargetAspectRatio(screenAspectRatio)
        setTargetRotation(viewFinder.display.rotation)
    }.build()

    videoCapture = VideoCapture(videoCaptureConfig)

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

и код updateTransform

private fun updateTransform() {
    val matrix = Matrix()

    // Compute the center of the view finder
    val centerX = viewFinder.width / 2f
    val centerY = viewFinder.height / 2f

    // Correct preview output to account for display rotation
    val rotationDegrees = when (viewFinder.display.rotation) {
        Surface.ROTATION_0 -> 0
        Surface.ROTATION_90 -> 90
        Surface.ROTATION_180 -> 180
        Surface.ROTATION_270 -> 270
        else -> return
    }
    matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)

    // Finally, apply transformations to our TextureView
    viewFinder.setTransform(matrix)
}

Я пытаюсь переключиться между предварительным просмотром камеры:

 lensFacing = if (lensFacing == CameraX.LensFacing.BACK) {
            CameraX.LensFacing.FRONT
        } else
            CameraX.LensFacing.BACK
        try {
            CameraX.getCameraWithLensFacing(lensFacing)
            CameraX.unbind(preview, imageCapture, videoCapture)
            startCamera()
        } catch (e: Exception) {
            e.printStackTrace()
        }

и после вызова приведенного выше кода при нажатии кнопки предварительный просмотр замораживается.


person Maraj Hussain    schedule 24.07.2019    source источник


Ответы (1)


Возможно, вам придется снова добавить textureView в обратный вызов onUpdated. См. это, https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/camera/integration-tests/coretestapp/src/main/java/androidx/camera/integration/core/CameraXActivity.java#168

Дайте мне знать, если это работает.

person Wasim Ansari    schedule 01.08.2019