'How to resolve: javax.imageio.IIOException: Bogus input colorspace

I have a function generateImageOutput below to write BufferedImage to jpeg file.

public boolean generateImageOutput(BufferedImage image, String filename){
        //The image is written to the file by the writer
        File file = new File( projectFolder+"/data/"+filename+".jpg");
        //Iterator containing all ImageWriter (JPEG)
        Iterator encoder = ImageIO.getImageWritersByFormatName("JPEG");
        ImageWriter writer = (ImageWriter) encoder.next();
        //Compression parameter (best quality)
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(1.0f);
        //Try to write the image
        try{
            ImageOutputStream outputStream = ImageIO.createImageOutputStream(file);
            writer.setOutput(outputStream);
            writer.write(null, new IIOImage(image, null, null), param);
            outputStream.flush();
            writer.dispose();
            outputStream.close();
        }catch(IOException e){
            e.printStackTrace();
            System.out.println(e.toString());
            return false;
        }
        return true;
    }

It works for some, but it fails for a BufferedImage converted from base64 string:

String encodedString = JSON.parseObject(string).getString("image");
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
buffered_image = ImageIO.read(bis);

When writing the above buffered_image to jpeg using generateImageOutput, it raises exception :

javax.imageio.IIOException: Bogus input colorspace
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1007)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:371)

The string encodedString has no issue, I have sucessfully converted it to an image online.

How can I resolve the exception ?



Solution 1:[1]

Finally I find that the reason is the image has an ALPHA channel.

Solution 2:[2]

Looks like JPEG doesn't handle the alpha channel correctly in this context. For me saving the image as PNG did the trick.

Solution 3:[3]

According to @J.doe Alpha channel must be remove. I have a code that removes the Alpha channel. The code below was to determine whether or not the image has alpha channel. If the image has alpha channel it will create an image without alpha channel.

private static BufferedImage removeAlphaChannel(BufferedImage img) {
    if (!img.getColorModel().hasAlpha()) {
        return img;
    }

    BufferedImage target = createImage(img.getWidth(), img.getHeight(), false);
    Graphics2D g = target.createGraphics();
    // g.setColor(new Color(color, false));
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    g.drawImage(img, 0, 0, null);
    g.dispose();

    return target;
}
private static BufferedImage createImage(int width, int height, boolean hasAlpha) {
    return new BufferedImage(width, height, hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J. Doe
Solution 2
Solution 3 kelsky