How to Load and Display Images in Java


In this example we will show the method to load and display an image in Java, as shown below.

Source Code

package com.beginner.examples;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ReadAndWriteImgExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		
		try {
			

			//This path is an absolute path
			BufferedImage buffImg = 
					ImageIO.read(new File("C:UsersAdministratorDesktopapple.jpg"));
			
			ImageIO.write(buffImg, "jpg", new File("new.jpg"));

		    System.out.println(buffImg);
			
		} catch (IOException e) {
			
			e.printStackTrace();
			
			
		}

	}

}

Output:

BufferedImage@2c6f7ce9: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@4b71bbc9 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 500 height = 377 #numDataElements 3 dataOff[0] = 2

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments