javax. imageio. Is imageio broken? It rotates some images
Below you will see a delicious cake "simit" from Turkey It's taken from the iPad 2, so it's a JPEG with a size of 720 * 960
The problem is, when I use javax imageio. ImageIO. When using the read method, it strangely imports a bufferedimage rotated to the left and becomes 960 * 720
My sun JVM on OS X is 1.6 0_ 29 and sun JVM 1.6 on Debian 0_ This is repeated on 26 This is 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()); } }
Its output width: 960 and height: 720. When I save this output image, it rotates to the left as I said before If you want to reproduce this, download the code and images from here and run the following command to build and run:
javac Main.java && java Main
Note: you may see that the JPG in the archive has been rotated, but it is displayed as 720 * 960 on OS X, iPad and iPhone. As shown above, it has been correctly uploaded to imgur com. It can also be opened correctly in Adobe Photoshop, uploaded to Facebook, etc
What could be the problem?
Solution
The photo may be taken in portrait mode, so it contains EXIF direction information ignored by imageio, but you can use other libraries (such as Apache sanselan) to handle it correctly
So the image itself is 960 × 720, but MacOS, imgur, Facebook, etc. have correctly considered EXIF information
And simit looks delicious:)