简体   繁体   English

jar文件中的ImageIO

[英]ImageIO inside jar file

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用于多个游戏,我花了最后5-6个小时试图弄清楚这个问题。

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. 假设我在我的游戏中引用了API.jar,并且我试图调用方法getByteArray(),它应该将帧加载为BufferedImage然后返回byte []数据。 I constantly get input == null when attempting to load the image. 在尝试加载图像时,我经常得到input == null。

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. 我真的需要帮助,如果需要,我可以提供更多信息。

NOTE 注意

The code above is in the API.jar and so are all the images. 上面的代码在API.jar中,所有图像都是如此。

If you use Class.getResourceAsStream(path) , the path must start with a / (otherwise it's considered as a path relative to the class package). 如果使用Class.getResourceAsStream(path) ,则路径必须以/开头(否则它被视为相对于类包的路径)。 If you use ClassLoader.getResourceAsStream(path) , the path must NOT start with a / . 如果使用ClassLoader.getResourceAsStream(path) ,则路径不能以/开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM