How do I read image file?

Category: java.awt, viewed: 35K time(s).

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.

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ReadingImage {
    public void getImage() {
        try {
            BufferedImage image = 				 
                ImageIO.read(getClass().getResource("Sunset.jpg"));

            // Do something with the image.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {		
        ReadingImage ri = new ReadingImage();
        ri.getImage();
    }
}
Powered by Disqus