【问题标题】:save image from image path从图像路径保存图像
【发布时间】:2013-03-21 22:58:28
【问题描述】:

我使用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!!!!");
    }

}

【问题讨论】:

    标签: java image swing netbeans jfilechooser


    【解决方案1】:

    我希望我对问题的解释是正确的。查看 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();
                }
            });
        }
    }
    

    【讨论】:

    • 看到我有一个在按钮操作上选择图像的 gui,我已经发布了该代码,现在我还创建了一个新的 jframe.class,按钮上有 2 个 jlabel,单击这个新的 jframe.class 已加载jlabel1 显示具有固定路径的图像我希望 jlabel2 显示我上面的代码选择的图像
    • @Akshay 我不确定是什么问题。您可以使用setIconJLabel 上设置图标,即:jlabel2.setIcon(new ImageIcon(image));。您可以看到弹出带有两个标签的面板的最后一次编辑。
    猜你喜欢
    • 2020-01-25
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2018-08-11
    • 2020-08-22
    相关资源
    最近更新 更多