jar文件中的ImageIO [英] ImageIO inside jar file
问题描述
好的,所以我正在尝试为我的介绍屏幕制作一个API用于多个游戏,我花了最后5-6个小时试图找出这个问题。
Okay so I'm trying to make an API for my intro screen to be used across multiple games and I've spent the last 5-6 hours trying to figure out this 1 problem.
假设我在我的游戏中引用了API.jar,我试图调用方法getByteArray(),它应该将帧加载为BufferedImage然后返回byte []数据。尝试加载图像时,我经常得到input == null。
Lets say that I have the API.jar referenced in on of my games and I'm trying to call the method getByteArray() which is supposed to load the frame as a BufferedImage then return the byte[] data. I constantly get input == null when attempting to load the image.
public byte[] getByteArray() throws IOException {
// Open Image
String url = "/com/jumpbuttonstudio/api/resources/intro/Frame (" + current_frame + ").jpg";
System.out.println("Loading: " + url);
BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream(url));
DataBufferByte data = (DataBufferByte) img.getRaster().getDataBuffer();
return (data.getData());
}
我真的需要帮助,如果需要,我可以提供更多信息。
I really need help with this, I can provide more information if required.
上面的代码在API.jar中,所有图像都是如此。
The code above is in the API.jar and so are all the images.
推荐答案
如果使用 Class.getResourceAsStream(path)
,路径必须以 /
(否则它被视为相对于类包的路径)。如果您使用 ClassLoader.getResourceAsStream(path)
,则路径不得以 /
开头。
If you use Class.getResourceAsStream(path)
, the path must start with a /
(otherwise it's considered as a path relative to the class package). If you use ClassLoader.getResourceAsStream(path)
, the path must NOT start with a /
.
这篇关于jar文件中的ImageIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!