Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is javax.imageio.ImageIO broken? It imports some images as rotated

Below you will see a picture of beatiful pastry called "simit" from Turkey. It is taken from iPad 2, therefore it is a JPEG with dimensions 720*960.

Picture from iPad 720x960

The problem is, when I use javax.imageio.ImageIO.read method, the image it strangely imports is to a BufferedImage rotated to left and becomes 960*720.

I reproduced this in my Sun JVM 1.6.0_29 on OS X and Sun JVM 1.6.0_26 on Debian. Here's the code:

public class Main {
    public static void main(String[] args) throws Exception {
        FileInputStream stream = new FileInputStream(new File("IMG_0159.JPG"));
        BufferedImage img = ImageIO.read(stream);
        System.out.println("width:" + img.getWidth() + " height:"
                + img.getHeight());
    }
}

It outputs width:960 height:720, and when I save this output image, it is rotated to left as I told before. If you would like to reproduce this, download code and picture from here and run the following commands to build and run:

javac Main.java && java Main

NOTE: You may see the JPG in the archive as already rotated, however it appears 720*960 on OS X, iPad, iPhone and as you see above, it is uploaded correctly to imgur.com. And it is also opened correctly in Adobe Photoshop, uploaded to Facebook correctly etc.

What could be the problem here?

like image 551
ahmet alp balkan Avatar asked Feb 26 '12 13:02

ahmet alp balkan


1 Answers

The photo was probably taken holding the iPad in portrait mode, and therefore contains EXIF orientation information, which ImageIO ignores, but you can use other libraries, like Apache Sanselan to correctly handle it.

So the image itself is 960x720, but MacOS, ImgUR, Facebook etc correctly take the EXIF info into account.

And simit looks delicious :)

like image 179
ᅙᄉᅙ Avatar answered Oct 06 '22 01:10

ᅙᄉᅙ