Image appearing 90 degree tilt when downloaded from server in Android?

2.3k views Asked by At

Working on an application in which we capture images and upload over server. Application is in Android And I Phone. When we post image from Android, they are of quantity in Kilo Bytes but when we Post image from I Phone they are of MB size.

When we the images posted from IPHONE 5 with URL on browser, they appear good as they supposed to appear but when we download that image in Android device and show in an IMAGE VIEW, they appears 90 deg tilt to the left hand side.

I am not using, any rotation code after downloading the images in Android or in I Phone.

In I Phone the images are appearing fine.

IMages captured from Android are also visible straight. Images of low resolution capture from I Phone are also visible straight in Android.

Image uploaded from Android:

https://s3.amazonaws.com/WeddingApp/Weddingimage/933_6_stan.jpg

Image uploaded from I Phone:

https://s3.amazonaws.com/WeddingApp/Weddingimage/937_6_stan.jpg


public static boolean downloadFile(final String fileURL,File directory,Context CONTEXT){
        try{
            URL url = new URL(fileURL);

            URLConnection ucon = url.openConnection();
            ucon.setReadTimeout(35000);
            ucon.setConnectTimeout(10000);

            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

            File file = directory;

            if (file.exists())
            {
                file.delete();
            }
            file.createNewFile();

            FileOutputStream outStream = new FileOutputStream(file);
            byte[] buff = new byte[5 * 1024];

            int len;
            while ((len = inStream.read(buff)) != -1)
            {
                outStream.write(buff, 0, len);
            }

            outStream.flush();
            outStream.close();
            inStream.close();

        }
        catch (IOException e){  //IF SDCARD NOT EXIST THEN PASS RESPONSE TRUE`  
            e.printStackTrace();
            return false;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    } 

Please suggest me.

2

There are 2 answers

1
Junior Buckeridge On BEST ANSWER

The problem seems to be related to EXIF data found on the images. We must process it before displaying the image. It appears to be that not every camera outputs EXIF data 'cause this issues only happens to me in some android handsets. Take a look at: http://commons.wikimedia.org/wiki/Commons:Exif#Orientation_.28rotation_and_mirroring.29

EDIT: We could implement something like:

public Bitmap getBitmap(String path){
    Bitmap tmpBitmap = BitmapFactory.decodeFile(path);
            Bitmap bitmap = null;
            if(path != null){
                try {
                    ExifInterface ei = new ExifInterface(path);
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                    Matrix mtx = new Matrix();
                    int w = tmpBitmap.getWidth();
                    int h = tmpBitmap.getHeight();
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        //rotate CCW
                        mtx.preRotate(-90);
                        bitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, w, h, mtx, true);
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_270:
                        //rotate CW
                        mtx.preRotate(90);
                        bitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, w, h, mtx, true);
                        break;

                        //CONSIDER OTHER CASES HERE....

                    default:
                        bitmap = tmpBitmap;
                        break;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
    return bitmap;
}

Regards.

0
sanjeev mk On

This is because of iPhone's silly new camera implementation. I found below on research (sources given below):

1. The iPhone camera's interpretation of "up" direction is rotated 90 degrees from the actual "up" direction we know. What you and I call "up", is iphone's "left" or "right". The camera's orientation has been changed. This was done so that they could support taking pictures with the volume button.

2. But, being aware that other devices and cameras have normal orientation, and so that those devices and browsers display the images properly, iOS adds EXIF data to the uploaded images. This is added on image upload. EXIF, as mentioned in the link, is a metadata that contains all the information of how the image is actually supposed to look.

3. The target platform on which the image is downloaded is supposed to read the EXIF data, and then show the correct representation of the image. So, if the target platform reads the EXIF, it will realize that the image received is 90 degrees rotated, and that it should rely on EXIF data, and not on what's received. This is why iPhone camera apps can show the image properly.

4 However, not everybody reads EXIF. Apps and browsers that are aware of EXIF, read the EXIF data of the image, and show it properly. But those that are not aware of EXIF, don't read that data, and show the image exactly as received --> 90 degrees rotated

5 People have worked around this problem, by rotating their iPhone 90 degrees, before taking a picture (so that the camera is oriented right, before the picture is taken)

Resources:

1. Source 1

2. Source 2

Others with same problem:

1. SO Post 1

2. SO Post 2