【问题标题】:How to store a image and rename that image at the same time while storing?如何在存储时同时存储图像并重命名该图像?
【发布时间】:2014-09-10 01:02:39
【问题描述】:

想创建一个JFileChooser 和一个JTextField然后从任何文件夹浏览图像。给出了路径,因此它将重命名图像,图像名称将是jtextfield.getText();,然后存储在我指定的路径中。 我知道这很难实施。我做了一部分。

JFileChooserjtextfield 已创建。图像存储在我指定的路径中。但它不是重命名图像。 (重命名图片应该是jtextfield.jpg)

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;

public class ImgClass {

    public ImgClass() {
        final JFrame frame = new JFrame("Save Image");

                    final JTextField username = new JTextField();
            username.setBounds(105, 102, 192, 20);
            frame.add(username);
            username.setColumns(10);

        JButton saveImage = new JButton("Browse");
        saveImage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "JPG, GIF, and PNG Images", "jpg", "gif", "png");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(frame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    System.out.println("You choose to open this file: "
                            + file.getName());
                    BufferedImage image;
                    try {
                        image = ImageIO.read(file);
                       ImageIO.write(image, "jpg",new File("D:\\Img\\" + username.getText()+".jpg"));
                    } catch (IOException ex) {
                        Logger.getLogger(ImgClass.class.getName()).log(Level.SEVERE, null, ex);
                    }              
                }
            }
        });



        frame.add(saveImage);
        frame.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImgClass saveImageFile = new ImgClass();
            }
        });
    }
}

【问题讨论】:

  • java-ee 标签已移除。
  • 你应该使用showSaveDialog来保存

标签: java image swing file jfilechooser


【解决方案1】:

您在写入文件时使用了相同的文件名。考虑从 JTextField 获取文本并使用它。

String newName = username.getText();

// no not this!
// File newFile = new File("D:\\Img\\" + file.getName()));

// but this!
file newFile = new File("D:\\Img\\" + newName));
// write with newFile

编辑
您在评论中声明:

好方法。但问题是:如果我在 jtextfield 中写“abc”,那么图像将文件格式存储错误。如果我在 jtextfield 中写 abc.jpg,那么它工作正常。请建议我进行任何修改。

我挑战你编写一小段代码,检查字符串(上面名为 newName),看看它是否以 ".jpg" 或类似的东西(例如 ".JPG")结尾,如果不是,然后为字符串提供这样的结尾。我有信心你能解决这个问题。 ;)

【讨论】:

  • 好方法。但问题是:如果我在 jtextfield 中写“abc”,那么图像将文件格式存储错误。如果我在 jtextfield 中写 abc.jpg,那么它工作正常。请建议我进行任何修改。
  • @IndranilBanerjee:很容易修复,我挑战你这样做。请参阅上面的编辑。
  • @IndranilBanerjee:如果您在此尝试中遇到困难,请显示您所做的工作以编辑您的问题,然后在此处向我发表评论。我自己,我会使用 String 方法.endsWith(...)
  • @IndranilBanerjee:或者你可以look it up
  • @IndranilBanerjee 扩展名无关紧要,你传递给 ImageIO 的内容将决定实际使用的格式
猜你喜欢
  • 2014-11-25
  • 2019-05-07
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多