• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help for java ImageIO API for PNG Images  RSS feed

 
nek thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will do so.

As to your question, what do you need those imports for? Doesn't the code work as it is?
 
nek thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply


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


<CODE>

java.lang.UnsupportedOperationException : Compression not supported
at Javax.ImageWriteParam.setCompressionMode<Unknown Source>
at TestWriter.main<TestWriter.java:50>

<code>


Please give your suggestions on these exceptions


Thanks


Nek
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying to use CODE tags, but please make sure you're doing it right - they use square brackets, not angle brackets, and the closing tag is missing a slash. The easiest way to use them is to select the text you want in the CODE section, and then click the "CODE" button.

PNG does not support compression - it is a lossless image format. Only JPEGs can be compressed.
 
nek thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply

Please suggest how to do PNG compression using JAI /javaImageIO API


Thanks

Nek
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:PNG does not support compression - it is a lossless image format. Only JPEGs can be compressed.
 
Bill Zelan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
Ulf Dittmer wrote:PNG does not support compression - it is a lossless image format. Only JPEGs can be compressed.


http://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression wrote:
PNG uses a non-patented lossless data compression method known as DEFLATE, which is the same algorithm used in the zlib compression library.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!