ImageIO.read() 是否考虑 EXIF 方向元数据? [英] Does ImageIO.read() take EXIF orientation metadata into account?

查看:31
本文介绍了ImageIO.read() 是否考虑 EXIF 方向元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JPEG 图像中,有时会包含 EXIF 元数据,并说明图像应该以什么方向显示.

In JPEG images, EXIF metadata is sometimes included and tells in what orientation the image should be shown.

问题是,Java 的 ImageIO.read() 在读取 JPEG 图像时是否将 EXIF 考虑在内,并自动应用转换.

The question is, whether Java's ImageIO.read() takes EXIF into account while reading a JPEG image, and automatically applies the transformation.

更具体地说,如果我使用 Java 的 ImageIO 将带有 EXIF 的 JPEG 图像转换为 PNG 图像,PNG 图像的方向是否正确?或者下面的代码是否会在不考虑 EXIF 方向指令的情况下生成 PNG 图像?

More concretely, if I use Java's ImageIO for converting a JPEG image with EXIF into a PNG image, is the orientation of the PNG image going to be correct? Or is the below code going to produce a PNG image without taking EXIF orientation instructions into account?

private byte[] convertToPng(byte[] imageFileAsByteArray) {
    ByteArrayInputStream bis = new ByteArrayInputStream(imageFileAsByteArray);
    BufferedImage bi = ImageIO.read(bis);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(bi, "png", bos);

    return bos.toByteArray();
}

推荐答案

不幸的是,简短的回答是不;ImageIO.read(..)(默认情况下)不考虑 Exif Orientation 标签.您的 PNG 将不会相应地旋转.

The short answer is, unfortunately, no; ImageIO.read(..) (by default) does not take the Exif Orientation tag into account. Your PNG will be not be rotated accordingly.

然而,所有 ImageIO.read(..) 在内部所做的就是为输入查找合适的 ImageReader 插件,并将读取委托给它.虽然 JRE 捆绑的插件没有考虑 Exif,但可以在第三方 ImageReader 中添加对它的支持.不幸的是,我不知道有什么可以做的,但我正在考虑将 Exif 方向支持添加到TwelveMonkeys ImageIO JPEGImageReader 未来.

However, all ImageIO.read(..) does internally, is to look up an appropriate ImageReader plug-in for the input, and delegate reading to it. And while the JRE-bundled plug-in does not take Exif into account, it is possible to add support for it, in a third-party ImageReader. Unfortunately, I don't know of any that do, but I am considering adding Exif orientation support to the TwelveMonkeys ImageIO JPEGImageReader in the future.

与此同时,您必须通过读取元数据和旋转来自己应用旋转.可以使用 ImageIO JPEG 元数据 或一些第三方库.我认为提到的 metadata-extractorTwelveMonkeys ImageIO 可用于此目的.JAI ImageIO(使用 TIFF 元数据)可能也可以做到这一点.

In the mean time, you have to apply the rotation yourself, by reading the metadata and rotating. The value of the orientation tag can be acquired either using the ImageIO JPEG metadata or some third-party library. I think both the mentioned metadata-extractor or TwelveMonkeys ImageIO can be used for this purpose. JAI ImageIO (using the TIFF metadata) can probably also do this.

如果使用 ImageIO JPEG 元数据,请注意:

If using the ImageIO JPEG metadata, be aware:

请注意,希望在给定 javax_imageio_jpeg_image_1.0 格式的元数据树结构的情况下解释 Exif 元数据的应用程序必须检查 unknown 标记段,该标记段带有指示 APP1 标记并包含将其标识为 Exif 标记段的数据.

Note that an application wishing to interpret Exif metadata given a metadata tree structure in the javax_imageio_jpeg_image_1.0 format must check for an unknown marker segment with a tag indicating an APP1 marker and containing data identifying it as an Exif marker segment.

这篇关于ImageIO.read() 是否考虑 EXIF 方向元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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