Read and Write Image file In Java

In this tutorial, we will find out the way to Read and Write images in Java.

For performing read and write operation we will import multiple classes. There are two ways to read and write an image from a Local disk or from URL. In this tutorial, we will learn how to read and write an image file from the Local Disk.

How to Read and Write Image file In Java?

Below is the step by step approach to performing our task:

  1. The first step is to make a new java file and name it as  “ImgRead_Write.java”. Then import the File class in order to perform the Read and Write operations which are inside the java.io package
    import java.io.File;

    . The File class is the representation of the file or directory pathname.

  2. When we perform read and write operation error may occur to handle we import IOException class.
    //to handle the Exception
    import java.io.IOException;
  3. Now,  we import BufferedImage class which is used to hold the image by creating its object.
    import java.awt.image.BufferedImage;
  4. In this step, we will import the ImageIO class to perform the image read-write operation.
    //importing ImageIO class
    import javax.imageio.ImageIO;
  5. Since we are performing IO operation, therefore, we will add throws IOException as shown below.
    public class ImgRead_Write {
     public static void main(String[] args) throws IOException {
                }
               }

CODE inside main() block:

The first code we will be writing inside the main variable is declaring two variables of int type to store the dimension of an image we will read. You can see the dimension of your image by going on the properties of an image.

int width = 1920;    // the width of our image
   int height = 1080;   // the height of our image

Now creating a BufferedImage variable image and File variable f and set both of them to null.

BufferedImage image = null;
   File f = null;

NOTE:  Since we are performing io operation so there may be the case that the exception occurs so to handle the exception we write code inside a try-catch block.
Now to perform read operation on the image creating an object of the File class and also passing the image path.

try{
//here D:\\Wallpaper\\img2.jpg is image path which is inside D drive Wallpaper folder and image name is img.jpg
      f = new File("D:\\Wallpepar\\img.jpg"); 

Also creating the object of the BufferedImage class and passing a parameter the width, height and image int type. Now we will read the image by using the read() function of the ImageIO class and passing the path of the file which is stored in variable f. Also printing “Read Successfully”.

  image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      image = ImageIO.read(f);
      System.out.println("Read Successfully.");
}

Now to handle the exception that may occur inside the try block.

catch(IOException e){
//printing Exception     
 System.out.println("Exception: "+e);
    }

Similarly to perform a write operation we will use a try-catch block. First, we will be creating an object of the File class and pass the image path where we want to write(save) in this program we are writing it to E drive.

//write image
    try{// where E:Output.jpg is image path where you want to write and output.jpg is an image name
      f = new File("E:Output.jpg");

Now to perform the write operation we will use write() function which is inside the ImageIO class.

ImageIO.write(image, "jpg", f);
    System.out.println("Write successfully");
  }

where the image is a variable that stores the image and that we want to write. jpg is the extension upon which it will write(save )and ” f” gives the path where an image needs to be written (save).

Also, read:

 

Now similar to read block here also we use a catch block to handle exception and printing exception.

catch(IOException e){
      System.out.println("Exception: "+e);
    }
  }
}

Complete Java code to read the image

Below is the complete code to read and write image file in Java:

import java.io.File; 
//to handle the Exception
import java.io.IOException; 
import java.awt.image.BufferedImage; 
//importing ImageIO class
import javax.imageio.ImageIO; 
    public class ImgRead_Write {
    public static void main(String[] args) throws IOException {
        
    int width = 1920;    //width of the image
    int height = 1080;   //height of the image
    BufferedImage image = null;
    File f = null;

    //read image
    try{//here D:\\Wallpaper\\img2.jpg is image path which is inside D drive Wallpaper folder and image name is img.jpg
      f = new File("D:\\Wallpepar\\img.jpg"); 
      image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      image = ImageIO.read(f);
      System.out.println("Read Successfully.");
    }catch(IOException e){
      System.out.println("Error: "+e);
    }

    //write image
    try{// where E:Output.jpg is image path where you want to write and output.jpg is an image name
      f = new File("E:Output.jpg");  
      ImageIO.write(image, "jpg", f);
      System.out.println("Write Successfully.");
    }catch(IOException e){
      System.out.println("Error: "+e);
    }
  }
}

Below is the output of our code:

 Read Successfully

 Write Successfully

To check the written image go to the path you in the above program we mention drive E.

Hope this tutorial was helpful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *