gpt4 book ai didi

java - 从图像路径保存图像

转载 作者:行者123 更新时间:2023-12-01 14:44:39 25 4
gpt4 key购买 nike

我使用 JFileChooser 实现了此代码来浏览图像,但问题是我无法实现在本地磁盘上保存图像的代码。或者如果可能的话,我想直接在新的 JFrame 类中显示该图像,这将是一个动态链接

private void btnBrowseVideo1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
mediaUrl = null;
String path = "";

path = fileChooser.getSelectedFile().toString();
path = path.trim();

// System.out.println("URI : "+mediaUrl);
if (path.endsWith(".jpg") || path.endsWith(".JPG")) {

lblBrowseImage.setText(path);
} else {
JOptionPane.showMessageDialog(this, "SELECT .jpg FILE!!!!");
}

}

最佳答案

我希望我对这个问题的解释是正确的。看ImageIO用于加载和保存图像的 read()write() 方法。另请参阅Working with ImagesHow to Use Labels教程以获取更多详细信息和示例。

为了简单起见,下面是一个在标准对话框中显示用户选择的图像的示例:

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ShowImage {
private static void createAndShowUI() {
final JFrame frame = new JFrame("Load Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton loadButton = new JButton("Display Image");
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(
System.getProperty("user.home"));
fc.addChoosableFileFilter(new FileNameExtensionFilter(
"Image files", new String[] { "png", "jpg", "jpeg",
"gif" }));
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
try {
Image image = ImageIO.read(fc.getSelectedFile());
if (image != null) {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.add(new JLabel(fc.getSelectedFile().toString()),
BorderLayout.NORTH);
panel.add(new JLabel(new ImageIcon(image)));
JOptionPane.showMessageDialog(frame, panel);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});

frame.add(loadButton);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
createAndShowUI();
}
});
}
}

关于java - 从图像路径保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com