I have a BufferedImage :
BufferedImage bi = new BufferedImage(14400, 14400, BufferedImage.TYPE_INT_ARGB);
I saved this image in a PNG file using the following code:
public static void saveGridImage(BufferedImage sourceImage, int DPI, File output) throws IOException { output.delete(); final String formatName = "png"; for (Iterator<ImageWriter> iw = ImageIO .getImageWritersByFormatName(formatName); iw.hasNext();) { ImageWriter writer = iw.next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata( typeSpecifier, writeParam); if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { continue; } setDPI(metadata, DPI); final ImageOutputStream stream = ImageIO .createImageOutputStream(output); try { writer.setOutput(stream); writer.write(metadata, new IIOImage(sourceImage, null, metadata), writeParam); } finally { stream.close(); } break; } } public static void setDPI(IIOMetadata metadata, int DPI) throws IIOInvalidTreeException { double INCH_2_CM = 2.54;
When the code is executed, a PNG file is created with 400 DPI and a disk size of 168 MB ; it's too much.
Is there a way or options that I can use to save a smaller PNG?
I used to have a 1.20 GB TIFF file, and when I converted it to PNG using imagemagick at 400 DPI, the resulting file size was only 700 KB.
So, I think I could save the above file.
Can pngj help me? Since I now have a png file that I can read in the pngj library.
Mihir
source share