Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Imageprocessing How to read content of JPEG file in Java?

Related Tutorials


 
 

Share on Google+Share on Google+

How to read content of JPEG file in Java?

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

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2014

Related Tutorials

Discuss: How to read content of JPEG file in Java?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com