【问题标题】:How to save an image in a set location?如何将图像保存在设定的位置?
【发布时间】:2015-07-01 01:48:48
【问题描述】:

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

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

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

这一切都很好,但我还想将文件输出到一个设定的位置,让我们对用户桌面说,以使其更容易。我目前正在查看输出流,但无法完全理解它。

任何帮助都会很棒。

【问题讨论】:

  • 将生成的dimg 绘制回BufferedImage,然后使用ImageIO.write
  • “试图将调整大小的图片保存到用户的桌面” 不要那样做。 Desktop 目录在 *nix 和 OS X 上将不存在。最好为用户提供指向 user.homeJFileChooser(我认为这是默认设置)并让用户选择路径和名称。跨度>

标签: java image swing save outputstream


【解决方案1】:

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 来选择文件位置/名称)

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

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

【讨论】:

    【解决方案2】:

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

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2020-12-25
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多