jar 文件中的 ImageIO [英] ImageIO inside jar file
问题描述
好的,所以我正在尝试为我的介绍屏幕制作一个 API,以便在多个游戏中使用,并且在过去的 5-6 个小时里我一直在试图找出这 1 个问题.
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屋!