In this article, we will show you how to use ImageIO, JAI and JDeli to write out Buffered Images as PNG.
Previously, we have used ImageIO and JAI to handle images during conversion. Both Libraries currently are not able to fulfill our needs so we developed our own Encoder and Decoder Image Library called JDeli ( Java Decoding Encoding Library for Images). We also stated in our previous blog article why we are dropping ImageIO and JAI.
What is PNG
PNG stands for Portable Network Graphics.It is a raster graphic file format that supports lossless data compression. It is used widely on the world wide web as it supports transparency in browsers. Also PNG files are non lossy files and supports 8 and 24 bit colors.
The file name extension for PNG files is:
Example Java code to write PNG images in Java:
Here are some code examples using Image IO, JAI and JDeli to save a BufferedImage to a PNG format
– Using Image IO
Javadoc and included in JDK
File myNewPNGFile = new File("ImageAsPNG.png"); ImageIO.write(bufferedImage, "PNG", myNewPNGFile);
PNGEncodeParam param = PNGEncodeParam.getDefaultEncodeParam(bufferedImage); ImageEncoder encoder= ImageCodec.createImageEncoder("PNG", bufferedOutputStream, param); encoder.encode(bufferedImage);
– Using JDeli
PngEncoder pngEncoder = new PngEncoder(); OutputStream out = new FileOutputStream("myNewPngFile.png"); pngEncoder.write(bufferedImage, out);
We have also created a useful how to series on how to read and write various Image formats.
- How to Write out TIFF in Java
- How to Read TIFF Images in Java
- How to generate smaller PNG files in Java
- How to read JPEG2000 in Java
- How to Write out JPEG Images in Java
Latest posts by Ernest Duodu (see all)
- 10 things Developers should know about Windows 10 - September 15, 2015
- How to test your Java Application using PMD, and Jenkins - July 30, 2015
- Setting up Continuous Testing with Jenkins - July 23, 2015
- How to Build Java Projects with Apache ANT - July 21, 2015
- How to upload your Java artifact to Maven Central - June 30, 2015