5 Replies Latest reply: Aug 28, 2009 3:56 PM by 843802 RSS

    Help for Java Image IO PNG Image

    843802
      Hello people

      I need your help for program related to ImageIO API while working on PNG images.


      Here is the code,

      import java.io.*;
      import java.util.*;
      import java.awt.image.*;
      import javax.imageio.*;
      import javax.imageio.stream.ImageOutputStream;
      import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
      import com.sun.media.imageioimpl.plugins.png.*; // had to comment this as this package does not exist
      //import com.sun.imageio.plugins.png;

      public class TestWriter
      {
      public static void main(String[] args)
      throws IOException
      {
      /*
           if (args.length < 2) {
                     // Exit if both the arguments (Input File and Output File) are not provided
      System.err.println("Usage: java TestWriter c:\\in.png c:\\out.png");
      return;
      }
                */
                Iterator writers;
                BufferedImage bufferedImage;
                ImageOutputStream imageOutputStream;          
                ImageWriter imageWriter;
                ImageWriteParam pngparams;

                // Read an image from the disk (First Argument)
                //bufferedImage = ImageIO.read(new File(args[0]));
      bufferedImage = ImageIO.read(new File("Download.png"));
                // Get all the PNG writers
                writers = ImageIO.getImageWritersByFormatName( "png" );

                // Fetch the first writer in the list
                imageWriter = (ImageWriter) writers.next();

                // Just to confirm that the writer in use is CLibPNGImageWriter
                System.out.println("\n Writer used : " + imageWriter.getClass().getName() + "\n");

                // Specify the parameters according to those the output file will be written

                // Get Default parameters
                pngparams = imageWriter.getDefaultWriteParam();
                
                // Define compression mode
                pngparams.setCompressionMode( javax.imageio.ImageWriteParam.MODE_EXPLICIT );

                // Define compression quality
                pngparams.setCompressionQuality( 0.5F );

                // Define progressive mode
                pngparams.setProgressiveMode( javax.imageio.ImageWriteParam.MODE_COPY_FROM_METADATA );

                // Deine destination type - used the ColorModel and SampleModel of the Input Image
                pngparams.setDestinationType(
                          new ImageTypeSpecifier( bufferedImage.getColorModel(), bufferedImage.getSampleModel() ) );

                // Set the output stream to Second Argument
                //imageOutputStream = ImageIO.createImageOutputStream( new FileOutputStream(args[1]) );
                imageOutputStream = ImageIO.createImageOutputStream( new FileOutputStream("PNGCopy.png") );
                imageWriter.setOutput( imageOutputStream );

                // Write the changed Image
                imageWriter.write( null, new IIOImage( bufferedImage, null, null ), pngparams );

                // Close the streams
                imageOutputStream.close();
                imageWriter.dispose();
      }
      }




      And, I get the following exception, package com.sun.media.imageioimpl.plugins.png does not exist



      Please guide me if I need to add specific JARs to use this code involving PNG images.





      Thanks


      Nek
        • 1. Re: Help for Java Image IO PNG Image
          843802
          The package
          com.sun.media.imageioimpl.plugins.png.*
          is a package of JAI-ImageIO. [This is|https://jai-imageio.dev.java.net/] the homepage on sourceforge and [this is|https://jai-imageio.dev.java.net/binary-builds.html] the binary builds where you can install it or obtain the jar.
          • 2. Re: Help for Java Image IO PNG Image
            843802
            Thanks for your suggestions

            Although, when I remove the import, I get another exception

            java.lang.UnsupportedOperationException : Compression not supported
            at Javax.ImageWriteParam.setCompressionMode<Unknown Source>
            at TestWriter.main<TestWriter.java:50>
            Please give your suggestions on these exceptions

            Thanks

            Nek
            • 3. Re: Help for Java Image IO PNG Image
              843802
              PNG is a lossless image compresson format. Normally you can't set the compression quality on it like a JPEG since it has only one quality --> 100% .

              Basically, these two lines
              // Define compression mode
              pngparams.setCompressionMode( javax.imageio.ImageWriteParam.MODE_EXPLICIT );
              
              // Define compression quality
              pngparams.setCompressionQuality( 0.5F );
              are invalid. They do not apply to the PNG format using the default PNGImageWriter.
              • 4. Re: Help for Java Image IO PNG Image
                843802
                setCompressionQuality is also valid for lossless schemes. The Javadoc says the following:

                "For lossless schemes, the compression quality may be used to control the tradeoff between file size and time taken to perform the compression (for example, by optimizing row filters and setting the ZLIB compression level when writing PNG images)."
                • 5. Re: Help for Java Image IO PNG Image
                  843802
                  The original poster doesn't have JAI-ImageIO installed. That means the code was using the default png image writer that comes with the jdk. Incidentally, that png image writer doesn't support compression options as I said in my reply.

                  If I gave you the impression that setCompressionQuality is invalid for lossless schemes I apologize. That wasn't my intent.