I am using CameraX for developing my android application in which when I take the pricture in landscape mode or portrait mode the image captured and saved is mirror image.
I know that FRONT camera works in the same way. But what to do if I want to save the picture in the same way it was taken?
Here is the buildUseCase() Code I am using:
private fun buildUseCases() {
val screenAspectRatio = Rational(width, height)
val screenTargetRotation = display.rotation
//Preview
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(screenTargetRotation)
setLensFacing(lensFacing)
}.build()
preview = AutoFitPreviewBuilder.build(previewConfig, this)
//End - Preview
// Set up the capture use case to allow users to take photos
val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(screenTargetRotation)
setLensFacing(lensFacing)
setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY)
}.build()
imageCapture = ImageCapture(imageCaptureConfig)
}
Please help me with what to change to get correct image captured.
Note: The camera facing is FRONT and it is in Landscape mode.
You need to read EXIF data of the image created & have to write your own custom controllers as per requirements & needs. It's very normal in most Android & iOS devices that the captured images get rotated & it must be handled accordingly. In most of the devices, the default orientation of the camera is set to landscape mode, so even if you take a pic in portrait mode it gets rotated to 90degrees.
From EXIF data, you can get the degree of the image rotated or if its mirrored & then you can handle it in the backend.
To rotate your image you can try
private static Bitmap rotateImageIfRequired(Bitmap img, Uri selectedImage) throws IOException
{
ExifInterface ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree)
{
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
For the image flipping issue you can try this
public static Bitmap flip(Bitmap src, int type)
{
// create new matrix for transformation
Matrix matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);
// return transformed image
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
And then set the image to ImageView as
imgPreview.setImageBitmap(flip(bitmap));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With