无法使用 ImageIO.read(File file) 读取 JPEG 图像
我在使用 ImageIO.read(File file) 读取这个 JPEG 文件时遇到问题 - 它抛出异常并显示消息“不支持的图像类型”。
我尝试过其他 JPEG 图像,它们似乎工作得很好。
我能够发现的唯一区别是该文件似乎包含缩略图 - 是否已知会导致 ImageIO.read() 出现问题?
编辑:
添加了生成的图像:
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws an exception with the message "Unsupported Image Type".
I have tried other JPEG images, and they seem to work fine.
The only differance I've been able to spot is that this file seems to include a thumbnail - is that known to cause problems with ImageIO.read()?
EDIT:
Added the resulting image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
旧帖子,但供将来参考:
受此问题和此处找到的链接的启发,我为 ImageIO 编写了一个 JPEGImageReader 插件,支持 CMYK 颜色模型(均使用原始颜色模型,或在读取时隐式转换为 RGB)。与此处提到的其他解决方案相比,阅读器还使用嵌入在 JPEG 流中的 ICC 配置文件进行适当的颜色转换。
它是纯 Java,不需要 JAI。源代码和二进制发行版可在 github.com/haraldk/TwelveMonkeys 免费获取,并且涵盖通过 BSD 风格的许可证。
安装后,它允许您使用
ImageIO.read(...)
读取 CMYK JPEG,如下所示:即:在大多数情况下,无需修改代码。
Old post, but for future reference:
Inspired by this question and links found here, I've written a JPEGImageReader plugin for ImageIO that supports CMYK color models (both with original color model, or implicitly converted to RGB on read). The reader also does proper color conversion, using the ICC profile embedded in the JPEG stream, in contrast to other solutions mentioned here.
It's plain Java and does not require JAI. The source code and binary distributions are freely available at github.com/haraldk/TwelveMonkeys, and is covered by a BSD-style license.
Once you have it installed, it allows you to read CMYK JPEGs using
ImageIO.read(...)
like this:I.e.: In most cases, it's not necessary to modify your code.
您的图像“颜色模型”是CMYK,
JPEGImageReader
(读取文件的内部类)仅读取RGB颜色模型。如果您需要读取 CMYK 图像,那么您将需要转换它们,请尝试此代码。
更新
将 CMYK 图像读取到 RGB BufferedImage 中。
更新 - 2015 年 3 月 - 添加模拟图像
原始图像已从 OP 的保管箱中删除。因此,我添加了新图像(不是原始图像)来模拟它们所发生的问题。
第一张图像是普通 RGB 图像的样子。
第二张图像 是同一图像在 CMYK 颜色模型中的外观。
您实际上无法看到它在网络上的样子,因为它会被主机转换为 RGB。要准确查看其外观,请获取 RGB 图像并通过 RGB 到 CMYK 转换器运行它。
第三张图像是使用 Java ImageIO 读取和写入时 CMYK 图像的外观。
OP 发生的问题是他们有类似图像 2 的东西,当你尝试阅读它。
Your image "Color Model" is CMYK,
JPEGImageReader
(the inner class that reads your file) reads only RGB Color Model.If you need to read CMYK images, then you will need to convert them, try this code.
UPDATE
Read a CMYK image into RGB BufferedImage.
UPDATE - March 2015 - Adding simulation images
Original images were removed from OP's dropbox. So I'm adding new images (not the originals) that simulates the problem that was happening with them.
First image is how a normal RGB image looks like.
Second image is how the same image will look like in CMYK color model.
You cannot actually see how it looks on the web because it will be converted to RGB by the host. To see exactly how it looks, take the RGB image and run it through an RGB to CMYK converter.
Third image is how the CMYK image will look like when read then written using Java ImageIO.
The problem that was happening with OP is they had something like image 2 which throws an exception when you try to read it.
我参加聚会有点晚了。但我发布我的答案可能仍然是值得的,因为没有一个答案能够真正解决问题。
该解决方案需要 Sanselan(或现在称为 Apache Commons Imaging),并且需要合理的 CMYK 颜色配置文件(.icc 文件)。您可以从 Adobe 或 eci.org 获取后一种版本。
基本问题是 Java(开箱即用)只能读取 RGB 格式的 JPEG 文件。如果您有 CMYK 文件,则需要区分常规 CMYK、Adobe CMYK(具有反转值,即 255 表示无墨水,0 表示最大墨水)和 Adobe CYYK(某些也具有反转颜色的变体)。
该代码首先尝试使用常规方法读取文件,该方法适用于 RGB 文件。如果失败,它将读取颜色模型的详细信息(配置文件、Adobe 标记、Adobe 变体)。然后它读取原始像素数据(光栅)并进行所有必要的转换(YCCK 到 CMYK、反转颜色、CMYK 到 RGB)。
我对我的解决方案不太满意。虽然颜色大部分都很好,但黑暗区域有点太亮,特别是黑色并不是全黑。如果有人知道我可以改进什么,我会很高兴听到。
I'm a bit late to the party. But it's probably still worth that I post my answer as none of the answers really solves the problem.
The solution requires Sanselan (or Apache Commons Imaging as it's called now) and it requires a reasonable CMYK color profile (.icc file). You can get the later one from Adobe or from eci.org.
The basic problem is that Java - out of the box - can only read JPEG files in RGB. If you have a CMYK file, you need to distinguish between regular CMYK, Adobe CMYK (with inverted values, i.e. 255 for no ink and 0 for maximum ink) and Adobe CYYK (some variant with inverted colors as well).
The code first tries to read the file using the regular method, which works for RGB files. If it fails, it reads the details of the color model (profile, Adobe marker, Adobe variant). Then it reads the raw pixel data (raster) and does all the necessary conversion (YCCK to CMYK, inverted colors, CMYK to RGB).
I'm not quite satisfied with my solution. While the colors are mostly good, dark areas are slightly too bright, in particular black isn't fully black. If anyone knows what I could improve, I'd be glad to hear it.
ImageIO.read()
->ImageIO.read()
->我发现 https://stackoverflow。 com/questions/22409... 在这里,这个也做了很好的颜色转换
并将两者结合起来得到这个:
I found https://stackoverflow.com/questions/22409... on here as well, this one does a great colour conversion
And combined both to get this:
我用这个解决它。
只需要添加这个依赖即可。我可以通过 ImageIO 读取 CMYK 图像。
十二猴子
i fix it by this.
only need add this dependency. i can read CMYK image by ImageIO.
TwelveMonkeys