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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java ImageIO API with PNG image compression

 
Greenhorn
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.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
 
Rancher
Posts: 43081
77
  • 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