简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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