[英]How to save the correct rotated image using camerax Front Camera?
[英]Android CameraX image rotated
我已經按照 Google CameraX 代碼實驗室來實現自定義相機。 相機預覽很好,但是當我在圖像捕獲圖像旋轉后拍攝圖像時。 我正在以縱向模式拍攝圖像,但保存的圖像是橫向的。 這是配置相機的方法
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。 它為您處理旋轉並以所見即所得的方式使所有用例保持一致。
我正在遭受同樣的情況。 我用hacky的方式解決了這個問題。
我的解決辦法是:
fun Bitmap.rotate(degrees: Float): Bitmap {
val matrix = Matrix().apply { postRotate(degrees) }
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
用法 :
imageViewCapturedImage.setImageBitmap(bitmap?.rotate(90F))
我用這個類來旋轉圖像
object CaptureImageHelper {
/**
* This method is responsible for solving the rotation issue if exist. Also scale the images to
* 1024x1024 resolution
*
* @param context The current context
* @param selectedImage The Image URI
* @return Bitmap image results
* @throws IOException
*/
@Throws(IOException::class)
fun handleSamplingAndRotationBitmap(context: Context, selectedImage: Uri?): Bitmap? {
val MAX_HEIGHT = 1024
val MAX_WIDTH = 1024
// First decode with inJustDecodeBounds=true to check dimensions
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
var imageStream: InputStream = context.getContentResolver().openInputStream(selectedImage!!)!!
BitmapFactory.decodeStream(imageStream, null, options)
imageStream.close()
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT)
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false
imageStream = context.getContentResolver().openInputStream(selectedImage!!)!!
var img = BitmapFactory.decodeStream(imageStream, null, options)
img = rotateImageIfRequired(img!!, selectedImage)
return img
}
/**
* Calculate an inSampleSize for use in a [BitmapFactory.Options] object when decoding
* bitmaps using the decode* methods from [BitmapFactory]. This implementation calculates
* the closest inSampleSize that will result in the final decoded bitmap having a width and
* height equal to or larger than the requested width and height. This implementation does not
* ensure a power of 2 is returned for inSampleSize which can be faster when decoding but
* results in a larger bitmap which isn't as useful for caching purposes.
*
* @param options An options object with out* params already populated (run through a decode*
* method with inJustDecodeBounds==true
* @param reqWidth The requested width of the resulting bitmap
* @param reqHeight The requested height of the resulting bitmap
* @return The value to be used for inSampleSize
*/
private fun calculateInSampleSize(
options: BitmapFactory.Options,
reqWidth: Int, reqHeight: Int
): Int {
// Raw height and width of image
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
val heightRatio =
Math.round(height.toFloat() / reqHeight.toFloat())
val widthRatio =
Math.round(width.toFloat() / reqWidth.toFloat())
// Choose the smallest ratio as inSampleSize value, this will guarantee a final image
// with both dimensions larger than or equal to the requested height and width.
inSampleSize = if (heightRatio < widthRatio) heightRatio else widthRatio
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
val totalPixels = width * height.toFloat()
// Anything more than 2x the requested pixels we'll sample down further
val totalReqPixelsCap = reqWidth * reqHeight * 2.toFloat()
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++
}
}
return inSampleSize
}
/**
* Rotate an image if required.
*
* @param img The image bitmap
* @param selectedImage Image URI
* @return The resulted Bitmap after manipulation
*/
@Throws(IOException::class)
private fun rotateImageIfRequired(img: Bitmap, selectedImage: Uri): Bitmap? {
val ei = ExifInterface(selectedImage.path)
val orientation: Int =
ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(img, 90)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(img, 180)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(img, 270)
else -> img
}
}
private fun rotateImage(img: Bitmap, degree: Int): Bitmap? {
val matrix = Matrix()
matrix.postRotate(degree.toFloat())
val rotatedImg =
Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true)
img.recycle()
return rotatedImg
}
}
我一直有同樣的問題; 據我可以理解,從門票如回復此或此,背后CameraX球隊不喜歡與原始圖像數據染指從硬件返回,並會非常喜歡,只是把自己限制設置EXIF元數據。
所以我只是解決了這個問題,從一個與你的代碼非常相似的代碼開始(好吧,從代碼實驗室中的代碼中得到了很大的啟發),我添加了這個:
Display d = getDisplay();
if (d != null) {
iCapture.setTargetRotation(d.getRotation());
}
就在調用iCapture.takePicture()
之前(iCapture 是我的ImageCapture
用例實例)。 這樣可以確保 EXIF 文件元數據中的目標旋轉與照片拍攝時的實際顯示旋轉一致。
然后,在接收到數據后(在我的情況下,在onImageSaved()
處理程序上),我檢查 EXIF 元數據以進行旋轉,在這種情況下,手動旋轉圖像所需的度數並保存不同的文件以確保沒有 EXIF 標簽保持不連貫的價值觀。
try {
ExifInterface ei = new ExifInterface(tempFile.getAbsolutePath());
if (ei.getRotationDegrees() != 0) {
actualPicture = ImageUtil.rotateDegrees(tempFile, ei.getRotationDegrees());
}
} catch (IOException exc) {
Log.e(TAG, "Tried to fix image rotation but could not continue: " + exc,getMessage());
}
其中 ImageUtil 是一個自定義的圖像工具類,而 rotateDegrees 就是這樣做的,自定義矩陣初始化如下:
//inside rotateDegrees(), degrees is the parameter to the function
Matrix m = new Matrix();
m.postRotate(degrees);
並從原始文件導入的位圖開始創建一個新的位圖:
Bitmap b = Bitmap.createBitmap(sourceFile, 0, 0, sourceFile.getWidth(), sourceFile.getHeight(), m, true);
b.compress(Bitmap.CompressFormat.JPEG, 85, /* a suitably-created output stream */);
盡管如此,我還是希望 CameraX 能夠直接處理圖像旋轉,而不依賴於元數據(據他們自己承認,很少有庫和工具可以讀取和實際處理)。
這個簡單的代碼對我有用:
Java版本:
Camera camera = cameraProvider.bindToLifecycle(...);
int rotationDegrees = camera.getCameraInfo().getSensorRotationDegrees() - context.getDisplay().getRotation() * 90;
科特林版本:
val camera: Camera? = cameraProvider.bindToLifecycle(...)
val rotationDegree = camera!!.cameraInfo.sensorRotationDegrees - context.display!!.rotation * 90
然后我使用rotationDegrees 來旋轉CameraX 在takePicture 和analyze 的回調中傳遞給你的imageProxy。
如果需要,您可以在這里找到完整的 Java 代碼: https ://github.com/CristianDavideConte/SistemiDigitali/blob/main/app/src/main/java/com/example/sistemidigitali/model/CameraProvider.java
對我有用的最簡單的解決方案。
從 imageProxy 獲取 rotationDegrees 並將位圖旋轉該度數。
Matrix matrix = new Matrix();
matrix.postRotate((float)imageProxy.getImageInfo().getRotationDegrees());
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
binding.imgPreview.setImageBitmap(bitmap2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.