gpt4 book ai didi

Android CameraX 图像旋转

转载 作者:行者123 更新时间:2023-12-04 11:03:03 31 4
gpt4 key购买 nike

我关注了 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com