如何将图像保存在设定的位置?

How to save an image in a set location?

我正在尝试将调整大小的图片保存到用户的桌面,但不确定如何操作。

到目前为止,这是我的代码:

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;
            }
        }
    });

上面的代码调整了所选图像的大小以适合 JFrame 的大小,然后将其设置为 JLabel

这一切都很好,但我也想将文件输出到一个设定的位置,让我们说到用户桌面,这样更容易。我目前正在查看输出流,但不太了解它。

任何帮助都会很棒。

JLabel...

获取当前 Icon
Icon icon = imgBg.getIcon();

将图标绘制成 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();

将图像保存到文件...

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

(是的,您可以使用 JFileChooser 来选择文件 location/name)

您还应该查看 this 以获得更好的缩放图像的示例,这样,您可以将 BufferedImage 缩放到另一个 BufferedImage 并省去拥有的麻烦重新绘制 Icon

您可能还想看看 Writing/Saving an Image

这是一个从Web保存图片到本地的例子

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();  
    }  
}  

希望对您有所帮助!