【问题标题】:Loading images using an InputStream使用 InputStream 加载图像
【发布时间】:2015-10-25 06:43:10
【问题描述】:

在我的 IDE 中,我可以获取资源文件夹中图像的路径,并通过执行以下操作使该路径成为新的文件对象:

URL imagePath = getClass().getResource("/image.png");
try
{
    //Convert the URLs into URIs and make a file object with that path
    File image = new File(imagePath.toURI());

}
catch (URISyntaxException e)
{
    e.printStackTrace();
}

但是当我为我的程序制作一个 jar 文件时,我得到错误 URI 不是分层的。我做了一些研究,发现我必须使用 getResourceAsStream() 方法创建一个 InputStream。但我不知道如何使它适用于图像。我只需要能够从我的资源文件夹中获取图像的路径。即使它是一个罐子,我该怎么做。

【问题讨论】:

    标签: java jar


    【解决方案1】:

    不要将URL 转换为File 引用,这违背了嵌入资源的意义,嵌入资源只是压缩文件中的条目,因此不能将它们视为File .

    改为使用ImageIO.read(imagePath)之类的东西

    详情请见Reading/Loading an Image

    【讨论】:

      【解决方案2】:

      我认为在这种情况下最好的解决方案是直接向ClassLoader 询问InputStream(使用ClassLoader.getResourceAsStream)并将其传递给ImageIO.read

      这是一个完整的例子。

      import java.awt.image.BufferedImage;
      import java.io.IOException;
      import java.io.InputStream;
      import javax.imageio.ImageIO;
      
      public final class Main {
      
          public static void main(final String[] args) {
              final ClassLoader clsldr = Main.class.getClassLoader();
              for (final String path : args) {
                  try {
                      InputStream is = null;
                      BufferedImage image = null;
                      try {
                          is = clsldr.getResourceAsStream(path);
                          if (is != null) {
                              image = ImageIO.read(is);
                              if (image != null) {
                                  // Do something with the image.
                                  System.out.printf("%s: %d x %d%n",
                                                    path,
                                                    image.getWidth(),
                                                    image.getHeight());
                              } else {
                                  System.err.printf("error: %s: %s%n",
                                                    path,
                                                    "not a valid image file");
                              }
                          } else {
                              System.err.printf("error: %s: %s%n",
                                                path,
                                                "no such resource");
                          }
                      } finally {
                          if (is != null) {
                              is.close();
                          }
                      }
                  } catch (final IOException e) {
                      System.err.printf("error: %s: %s%n", path, e.getMessage());
                  }
              }
          }
      
      }
      

      假设我有一个图片文件photo.jpg 然后编译上面的文件并创建一个这样的JAR文件

      $ javac *.java
      $ jar -cfe example.jar Main *.class photo.jpg
      

      然后我可以像这样运行程序并得到以下输出。

      $ java -jar example NoSuchThing Main.class photo.jpg
      error: NoSuchThing: no such resource
      error: Main.class: not a valid image file
      photo.jpg: 328 x 328
      

      【讨论】:

      • 如果我使用 InputStream,您的回答会很好。但是 MadProgrammer 指出我不必使用 InputStream,我收到错误,因为我将 URL 转换为 URI,然后转换为文件。所以就我而言,他的答案更简单。
      猜你喜欢
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2016-06-23
      • 2013-09-16
      • 2012-11-02
      • 2011-08-25
      相关资源
      最近更新 更多