How to read content of JPEG file in Java?

In this tutorial I will explains you the code of reading the content of JPEG file in Java on the image.

How to read content of JPEG file in Java?

Example of reading the content of JPEG file in Java

Java JAI API helps the programmers to write program for handling the different types of image in the Java Program. In this article we will see how to read the JPEG image file.

We will use the javax.imageio.ImageIO class for reading the image file data into java.awt.Image object. The  javax.imageio.ImageIO class provides a method ImageIO.read(file) which actually reads the image data into the Image object.

The javax.imageio.ImageIO class provides many static methods which can be used for locating ImageReaders and ImageWriters, and performing simple encoding and decoding.

Here is the video tutorial of "Image processing in Java Example program":

Here is the important methods of the javax.imageio.ImageIO class used for reading the image:

static BufferedImage read(File input)

This method is used for reading the data into BufferedImage and it returns the BufferedImage. It takes File as input. I automatically choose one of the registered ImageReader.

static BufferedImage read(ImageInputStream stream)

This method takes the ImageInputStream   as parameter and reads the data using one of the registered ImageReader. It returns the BufferedImage.

static BufferedImage read(InputStream input)

This method is used to read the image data from the InputStream.

static BufferedImage read(URL input)

If you want to read the image from the URL then use this method of the class.

Here is the example code of reading image data from a file:

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ReadImageExampleFromFlile {

	public static void main(String[] args) throws IOException {

		//Ge the file to read
		File file = new File("DeepakKumar.jpg");
		//Read it using the read method of ImageIO class
		Image image = ImageIO.read(file);
		//Get width
		int width = image.getWidth(null);
		//Get height
		int height = image.getHeight(null);
		//Print the data
		System.out.println("Width:" + width + "  Height:" + height);
	}
}

In the above example you can see that program loads the image and then prints it width and height. After loading the program in the Image object you can perform the operations on the image.

Here is an example of reading the image file from URL:

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class ReadImageExampleFromURL {

	public static void main(String[] args) throws IOException {
		
		//Ge the url of image to read
		URL url = new URL("http://www.roseindia.net/d2/images/logo.gif");

		//Read it using the read method of ImageIO class
		Image image = ImageIO.read(url);
		//Get width
		int width = image.getWidth(null);
		//Get height
		int height = image.getHeight(null);
		//Print the data
		System.out.println("Width:" + width + "  Height:" + height);

	}

}

We should construct the URL of the image and then pass it to the ImageIO.read(url) function.

Check more tutorials at Image Processing Tutorials in Java Programming Language.