ImageIO.read 非法参数异常 - 光栅带/颜色空间组件? [英] ImageIO.read illegal argument exception - raster bands/colour space components?

查看:33
本文介绍了ImageIO.read 非法参数异常 - 光栅带/颜色空间组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉标题有点含糊,我不知道这里的关键字是什么.设置很简单,我用

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));

这适用于大多数文件,但是对于一个文件,我得到了一个 IllegalArgumentException,其中包含详细信息:源栅格波段数和源颜色空间分量不匹配".该图像是通过有效 Flickr URL 上的 wget 获得的,并且我使用了通过这种方式获得的其他图像,因此获取图像的方法原则上似乎是合理的.我不确定是什么导致了异常.

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.

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

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

这是有问题的图像,以防万一:

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

推荐答案

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

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.

            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;
            }
        }

这篇关于ImageIO.read 非法参数异常 - 光栅带/颜色空间组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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