How to read and write image file in Java

Image Processing Project

In this project we will learn to read and write image file using Java programming language.

Open a new file and name it MyImage.java

It is important that you save the source code file in .java format.

To read and write image file we have to import the File class. For this we will write:

import java.io.File;

When we perform read/write operations, also known as I/O or Input/Output operation, errors may occur. So, to handle errors we use the IOException class. For this we write:

import java.io.IOException;

To hold the image we create the BufferedImage object. For this we import the BufferedImage class.

import java.awt.image.BufferedImage;

To perform the image read write operation we will import the ImageIO class. For this we will write:

import javax.imageio.ImageIO;

Creating class

Now we will create our MyImage class. For this we will write:

public class MyImage{
}

Note! Since our file name is MyImage.java so, our class must have the name MyImage.

The main() method

Now, inside this class we will define the main() function and since we are going to perform IO operation so, we will add the throws IOException right next to the main() function.

So, our code will now look like the following:

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

Variables

The image we are going to read has a width 963px and height 640px. So, we will declare two variables inside the main() function to hold the dimension of the image.

int width = 963;    //width of the image
int height = 640; //height of the image

Next we create a BufferedImage variable image and File variable f and set both of them to null.

BufferedImage image = null;
File f = null;

Now, our code will look something like this:

public class MyImage{
public static void main(String args[])throws IOException{
int width = 963; //width of the image
int height = 640; //height of the image
BufferedImage image = null;
File f = null;
}
}

Reading Image

Now we will read the image file.

Please note, while performing read/write operation it is always suggested to use the try-catch block. This is because IO operation can generate exception (error) and to take care of that IO exception we need a exception handling code. If we don't use the try-catch block then our code will simply crash when an exception (error) will occur.

So, we will write

try{
// some code goes here...
}catch(IOException e){
// some code goes here...
}

Inside the try body we will create an object of File class and pass as parameter the image file path.

f = new File("D:\\Image\\Taj.jpg"); //image file path

Note! The image file we are using in the above line is named as Taj.jpg and it is in Image folder which is inside the D: drive on a Windows PC.

Next, we will create an object of BufferedImage type and pass as parameter the width, height and image int type.

image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Note! TYPE_INT_ARGB means that we are representing the Alpha, Red, Green and Blue component of the image pixel using 8 bit integer value.

Next, we will read the image using the function read() of the ImageIO class and pass as parameter the image file path which we have stored in variable f.

image = ImageIO.read(f);

Then we will output "Reading Complete"

System.out.println("Reading complete.");

Inside the catch body we will output the error message. For this we will write:

System.out.println("Error: "+e);

Now, our code will look something like this:

public class MyImage{
public static void main(String args[])throws IOException{
int width = 963; //width of the image
int height = 640; //height of the image
BufferedImage image = null;
File f = null;

//read image
try{
f = new File("D:\\Image\\Taj.jpg"); //image file path
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Reading complete.");
}catch(IOException e){
System.out.println("Error: "+e);
}
}
}

Writing Image

Similarly, to write the image as a file we will again use the try-catch block. We will first create an object of File type and pass as parameter the image file path where we want to write (save) the image. For this we will write:

f = new File("D:\\Image\\Output.jpg");  //output file path

Next, we will write the image using the write() function of the ImageIO class. For this we will write:

ImageIO.write(image, "jpg", f);

Note! image is the variable that stores the image that we want to write (save). "jpg" is the destination file extension. And f is the variable that stores the destination file path name.

Then we will output "Writing Complete"

System.out.println("Writing complete.");

Inside the catch body we will output the error message. For this we will write:

System.out.println("Error: "+e);

Final code

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

public class MyImage{
public static void main(String args[])throws IOException{
int width = 963; //width of the image
int height = 640; //height of the image
BufferedImage image = null;
File f = null;

//read image
try{
f = new File("D:\\Image\\Taj.jpg"); //image file path
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Reading complete.");
}catch(IOException e){
System.out.println("Error: "+e);
}

//write image
try{
f = new File("D:\\Image\\Output.jpg"); //output file path
ImageIO.write(image, "jpg", f);
System.out.println("Writing complete.");
}catch(IOException e){
System.out.println("Error: "+e);
}
}//main() ends here
}//class ends here

Another way to write the above code

/**
* File: ReadWriteImage.java
*
* Description:
* Read and write image.
* @author Yusuf Shakeel
* Date: 26-01-2014 sun
*
* www.github.com/yusufshakeel/Java-Image-Processing-Project
*/

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

public class MyImage{
public static void main(String args[])throws IOException{
BufferedImage image = null;
File f = null;

//read image file
try{
f = new File("D:\\Image\\Taj.jpg");
image = ImageIO.read(f);
}catch(IOException e){
System.out.println("Error: "+e);
}

//write image
try{
f = new File("D:\\Image\\Output.jpg");
ImageIO.write(image, "jpg", f);
}catch(IOException e){
System.out.println("Error: "+e);
}
}//main() ends here
}//class ends here

You can use any of the two codes to read and write image file in Java.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT