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"
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.
FormateName
应该是"png"
而不是".png"
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.
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.