Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image compression code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i m working on Image compression using JPEG compression standard
i want some help regarding my project and the implementation of the algorithm.
If anybody is having the source code for for image compression then please help me .

Thanks
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ImageCompression {
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("c:\\test.jpg"));
// Get all the PNG writers
writers = ImageIO.getImageWritersByFormatName("jpg");

// 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("c:\\new_test.jpg"));
imageWriter.setOutput(imageOutputStream);

// Write the changed Image
imageWriter.write(null, new IIOImage(bufferedImage, null, null),
pngparams);

// Close the streams
imageOutputStream.close();
imageWriter.dispose();
}
}

I picked this code from: https://coderanch.com/t/430762/Java-General-intermediate/java/Help-java-ImageIO-API-PNG#1926836
 
Sheriff
Posts: 22778
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do know this thread was over 4 months old? http://faq.javaranch.com/java/DontWakeTheZombies

Also, http://faq.javaranch.com/java/UseCodeTags
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic