无法使用 ImageIO.read(File file) 读取 JPEG 图像 [英] Unable to read JPEG image using ImageIO.read(File file)

查看:124
本文介绍了无法使用 ImageIO.read(File file) 读取 JPEG 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 ImageIO.read(File file) 读取这个 JPEG 文件时遇到问题 - 它引发异常并显示消息不支持的图像类型".

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.

更新

将 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(File file) 读取 JPEG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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