Java ImageIO.write return false
Asked Answered
S

3

7

I want to save a BufferedImage (named "result"):

boolean bres = ImageIO.write(result, ".png", new File(saveP)); 

But bres is always null.

This

ImageIO.getWriterFormatNames()

returns that:

jpg BMP bmp JPG jpeg wbmp png JPEG PNG WBMP GIF gif

I should be able to save as an png.

And the type of the BufferedImage is "2"

BufferedImage@137695c: type = 2 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 720 height = 576 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0

Type 2 is "ARGB".

Why i can't save the BufferedImage?

EDIT: saveP = "ex000567.png"

Seethrough answered 13/3, 2014 at 8:42 Comment(2)
Can you provide detail of result and saveP and change .png to pngEmanate
All the Details of result are on the Object "BufferedImage@137695c"Seethrough
R
12
private static void savePNG( final BufferedImage image, final String path ){
        try {
            RenderedImage rendImage = image;
            ImageIO.write(rendImage, "PNG", new File(path));
        } catch ( IOException e) {
            e.printStackTrace();
        }
    }

Test this function. This one works for me.

The important change is that the second parameter to ImageIO.write is changed from ".png" to "PNG" (lowercase "png" would also work), refer to the output of ImageIO.getWriterFormatNames() for valid names.

Resolutive answered 13/3, 2014 at 8:44 Comment(1)
Format issue of imageEmanate
T
6

For anyone who experiences this issue with JPEG files, please make sure that your buffered image does not have an alpha channel. Otherwise, com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi#canEncodeImage will reject your image.

For example, instead of BufferedImage.TYPE_INT_ARGB use BufferedImage.TYPE_INT_RGB or something else that does not have an alpha channel.

Tendon answered 26/5, 2022 at 10:45 Comment(1)
This was driving me crazy. Thank you.Bullpen
N
2

FormateName should be "png" not ".png"

Nesline answered 13/3, 2014 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.