ImageIO inside jar file

Deadly 提交于 2019-12-31 07:07:18

问题


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.

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.

NOTE

The code above is in the API.jar and so are all the images.


回答1:


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 /.



来源:https://stackoverflow.com/questions/20761413/imageio-inside-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!