Android Camera Intent Saving Image Landscape When Taken Portrait
The picture is always taken in the orientation the camera is built into the device. To get your image rotated correctly you'll have to read the orientation information that is stored into the picture (EXIF meta data). There it is stored how the device was oriented, when the image was taken.
Here is some code that reads the EXIF data and rotates the image accordingly:file
is the name of the image file.
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
UPDATE 2017-01-16
With the release of the 25.1.0 Support Library, an ExifInterface Support Library was introduced, which should perhaps make the access to the Exif attributes easier. See the Android Developer's Blog for an article about it.
Why does an image captured using camera intent gets rotated on some devices on Android?
Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.
Note that the below solution depends on the camera software/device manufacturer populating the Exif data, so it will work in most cases, but it is not a 100% reliable solution.
ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
}
Here is the rotateImage
method:
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
Android - Ensuring that photo orientation is preserved when taking photos via Camera Intent?
Is there a reliable way of making sure that the orientation of the camera gets preserved when saving the photo?
No.
First, you are delegating the picture taking to a third-party app, one of thousands of possible camera apps. Those developers can do whatever they want.
Second, the underlying cause of this comes from camera hardware and firmware. You have no means of telling some chipset to rotate the image instead of using the EXIF orientation header.
Related Topics
How to Get Current Date in Android
Android Install APK with Intent.View_Action Not Working with File Provider
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar
How to Format Datetime in Flutter
How to Pass Multiple Primitive Parameters to Asynctask
Asynctask and Error Handling on Android
Cannot Launch Avd in Emulator:Qt Library Not Found
How to Show Shadow Around the Linearlayout in Android
Android Camera Intent Saving Image Landscape When Taken Portrait
The Following Classes Could Not Be Found: Android.Support.V7.Internal.App.Windowdecoractionbar
Android Listview with Checkbox and All Clickable
Does Broadcastreceiver.Onreceive Always Run in the UI Thread
How to Detect System Information Like Os or Device Type
Recyclerview Gridlayoutmanager: How to Auto-Detect Span Count
How to Set Text Size of Textview Dynamically for Different Screens
Viewpager Inside a Scrollview Does Not Scroll Correclty
Apply Custom Filters to Camera Output
Install_Failed_Shared_User_Incompatible While Using Shared User Id