Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Java ImageIO API with PNG image compression  RSS feed

 
nek thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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: 42974
76
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!