8

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?

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214

1 Answers1

10

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 :)

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • You are probably right. I've just confirmed it from some Exif Viewer http://regex.info/exif.cgi However Apache Sanselan clearly states that it does NOT support JPEG. Shame on ImageIO, still looking for a solution. – ahmet alp balkan Feb 26 '12 at 15:10
  • With Sanselan you can read the EXIF orientation info, and then rotate the Bitmap you've read with ImageIO.. Of course an integrated solution would be better, but I found none which is pure Java... There is always jMagick if JNI libraries are OK – P Varga Feb 26 '12 at 16:34
  • 1
    http://stackoverflow.com/questions/5905868/am-i-making-this-too-complicated-image-rotation – P Varga Feb 26 '12 at 17:07
  • I used metadata-extractor to read metadata, i think it's a good solution: https://github.com/drewnoakes/metadata-extractor/wiki com.drewnoakes metadata-extractor 2.7.2 – Paolo Biavati Mar 28 '15 at 16:30