Good Day!
I'am referring to the code below to compress an image file.
Currently, I'm doing a struts project where in I have to compress a big image 
file before it could be uploaded. I would like to ask for help on how to call 
the image compression class in my action...Thank you very much.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;

public class ImageKit {
static {
ImageIO.setUseCache(false);
}

//Tested buffering. Reads 1000bytes/call for jpeg (200 for png, 100 for gif)
public static BufferedImage read(InputStream in) throws IOException {
BufferedImage image = ImageIO.read(in);
if (image == null)
throw new IOException("Read fails");
return image;
}

public static BufferedImage read(byte[] bytes) {
try {
return read(new ByteArrayInputStream(bytes));
} catch(IOException e) {
throw new RuntimeException(e);
}
}

//quality means jpeg output, if quality is < 0 ==> use default quality
public static void write(BufferedImage image, float quality, OutputStream out) 
throws IOException {
Iterator writers = ImageIO.getImageWritersBySuffix("jpeg");
if (!writers.hasNext())
throw new IllegalStateException("No writers found");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (quality >= 0) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
}
writer.write(null, new IIOImage(image, null, null), param);
ios.close();
writer.dispose();
}

public static byte[] toByteArray(BufferedImage image, float quality) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream(50000);
write(image, quality, out);
return out.toByteArray();
} catch(IOException e) {
throw new RuntimeException(e);
}
}

public static BufferedImage compress(BufferedImage image, float quality) {
return read(toByteArray(image, quality));
}

public static void flush(BufferedImage image) {
try {
if (image != null)
image.flush();
} catch (NullPointerException e) {
//bug in sun's code
}

}
}


DEMO:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.text.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class CompressedView extends JComponent {
private BufferedImage originalImage, compressedImage;
private float quality=0.1f;
private int compressedSize;

public float getQuality() {
return quality;
}

public int getCompressedSize() {
return compressedSize;
}

public void setQuality(float quality) {
this.quality = quality;
calculateCompressedImage();
}

private void calculateCompressedImage() {
compressedImage = null;
if (originalImage == null) {
compressedSize = 0;
} else {
byte[] buff = ImageKit.toByteArray(originalImage, quality);
compressedImage = ImageKit.read(buff);
compressedSize = buff.length;
}
repaint();
}

public void setImage(BufferedImage image) {
originalImage = image;
calculateCompressedImage();
}

public int getImageWidth() {
return (originalImage == null) ? 0 : originalImage.getWidth();
}

public int getImageHeight() {
return (originalImage == null) ? 0 : originalImage.getHeight();
}

public Dimension getPreferredSize() {
return new Dimension(getImageWidth()*2, getImageHeight());
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (originalImage == null)
return;
g.drawImage(originalImage, 0, 0, null);
g.drawImage(compressedImage, originalImage.getWidth(), 0, null);
}

public static void main(String[] args) throws IOException {
URL url = new URL("http://today.java.net/jag/bio/JagHeadshot-small.jpg";);
BufferedImage image = ImageIO.read(url);
final CompressedView view = new CompressedView();
view.setImage(image);
final JLabel percent = new JLabel();
final JLabel size = new JLabel();
JSlider slider = new JSlider();
slider.addChangeListener(new ChangeListener(){
NumberFormat format = NumberFormat.getIntegerInstance();

public void stateChanged(ChangeEvent e) {
JSlider sl = (JSlider) e.getSource();
percent.setText(sl.getValue() + "%");
if (!sl.getValueIsAdjusting()) {
view.setQuality(sl.getValue()/100f);
size.setText(format.format(view.getCompressedSize()) + " bytes");
}
}
});
slider.setValue((int)(100* view.getQuality()));
JToolBar tb = new JToolBar();
tb.add(percent);
tb.add(slider);
tb.add(size);
JFrame f = new JFrame("CompressedView");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = f.getContentPane();
cp.add(tb, BorderLayout.NORTH);
cp.add(view);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
[Message sent by forum member 'ketm' (ketm)]

http://forums.java.net/jive/thread.jspa?messageID=159567

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to