javax.imageio.ImageIO坏了吗?它会旋转一些图像 [英] Is javax.imageio.ImageIO broken? It imports some images as rotated

查看:155
本文介绍了javax.imageio.ImageIO坏了吗?它会旋转一些图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面你会看到一张美丽糕点的照片,名为方法,它奇怪地导入的图像是 BufferedImage 向左旋转并变为 960 * 720



我在OS X上的Sun JVM 1.6.0_29和Debian上的Sun JVM 1.6.0_26上重现了这一点。这是代码:

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

输出宽度:960高度:720 ,当我保存此输出图像时,它会像我之前所说的那样向左旋转。如果您想重现这一点,请从此处下载代码和图片,然后运行以下命令构建和运行:

  javac Main.java&& java Main 

注意:您可能已在存档中看到JPG已经旋转,但它在OS X,iPad,iPhone上显示为720 * 960,如上所示,它正确上传到imgur.com。它也可以在Adobe Photoshop中正确打开,正确上传到Facebook等。



这里可能出现什么问题?

解决方案

这张照片可能是以纵向模式拍摄iPad,因此包含了ImageIO忽略的EXIF方向信息,但您可以使用其他库,如Apache Sanselan来正确处理它。



因此图像本身是960x720,但MacOS,ImgUR,Facebook等正确考虑了EXIF信息。



simit看起来很美味:)


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?

解决方案

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

这篇关于javax.imageio.ImageIO坏了吗?它会旋转一些图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆