
Using This Code I Compressed A JPEG Image And the Original Size of the image is 257kb and The Compressed Image Size Is 27kb How Can I Decompress It Please Give Me The "SOURCE CODE" And Hee is my Source Code.... Please kindly Help Me?
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Iterator;
import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageOutputStream; import javax.imageio.stream.ImageOutputStream; import javax.swing.JFrame;
import com.sun.media.jai.widget.DisplayJAI;
/* * Shows how to save an image in JPEG with different compression factors. * Based on code from http://forums.java.net/jive/thread.jspa?messageID=243429 * and from http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java */ public class DemoJPEGCompression { /* * Application starting point, open an image and save it in JPEG with a * compression factor. */ public static void main(String[] args) throws IOException { // Load the image (it is hard-coded here to make the code simpler). String imageFile = "/tmp/folhas.png"; BufferedImage i = ImageIO.read(new File(imageFile)); showImage("Original Image", i); // Show results with different compression ratio. compressAndShow(i, 0.5f); }
public static void compressAndShow(BufferedImage image, float quality) throws IOException
{
// Get a ImageWriter for jpeg format.
Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jpeg");
if (!writers.hasNext()) throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
// Create the ImageWriteParam to compress the image.
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
// The output will be a ByteArrayOutputStream (in memory)
ByteArrayOutputStream bos = new ByteArrayOutputStream(32768);
ImageOutputStream ios = ImageIO.createImageOutputStream(bos);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);
ios.flush(); // otherwise the buffer size will be zero!
// From the ByteArrayOutputStream create a RenderedImage.
ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());
RenderedImage out = ImageIO.read(in);
int size = bos.toByteArray().length;
showImage("Compressed to " + quality + ": " + size + " bytes", out);
// Uncomment code below to save the compressed files.
// File file = new File("compressed."+quality+".jpeg"); // FileImageOutputStream output = new FileImageOutputStream(file); // writer.setOutput(output); writer.write(null, new IIOImage(image, null,null), param); }
/*
* This method just create a JFrame to display the image. Closing the window
* will close the whole application.
*/
private static void showImage(String title,RenderedImage image)
{
JFrame f = new JFrame(title);
f.getContentPane().add(new DisplayJAI(image));
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.