简体   繁体   中英

ImageIO can't read certain Images

I have the following problem. We have an Webservice with Upload Function for images. When you try to upload certain images, it just fails. These images have the right MIMETYPE, they are not CMYK (at least GIMP say they are in RGB). The thrown Exception is: "Unsupported Image Type"! The problem occurs, when trying to start this command:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(image.getData()));

I dig a little deeper and the real exception gets thrown with the ImageIO.read(ImageInputStream stream) , when he tries to close the stream again!

public static BufferedImage read(ImageInputStream stream)
    throws IOException {
    if (stream == null) {
        throw new IllegalArgumentException("stream == null!");
    }

    Iterator iter = getImageReaders(stream);
    if (!iter.hasNext()) {
        return null;
    }

    ImageReader reader = (ImageReader)iter.next();
    ImageReadParam param = reader.getDefaultReadParam();
    reader.setInput(stream, true, true);
    BufferedImage bi;
    try {
        bi = reader.read(0, param);
    } finally {
        reader.dispose();
        stream.close();
    }
    return bi;
}

An Image throwing the exception is this for example:
崩溃的图像

I hope someone can help me figure out, why this crashes and how to fix it!

The attached picture has CMYK color model. Try to convert it to RGB.

This question can be useful: How to convert from CMYK to RGB in Java correctly?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM