javax.imageio.ImageIO 坏了吗?它以旋转的形式导入一些图像 [英] Is javax.imageio.ImageIO broken? It imports some images as rotated

查看:35
本文介绍了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("宽度:" + img.getWidth() + " 高度:"+ img.getHeight());}}

它输出 width:960 height:720,当我保存这个输出图像时,它会像我之前所说的那样向左旋转.如果您想重现此内容,请从此处下载代码和图片并运行以下命令来构建和运行:

javac Main.java &&主程序

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

这里可能有什么问题?

解决方案

这张照片可能是手持 iPad 以纵向模式拍摄的,因此包含 EXIF 方向信息,ImageIO 会忽略这些信息,但您可以使用其他库,例如 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天全站免登陆