问题描述
我在使用ImageIO.read(文件文件)读取这个JPEG文件时遇到问题 - 它会抛出一条带有Unsupported Image Type消息的异常。
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws an exception with the message "Unsupported Image Type".
我尝试了其他JPEG图像,它们似乎工作正常。
I have tried other JPEG images, and they seem to work fine.
我能发现的唯一区别是这个文件似乎包含一个缩略图 - 是已知会导致ImageIO.read()出现问题?
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()?
编辑:
添加了生成的图像:
推荐答案
您的图像颜色模型是CMYK, JPEGImageReader
(读取文件的内部类)只读取RGB颜色模型。
Your image "Color Model" is CMYK, JPEGImageReader
(the inner class that reads your file) reads only RGB Color Model.
如果你坚持阅读CMYK图像,那么你需要转换它们,试试这个代码。
If you insist on reading CMYK images, then you will need to convert them, try this code.
UPDATE
读取CMYK图像进入RGB BufferedImage。
Read a CMYK image into RGB BufferedImage.
File f = new File("/path/imagefile.jpg");
//Find a suitable ImageReader
Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while(readers.hasNext()) {
reader = (ImageReader)readers.next();
if(reader.canReadRaster()) {
break;
}
}
//Stream the image file (the original CMYK image)
ImageInputStream input = ImageIO.createImageInputStream(f);
reader.setInput(input);
//Read the image raster
Raster raster = reader.readRaster(0, null);
//Create a new RGB image
BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
//Fill the new image with the old raster
bi.getRaster().setRect(raster);
更新 - 2015年3月 - 添加模拟图像
从OP的保管箱中删除了原始图像。所以我正在添加模拟他们发生的问题的新图像(不是原件)。
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.
第一张图片是正常的RGB图像看起来像。
First image is how a normal RGB image looks like.
第二张图片是CMYK颜色模型中相同图片的样子。
Second image is how the same image will look like in CMYK color model.
你不能实际上看它在网络上看起来如何,因为它将由主机转换为RGB。要准确了解它的外观,请拍摄RGB图像并通过RGB到CMYK转换器运行。
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.
第三张图像是CMYK图像的方式看起来就像读取后使用Java ImageIO编写。
Third image is how the CMYK image will look like when read then written using Java ImageIO.
OP发生的问题是它们有类似图像2的东西,当你试图读取它时会引发异常。
The problem that was happening with OP is they had something like image 2 which throws an exception when you try to read it.
这篇关于无法使用ImageIO.read读取JPEG图像(文件文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!