Android CameraX 图像旋转

标签 android android-camera2 android-orientation image-rotation android-camerax

我关注了 Google CameraX code lab实现自定义相机。相机预览很好,但是当我在图像捕获图像旋转后拍摄图像时。我正在以纵向模式拍摄图像,但保存的图像是横向的。这是配置相机的方法

private fun startCamera() {

    val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

    cameraProviderFuture.addListener(Runnable {
        // Used to bind the lifecycle of cameras to the lifecycle owner
        val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

        // Preview
        val preview = Preview.Builder()
            .setTargetRotation(this.windowManager.defaultDisplay.rotation)
            .build()
            .also {
                it.setSurfaceProvider(viewFinder.createSurfaceProvider())
            }

        imageCapture = ImageCapture.Builder()
            .setTargetRotation(this.windowManager.defaultDisplay.rotation)
            .build()

        val imageAnalyzer = ImageAnalysis.Builder()
            .build()
            .also {
                it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma ->
                    Log.d(TAG, "Average luminosity: $luma")
                })
            }

        // Select back camera as a default
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        try {
            // Unbind use cases before rebinding
            cameraProvider.unbindAll()

            // Bind use cases to camera
            cameraProvider.bindToLifecycle(
                this, cameraSelector, preview, imageCapture, imageAnalyzer)

        } catch(exc: Exception) {
            Log.e(TAG, "Use case binding failed", exc)
        }

    }, ContextCompat.getMainExecutor(this))
}
这是捕获图像的方法:
private fun takePhoto() {
    val imageCapture = imageCapture ?: return

    // Create time-stamped output file to hold the image
    val photoFile = File(
        outputDirectory,
        SimpleDateFormat(FILENAME_FORMAT, Locale.US
        ).format(System.currentTimeMillis()) + ".jpg")

    // Create output options object which contains file + metadata
    val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()

    // Set up image capture listener, which is triggered after photo has
    // been taken
    imageCapture.takePicture(
        outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback {
            override fun onError(exc: ImageCaptureException) {
                Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
            }

            override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                val savedUri = Uri.fromFile(photoFile)
                val msg = "Photo capture succeeded: $savedUri"
                val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, savedUri)
                ivCapturedImage.setImageBitmap(bitmap)
                setCaptureUI(false)
                Log.d(TAG, msg)
            }
        })
}
使用EXIF拍摄后我需要自己旋转图像还是我可以在配置相机时修复它?

最佳答案

默认情况下,ImageCapture 将捕获的方向设置为显示旋转。如果图像保存到磁盘,旋转将在 EXIF 中。
您的设备是否处于锁定纵向模式?在这种情况下,显示旋转与设备的方向不匹配,您需要自己设置目标旋转。例子。

// The value is whatever the display rotation should be, if the device orientation is not locked.
imageCapture.setTargetRotation(...) 
或者,您可以简单地使用 LifecycleCameraController API。它为您处理轮换,并以所见即所得的方式使所有用例保持一致。

关于Android CameraX 图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63921260/

相关文章:

android - 即使我正在使用 MenuItemCompat.setOnActionExpandListener 我的应用程序也会崩溃,建议使用 MenuItemCompat.setOnActionExpandListener

java - Android 从静态上下文中获取 fragment 中的资源

android - 仅在发布版本中将 screenOrientation 设置为 "portrait"

java - Camera2 api 将相机切换为正常和黑白

Android:Camera2 API 仅在 Google Pixel 2 和 2 XL 设备上崩溃

Android 方向改变调用 Switch.onChange

Android - 使用相机 Intent (ACTION_VIDEO_CAPTURE) 显示不同的方向

android - OpenGLES 2.0 Phong 着色器奇怪的结果,在启用纹理时使我的对象透明!

android - 自定义布局在用作 ListView 项目时不会调整子项的大小

java - cameraIdList 中缺少相机