0

I am trying to save a resized picture to the user's desktop but not sure how to do that.

Here's my code so far:

mi.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String userhome = System.getProperty("user.home");
            fileChooser = new JFileChooser(userhome + "\\Desktop");
            fileChooser.setAutoscrolls(true);
            switch (fileChooser.showOpenDialog(f)) {
            case JFileChooser.APPROVE_OPTION:
                BufferedImage img = null;
                try {
                    img = ImageIO.read(fileChooser.getSelectedFile());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                Image dimg = img.getScaledInstance(f.getWidth(),
                        f.getHeight(), Image.SCALE_SMOOTH);

                path = new ImageIcon(dimg);
                configProps.setProperty("Path", fileChooser
                        .getSelectedFile().getPath());
                imBg.setIcon(path);

                break;
            }
        }
    });

The code above resizes the imaged selected to fit the size of the JFrame then sets it to the JLabel.

This all works well but I also want to output the file to a set location lets say to the users desktop to make it easier. I'm currently looking at output stream but can't quite get my head around it.

Any help would be great.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wardas23
  • 79
  • 9
  • Paint the resulting `dimg` back to a `BufferedImage` and then use `ImageIO.write` – MadProgrammer Apr 23 '15 at 04:50
  • *"trying to save a resized picture to the user's desktop"* Don't do that. The `Desktop` directory won't exist on *nix and OS X. Better to offer the user a `JFileChooser` pointing to `user.home` (which I think is the default) and let the user choose the path and name. – Andrew Thompson Apr 23 '15 at 04:57

2 Answers2

1

Get the current Icon from the JLabel...

Icon icon = imgBg.getIcon();

Paint the icon to a BufferedImage...

BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();

Save the image to a file...

ImageIO.write(img, "png", new File("ResizedIcon.png"));

(and yes, you could use a JFileChooser to pick the file location/name)

You should also take a look at this for better examples of scaling an image, this way, you could scale the BufferedImage to another BufferedImage and save the hassle of having to re-paint the Icon

You might also like to take a look at Writing/Saving an Image

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

This is a example which is about saving images from Web to the local.

package cn.test.net;  
import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.net.HttpURLConnection;  
import java.net.URL;  
public class ImageRequest {  
    /** 
     * @param args 
     */  
    public static void main(String[] args) throws Exception {  
        //a url from web  
        URL url = new URL("http://img.hexun.com/2011-06-21/130726386.jpg");  
        //open 
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        //"GET"! 
        conn.setRequestMethod("GET");  
        //Timeout
        conn.setConnectTimeout(5 * 1000);  
        //get data by InputStream
        InputStream inStream = conn.getInputStream();  
        //to the binary , to save
        byte[] data = readInputStream(inStream);  
        //a file to save the image
        File imageFile = new File("BeautyGirl.jpg");  
        FileOutputStream outStream = new FileOutputStream(imageFile);  
        //write into it 
        outStream.write(data);  
        //close the Stream 
        outStream.close();  
    }  
    public static byte[] readInputStream(InputStream inStream) throws Exception{  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();   
        byte[] buffer = new byte[1024];  
        //every time read length,if -1 ,end
        int len = 0;  
        //a Stream read from buffer
        while( (len=inStream.read(buffer)) != -1 ){  
            //mid parameter for starting position
            outStream.write(buffer, 0, len);  
        }   
        inStream.close();  
        //return data 
        return outStream.toByteArray();  
    }  
}  

Hope this is helpful to you!

Seven
  • 89
  • 6