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

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

问题描述

当我使用 getClass()。getResource()方法时,我需要在eclipse中导出可执行jar文件时获取res文件夹进行编译工作。

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天全站免登陆