【发布时间】: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.home的JFileChooser(我认为这是默认设置)并让用户选择路径和名称。跨度>
标签: java image swing save outputstream