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

后端 未结 3 728
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 20:11

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          


        
相关标签:
3条回答
  • 2020-12-14 20:34

    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 ImageMagick to strip the ICC profile from the image. ImageIO subsequently has no problems reading the images (~1k bad images). Hope that helps.

    0 讨论(0)
  • 2020-12-14 20:47

    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;
                }
            }
    
    0 讨论(0)
  • 2020-12-14 20:52

    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.

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

    I found this solution in the 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.

    <dependency>
        <groupId>com.twelvemonkeys.imageio</groupId>
        <artifactId>imageio-jpeg</artifactId>
        <version>3.3.2</version> <!-- Alternatively, build your own version -->
    </dependency>
    
    0 讨论(0)
提交回复
热议问题