从 .jar 文件中获取图像 [英] Getting images from a .jar file

查看:38
本文介绍了从 .jar 文件中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Eclipse 中导出可执行 jar 文件时,我需要让 res 文件夹进行编译,当我使用 getClass().getResource() 方法时,它不起作用.

I need to get res folder to compile when I export a executable jar file in eclipse also when I use the getClass().getResource() method it doesn't work.

当前阅读图片代码

public Image loadImage(String fileName) {
    return new ImageIcon(fileName).getImage();
}

无效的代码

public Image loadImage(String fileName) {
    return new ImageIcon(getClass().getResource(fileName).getImage();
}

推荐答案

我现在已经解决了这个问题 - 这是有效的代码

I have now fixed the problem - this is the code that works

 public BufferedImage loadImage(String fileName){

    BufferedImage buff = null;
    try {
        buff = ImageIO.read(getClass().getResourceAsStream(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return buff;

}

fileName 的值只是一个图像名称,例如BufferedImage img = loadImage("background.png");

The value of fileName is just an image name eg BufferedImage img = loadImage("background.png");

感谢大家的帮助.

这篇关于从 .jar 文件中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆