Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ImageIO.write return false

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"

like image 636
user3363881 Avatar asked Mar 13 '14 08:03

user3363881


1 Answers

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.

like image 184
schnawel007 Avatar answered Sep 18 '22 22:09

schnawel007