【问题标题】:Save an Image to a File in a Applet?将图像保存到 Applet 中的文件?
【发布时间】:2011-05-11 04:41:05
【问题描述】:

所以这就是事情,我试图为一个网页游戏做一个小程序来产生“自定义”头像,这个头像是为一个国家的军队提供的,所以头像的选择取决于形象用户,图片上的边框也代表该用户所属的四边形。

所以我的计划是让他们从他们计算机中的一个文件中进行选择,然后他们选择他们所属的小队。在此之后,他们将看到图片的预览,他们可以将其保存到他们的计算机中,以便以后在游戏中使用。

我知道您可以在组件的背景上使用 Graphic 或 Graphic2D 绘制图像,但是当我想将其保存到文件时,我该怎么做?

【问题讨论】:

    标签: java image swing applet save


    【解决方案1】:

    使用JFileChooser#showSaveDialog() 要求用户选择/指定要保存的文件,然后使用ImageIO#write()BufferedImage 写入文件。

    JFileChooser fileChooser = new JFileChooser();
    if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
        ImageIO.write(bufferedImage, "JPEG", fileChooser.getSelectedFile());
    } else {
        // User pressed cancel.
    }
    

    但是需要对小程序进行签名以避免最终用户被安全警告吓到。

    【讨论】:

    • 这在未签名的小程序中是行不通的。除非签名,否则 SecurityManager 将阻止本地文件系统修改。但如果签名,这是有效的。
    • @Chris:如果小程序已签名或最终用户已接受显示在加载小程序之前显示的安全警告,那么它可以正常工作。
    • 哎呀,没意识到,对不起。不过,最终用户接受它有点吓人。
    • @Chris:不管他们在做什么,警告都会显示给所有未签名的小程序。
    【解决方案2】:

    使用插件 2 (PI2 - 1.6.0_10+) 架构 JRE 部署的小程序不需要数字代码签名。在 PI2 JRE 中,嵌入式小程序可以访问通常仅适用于 Java Web Start 应用程序的所有服务。

    此小程序感兴趣的服务是FileOpenService (FOS) 和PersistenceService (PS)。 FOS 可用于允许用户导航到文件(或者更确切地说 - 文件内容)对象并从中获取流。一旦用户对裁剪后的图像感到满意,将其保存到 PS 以供以后检索(使用 ImageIO,如前所述)。

    【讨论】:

      【解决方案3】:

      这是你可以保存内容的记事本代码,如果你将文本转换为图像,请尝试通过它

      /*Arpana*/
      
      mport javax.swing.*;
      
      import java.awt.*;
      
      import java.awt.event.*;
      
      import java.util.Scanner;
      
      import java.io.*;
      
      
      
      public class Notepad extends JFrame implements ActionListener {
      
          private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
      
          private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
      
          private Menu file = new Menu(); // our File menu
      
          // what's going in File? let's see...
      
          private MenuItem openFile = new MenuItem();  // an open option
      
          private MenuItem saveFile = new MenuItem(); // a save option
      
          private MenuItem close = new MenuItem(); // and a close option!
      
      
      
          public Notepad() {
      
              this.setSize(500, 300); // set the initial size of the window
      
             this.setTitle("Java Notepad Tutorial"); // set the title of the window
      
              setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed)
      
              this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // set a default font for the TextArea
      
              // this is why we didn't have to worry about the size of the TextArea!
      
              this.getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
      
              this.getContentPane().add(textArea);
      
      
      
              // add our menu bar into the GUI
      
              this.setMenuBar(this.menuBar);
      
              this.menuBar.add(this.file); // we'll configure this later
      
      
      
              // first off, the design of the menuBar itself. Pretty simple, all we need to do
      
             // is add a couple of menus, which will be populated later on
      
              this.file.setLabel("File");
      
      
      
              // now it's time to work with the menu. I'm only going to add a basic File menu
      
              // but you could add more!
      
      
      
              // now we can start working on the content of the menu~ this gets a little repetitive,
      
            // so please bare with me!
      
      
      
              // time for the repetitive stuff. let's add the "Open" option
      
              this.openFile.setLabel("Open"); // set the label of the menu item
      
              this.openFile.addActionListener(this); // add an action listener (so we know when it's been clicked
      
              this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // set a keyboard shortcut
      
              this.file.add(this.openFile); // add it to the "File" menu
      
      
      
              // and the save...
      
              this.saveFile.setLabel("Save");
      
              this.saveFile.addActionListener(this);
      
              this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
      
              this.file.add(this.saveFile);
      
      
      
              // and finally, the close option
      
              this.close.setLabel("Close");
      
              // along with our "CTRL+F4" shortcut to close the window, we also have
      
            // the default closer, as stated at the beginning of this tutorial.
      
              // this means that we actually have TWO shortcuts to close:
      
              // 1) the default close operation (example, Alt+F4 on Windows)
      
              // 2) CTRL+F4, which we are about to define now: (this one will appear in the label)
      
              this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
      
              this.close.addActionListener(this);
      
              this.file.add(this.close);
      
          }
      
      
      
          public void actionPerformed (ActionEvent e) {
      
              // if the source of the event was our "close" option
      
              if (e.getSource() == this.close)
      
                  this.dispose(); // dispose all resources and close the application
      
      
      
              // if the source was the "open" option
      
              else if (e.getSource() == this.openFile) {
      
                  JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
      
                  int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
      
                  // NOTE: because we are OPENing a file, we call showOpenDialog~
      
                  // if the user clicked OK, we have "APPROVE_OPTION"
      
                  // so we want to open the file
      
                  if (option == JFileChooser.APPROVE_OPTION) {
      
                      this.textArea.setText(""); // clear the TextArea before applying the file contents
      
                      try {
      
                          // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
      
                          Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
      
                          while (scan.hasNext()) // while there's still something to read
      
                              this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
      
                      } catch (Exception ex) { // catch any exceptions, and...
      
                          // ...write to the debug console
      
                          System.out.println(ex.getMessage());
      
                      }
      
                  }
      
              }
      
      
      
              // and lastly, if the source of the event was the "save" option
      
              else if (e.getSource() == this.saveFile) {
      
                  JFileChooser save = new JFileChooser(); // again, open a file chooser
      
                  int option = save.showSaveDialog(this); // similar to the open file, only this time we call
      
                  // showSaveDialog instead of showOpenDialog
      
                  // if the user clicked OK (and not cancel)
      
                  if (option == JFileChooser.APPROVE_OPTION) {
      
                      try {
      
                          // create a buffered writer to write to a file
      
                          BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
      
                        out.write(this.textArea.getText()); // write the contents of the TextArea to the file
      
                          out.close(); // close the file stream
      
                      } catch (Exception ex) { // again, catch any exceptions and...
      
                          // ...write to the debug console
      
                          System.out.println(ex.getMessage());
      
                      }
      
                  }
      
              }
      
          }
      
          // the main method, for actually creating our notepad and setting it to visible.
      
          public static void main(String args[]) {
      
            Notepad app = new Notepad();
      
              app.setVisible(true);
      
      }
      
      }
      

      【讨论】:

        【解决方案4】:

        我想我倾向于在同一页面上将 Java 与 Javascript 进行交互,并提供一个“导出”按钮,将其在本地序列化为 PNG,并将其作为下载提供(应该可以在用户不需要的情况下全部完成)刷新页面或乱七八糟)。上一个问题有一些有趣的cmets:Java applet - saving an image in a png format

        【讨论】:

          【解决方案5】:

          我在这篇文章上方发布的记事本程序提供了任何类型的加载图像,也可以保存任何类型的图像....

          所以您可以参考相同的代码来加载图像并保存图像

          【讨论】:

            猜你喜欢
            • 2023-03-28
            • 2013-05-31
            • 1970-01-01
            • 2011-05-08
            • 1970-01-01
            • 2021-07-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多