Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera Intent Saving Image Landscape When Taken Portrait [duplicate]

I have had a look around but there doesn't seem to be a solid answer/solution to the, very irritating, problem.

I take a picture in portrait orientation and when I hit save/discard the buttons are in the correct orientation also. The problem is when I then retrieve the image later on it is in landscape orientation (the picture has been rotated 90 degrees anti-clockwise)

I don' want to force the user to use the camera in a certain orientation.

Is there a way to maybe detect whether the photo was taken in portrait mode and then decode the bitmap and flip it the correct way up?

like image 551
StuStirling Avatar asked Oct 17 '12 11:10

StuStirling


People also ask

Why does an image captured using camera intent gets rotated?

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.

Why do my pictures keep uploading sideways?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".

How do I fix the camera orientation on my Android?

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.


2 Answers

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.

like image 161
Ridcully Avatar answered Oct 11 '22 18:10

Ridcully


The selected answer uses the most common method answered to this and similar questions. However, it did not work for me with both front and back cameras on Samsung. For those needing another solution which works across both front and back cameras for Samsung and other major manufacturers, this answer by nvhausid is awesome:

https://stackoverflow.com/a/18915443/6080472

For those who don't want to click through, the relevant magic is to use the CameraInfo rather then relying on EXIF or a Cursor over the media files.

Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length); android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(mCurrentCameraId, info); Bitmap bitmap = rotate(realImage, info.orientation); 

Full code in the link.

like image 33
D. Scott Avatar answered Oct 11 '22 18:10

D. Scott