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

时间:2015-04-23 04:47:02

标签: java image swing save outputstream

我正在尝试将已调整大小的图片保存到用户的桌面,但不知道该怎么做。

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

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

这一切都运行良好,但我也想将文件输出到设置位置,让我们对用户桌面说,以使其更容易。我目前正在研究输出流,但不能完全了解它。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

Icon ...

获取当前JLabel
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来选择文件位置/名称)

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

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

答案 1 :(得分:0)

这是一个关于将图像从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();  
    }  
}  

希望这对你有所帮助!