2

我尝试使用新的 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)
            }
        }
    })
}

请给我建议我该如何解决?

PS我试图找到解决方案,所以不要先复制粘贴看起来类似的东西)

更新:我尝试像在这个示例中那样做我的 CameraView但在他们的情况下它可以工作,在我的 - 不)

4

1 回答 1

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)
于 2019-11-07T15:10:22.167 回答