Skip to main content

Help for Java ImageIO Api -PNG Compression code

7 replies [Last post]
Joined: 2009-02-11
Points: 0

Hello people

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

Here is the code,

import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import*; // 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");
Iterator writers;
BufferedImage bufferedImage;
ImageOutputStream imageOutputStream;
ImageWriter imageWriter;
ImageWriteParam pngparams;

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

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

// 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
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

And, I get the following exception, package does not exist

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



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-02-11
Points: 0

Please suggest what does it mean by

(you don't get an error when compiling I bet you have jai_imageio.jar
in path (e.g. un Eclipse) while compiling but not when running the JVM.


and how can I resolve this issue, as apparently I donot get problems while compiling code, but I do get Exceptions when I run this code? is that Runtime Exception, or some adds are not added properly

Joined: 2006-07-10
Points: 0

I'm not sure about this. Normally the import statement is not necessary
because you don't use any class from this package directly. During runtime
you should get either the PNGImageReader or an Exception when calling (when there is no ImageReader for PNG in classpath).
So this seems a little bit strange to me and I asked if the first "Exception"
was an error when compile your code.

Be sure to have the jai_imageio.jar in classpath to support PNG. The
jai_imageio-Plugin for PNG supports compression.

I don't know if J2SE supports PNG images. Maybe you don't have
jai_imageio.jar in your classpath during runtime but in your Eclipse project?
Maybe the J2SE PNG codec doesn't support PNG compression.

Joined: 2009-02-11
Points: 0

Thanks for your reply.

Is there any way to find out if J2SE PNG codec doesn't support PNG compression?

Please give your suggestions



Joined: 2006-07-10
Points: 0

Write a Java program that iterates through the PNGImageWriters and print out
the full qualified class name (including package name). The program can also
set the compression inside try/catch to check if compression is supported.

Start it first without jai_imageio.jar in the classpath and then again with
jai_imageio.jar. If the first run gives no PNGImageWriter then J2SE doesn't
support PNG. If you get one PNGImageWriter without jai_imaeio.jar and
two (or more) with jai_imageio.jar, choose the PNGImageWriter that supports

Maybe you have an old version of jai_imageio.jar or you have other imaging
libs in your classpath that plug into imageio. You will find it out when you
check it.

Joined: 2006-07-10
Points: 0

If you don't get an error when compiling I bet you have jai_imageio.jar
in path (e.g. un Eclipse) while compiling but not when running the JVM.

You don't cast to PngImageWriteParam, so you don't need this import
statement. Remove the import statement and you will see you
don't get a PngImageWriter because it is not part of JRE.
will fail.

Joined: 2009-02-11
Points: 0

Thanks for your reply

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

saying java.lang.UnsupportedOperationException : Compression not supported

at Javax.ImageWriteParam.setCompressionMode
at TestWriter.main

Please give your suggestions on these exceptions



Joined: 2006-07-10
Points: 0

I don't know why you get this Exception.

According to your code posted this means you got a PNGImageWriter.
Because J2SE doesn't support PNG this means you got it from the
jai_imagio.jar so this is in your classpath.

PNG supports compression: