Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Java ImageIO API with PNG image compression

Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.


Posts: 43081
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
    Bookmark Topic Watch Topic
  • New Topic