Help for Java ImageIO PNG image

N

nick

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
 
K

Knute Johnson

nick said:
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

I looked at this a while back. There is no ImageWriteParam subclass for
PNG. Support for PNG appears not to be complete as of now. Your
options are to either write one of your own (sounds like a big job to
me), use the generic writing capabilities or do it manually. I don't
know what support is provided in JAI, so you might look there.
 
N

nick

Thanks Knute


Please let me know if you have any other information about working on
PNG images using Java




nick said:
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.

Nek

I looked at this a while back.  There is no ImageWriteParam subclass for
PNG.  Support for PNG appears not to be complete as of now.  Your
options are to either write one of your own (sounds like a big job to
me), use the generic writing capabilities or do it manually.  I don't
know what support is provided in JAI, so you might look there.

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
             ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access- Hide quoted text -

- Show quoted text -
 
R

Roedy Green

}




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

Tell us the line the exception occurred on. That is a big hint.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer
 
R

Roedy Green

import com.sun.media.imageioimpl.plugins.png.*; // had to comment
this as this package does not exist
//import com.sun.imageio.plugins.png;

That is correct. You don't access these classes directly. You access
them through the generic JAI interface. It is JAI's job to find the
implementing classes. They are bundled with the JDK/JRE. Check with
Wassup properties to see if you did something to get he classes off
the internal classpath.

see http://mindprod.com/applet/wassup.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer
 
R

Roedy Green

Please let me know if you have any other information about working on
PNG images using Java

see http://mindprod.com/jgloss/png.html
http://mindprod.com/jgloss/imageio.html
http://mindprod.com/applet/masker.html

Masker creates a bit map in RAM, then writes it out as a PNG file with
transparency. Source code provided for your delectation.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer
 
N

nick

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
 
J

John B. Matthews

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

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

Please give your suggestions on these exceptions

I means that your attempt to set explicit compression parameters is not
supported by the PNG implementation you have installed. Note that the
default compression mode is ImageWriteParam.MODE_COPY_FROM_METADATA. As
PNG is a lossless compression scheme, setting quality is probably
meaningless anyway:

<http://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression>

Why don't you adapt the approach Roedy kindly suggested above.

[Please don't top-post.]
[Please trim signatures.]
[Please wrap and indent examples more carefully.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top