Here you see a code sample to read an image file. This code will work either the image file is located in a file folder or inside a jar file. You can use javax.imageio.ImageIO
class to read the image file.
package org.kodejava.awt;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class ReadingImage {
public static void main(String[] args) {
ReadingImage demo = new ReadingImage();
demo.getImage();
}
public void getImage() {
try {
InputStream is = getClass().getResourceAsStream("/kodejava.png");
BufferedImage image = ImageIO.read(is);
// Do something with the image.
} catch (IOException e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I use Proxy class to configure HTTP and SOCKS proxies in Java? - March 27, 2025
- How do I retrieve network interface information using NetworkInterface in Java? - March 26, 2025
- How do I work with InetAddress to resolve IP addresses in Java? - March 25, 2025