When I take a picture with Android's camera app, it detects the phone's orientation and saves the picture accordingly. So if I take a picture of a building, the roof will be on the topside, whether I hold the phone in landscape position or portrait.
However, when I use
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
to get a picture, the camera app doesn't react to the orientation. If I hold the phone vertically (portrait), the resulting picture will be rotated, with said building's roof to the left of the screen.
How can I set the intent so that the camera will take the orientation into account?
Or can I deduce in some way in what orientation the picture was taken and rotate it myself afterwards?
Or is there another way?
The length of the longest side determines the orientation. For example, if the height of the image is longer than the width, it is a “portrait” format. Images where the width is longer are called “landscape.”
Answer: 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.
Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.
I found the answer. The Exif of the image has an indicator of the orientation. Just in case, Exif can be viewed in Android like this:
ExifInterface exif = new ExifInterface("filepath"); exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Read from Exif if available, otherwise read from MediaStore
public static int getImageOrientation(Context context, String imagePath) { int orientation = getOrientationFromExif(imagePath); if(orientation <= 0) { orientation = getOrientationFromMediaStore(context, imagePath); } return orientation; } private static int getOrientationFromExif(String imagePath) { int orientation = -1; try { ExifInterface exif = new ExifInterface(imagePath); int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (exifOrientation) { case ExifInterface.ORIENTATION_ROTATE_270: orientation = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: orientation = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: orientation = 90; break; case ExifInterface.ORIENTATION_NORMAL: orientation = 0; break; default: break; } } catch (IOException e) { Log.e(LOG_TAG, "Unable to get image exif orientation", e); } return orientation; } private static int getOrientationFromMediaStore(Context context, String imagePath) { Uri imageUri = getImageContentUri(context, imagePath); if(imageUri == null) { return -1; } String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION}; Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null); int orientation = -1; if (cursor != null && cursor.moveToFirst()) { orientation = cursor.getInt(0); cursor.close(); } return orientation; } private static Uri getImageContentUri(Context context, String imagePath) { String[] projection = new String[] {MediaStore.Images.Media._ID}; String selection = MediaStore.Images.Media.DATA + "=? "; String[] selectionArgs = new String[] {imagePath}; Cursor cursor = context.getContentResolver().query(IMAGE_PROVIDER_URI, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int imageId = cursor.getInt(0); cursor.close(); return Uri.withAppendedPath(IMAGE_PROVIDER_URI, Integer.toString(imageId)); } if (new File(imagePath).exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, imagePath); return context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } return null; }
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