如何修复CameraX捕获的图像旋转?

时间:2019-11-07 12:42:30

标签: android android-camerax

我尝试使用新的CameraX api,但出现以下错误:捕获图像时,该图像以错误的旋转方式存储。例如,我以纵向拍摄,而图像以横向拍摄。

有我的代码:

private fun startCamera() {
    val previewConfig = PreviewConfig.Builder().apply {
        setTargetResolution(Size(textureView.width, textureView.height))
        setTargetRotation(textureView.display.rotation)
    }.build()

    val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
        setCaptureMode(CaptureMode.MIN_LATENCY)
        setTargetAspectRatio(RATIO_4_3)
        setTargetRotation(textureView.display.rotation)
    }.build()
    imageCapture = ImageCapture(imageCaptureConfig)

    val preview = Preview(previewConfig)
    preview.setOnPreviewOutputUpdateListener { previewOutput ->
        removeView(textureView)
        addViewMatchParent(textureView, position = 0)
        textureView.surfaceTexture = previewOutput.surfaceTexture
        textureView.updateTransformForCameraFinderView()
    }
    (context as? LifecycleOwner)?.let { lifecycleOwner ->
        CameraX.bindToLifecycle(lifecycleOwner, preview, imageCapture)
    }
}

private fun capturePhoto() {
    tempImageFile = generateTmpFile(false)
    val executor = Executor { it.run() }
    imageCapture.takePicture(tempImageFile!!, executor, object : OnImageSavedListener {
        override fun onError(error: ImageCaptureError, message: String, exc: Throwable?) {
            exc?.printStackTrace()
        }

        override fun onImageSaved(photoFile: File) {
            post {
                // load image into ImageView by Glide
                showCapturedPhotoPreview(photoFile)
            }
        }
    })
}

请告诉我如何解决?

P.S。我试图找到解决方案,所以先不要复制粘贴类似的东西

更新:我尝试像在this sample中那样操作我的CameraView,但在我的情况下,我可以这样做-不可以)

1 个答案:

答案 0 :(得分:0)

尝试:

    val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
        setCaptureMode(CaptureMode.MIN_LATENCY)
        setTargetAspectRatio(RATIO_4_3)
        // play with this line!
        setTargetRotation(Surface.ROTATION_0)
        setTargetRotation(textureView.display.rotation)
    }.build()

我选择了可​​以再次解决我问题的行:

setTargetRotation(Surface.ROTATION_0)