java ImageIO.read 非法参数异常 - 光栅带/颜色空间组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10416378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:58:02  来源:igfitidea点击:

ImageIO.read illegal argument exception - raster bands/colour space components?

javaimagejavax.imageio

提问by mtrc

Apologies for the somewhat vague title, I can't work out what the keywords are here. The setup's quite simple, I'm opening an image with

为有点模糊的标题道歉,我无法弄清楚这里的关键字是什么。设置很简单,我打开一张图片

ImageIO.read(new File(filename));

This works for most files, however for one I get an IllegalArgumentException with the detail: "numbers of source Raster bands and source color space components do not match". This image was obtained via wget on a valid Flickr URL, and I've used other images obtained this way, so the method for obtaining images seems sound in principle. I'm not sure what's causing the exception.

这适用于大多数文件,但是对于一个我得到IllegalArgumentException with the detail: "numbers of source Raster bands and source color space components do not match". 该图像是通过有效 Flickr URL 上的 wget 获得的,并且我使用了通过这种方式获得的其他图像,因此获取图像的方法原则上似乎是合理的。我不确定是什么导致了异常。

A workaround would be more than acceptable - I'm not fussed with using ImageIOin particular, and the image looks fine visually. I just need to get it being read without Java freaking out!

一种解决方法是可以接受的 - 我并不ImageIO特别热衷于使用,并且图像在视觉上看起来很好。我只需要读取它而不用担心 Java!

Here's the image in question, in case it's of any use:

这是有问题的图像,以防万一它有任何用处:

enter image description here

在此处输入图片说明

采纳答案by Tate Moore

So I was having this same issue and found that the image was gray-scale and that the default ImageIO.read implementation was not figuring that out because the image metadata wasn't quite as expected. I wrote a work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

所以我遇到了同样的问题,发现图像是灰度的,默认的 ImageIO.read 实现没有解决这个问题,因为图像元数据并不像预期的那样。我写了一个解决方法,如果主加载失败,则重试加载为“BufferedImage.TYPE_BYTE_GRAY”。

            Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

        Exception lastException = null;
        while (iter.hasNext()) {
            ImageReader reader = null;
            try {
                reader = (ImageReader)iter.next();
                ImageReadParam param = reader.getDefaultReadParam();
                reader.setInput(stream, true, true);
                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                while (imageTypes.hasNext()) {
                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                        param.setDestinationType(imageTypeSpecifier);
                        break;
                    }
                }
                bufferedImage = reader.read(0, param);
                if (null != bufferedImage) break;
            } catch (Exception e) {
                lastException = e;
            } finally {
                if (null != reader) reader.dispose();               
            }
        }
        // If you don't have an image at the end of all readers
        if (null == bufferedImage) {
            if (null != lastException) {
                throw lastException;
            }
        }

回答by Suchet

The error message is informative and indicates that the number of raster bands, as mentioned in the ICC color profile, seems to be incorrect. I used ImageMagickto strip the ICC profile from the image. ImageIO subsequently has no problems reading the images (~1k bad images). Hope that helps.

该错误消息提供信息,表明 ICC 颜色配置文件中提到的光栅带数似乎不正确。我使用ImageMagick从图像中去除ICC 配置文件。ImageIO 随后读取图像没有问题(约 1k 个坏图像)。希望有帮助。

回答by Hok

It is possible to read this image using twelvemonkeys ImageIO, which is a more robust and forgiving replacement for the original ImageIO provided by the JRE.

可以使用十二猴子 ImageIO 读取此图像,它是 JRE 提供的原始 ImageIO 的更强大和更宽容的替代品。

See https://github.com/haraldk/TwelveMonkeys/

https://github.com/haraldk/TwelveMonkeys/

I found this solution in the PDF Box Jira https://issues.apache.org/jira/browse/PDFBOX-3637

我在 PDF Box Jira https://issues.apache.org/jira/browse/PDFBOX-3637 中找到了这个解决方案

In order to use twelvemonkeys, it is sufficient to add it as a maven dependency. It then registers itself before the default image processor.

为了使用十二猴子,将其添加为 maven 依赖项就足够了。然后它在默认图像处理器之前注册自己。

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.3.2</version> <!-- Alternatively, build your own version -->
</dependency>