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

南笙酒味 提交于 2019-12-01 01:12:13

问题


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.

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?


回答1:


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



来源:https://stackoverflow.com/questions/9453367/is-javax-imageio-imageio-broken-it-imports-some-images-as-rotated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!