Hi Friend,
Try the following code:
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class SaveImage extends Component {
int index;
BufferedImage bi, bufferImage;
int w, h;
static JButton button;
public SaveImage() {
try {
bi = ImageIO.read(new File("
C://barbie.jpg";));
w = bi.getWidth(null);
h = bi.getHeight(null);
if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics big = bi2.getGraphics();
big.drawImage(bi, 0, 0, null);
bufferImage = bi = bi2;
}
} catch (IOException e) {
System.out.println("Image could not be read");
System.exit(1);
}
button=new JButton("Save");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
File saveFile = new File("image." + "jpg");
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(saveFile);
int val = chooser.showSaveDialog(button);
if (val == JFileChooser.APPROVE_OPTION) {
saveFile = chooser.getSelectedFile();
try {
ImageIO.write(bufferImage, "jpg", saveFile);
} catch (IOException ex) {
}
}
}
});
}
public Dimension getPreferredSize() {
return new Dimension(w, h);
}
public void paint(Graphics g) {
filterImage();
g.drawImage(bufferImage, 0, 0, null);
}
public void filterImage(){
BufferedImageOp op = null;
switch (index){
case 0:
bufferImage = bi;
return;
case 1:
}
bufferImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
op.filter(bi, bufferImage);
}
public static void main(String s[]) {
JFrame f = new JFrame("Save Image");
SaveImage si = new SaveImage();
f.add("Center", si);
JPanel panel = new JPanel();
panel.add(button);
f.add("South", panel);
f.pack();
f.setVisible(true);
}
}
Hope that it will be helpful for you.
Thanks