1

When I try to switch the camera preview from BACK to FRONT my screen getting freeze and if I minimize the screen and restart the same then camera preview work perfectly. below is the camera code.

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

and the updateTransform code is

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

I have try to switch between the camera preview is :

 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()
        }

and after calling the above code on button click preview getting freez.

4

1 回答 1

0

您可能需要在 onUpdated 回调中再次添加 textureView。参考这个, 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

让我知道它是否有效。

于 2019-08-01T03:45:03.963 回答