Sunday, 30 October 2016

Image Processing (Read and Write) in Java

To perform image read and write, the BufferedImage class can be used as holder class. This class is used to store an image in RAM.

To perform the image read write operation we will import the ImageIO class. This class has static methods to read and write an image.

package amazon.dp;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class ImageReader {

      * readImage - Method to read an IMAGE
      * @param width
      * @param height
     private static BufferedImage readImage(int width, int height) {

           /** Create an object of BufferedImage with parameter width, height and
            * image int type.TYPE_INT_ARGB represents the Alpha, Red, Green and Blue
            * component of the image pixel using 8 bit integer value. */
           //For storing image in RAM
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

           try {

                File input_file = new File("D:\\Python\\InImage.jpg"); //image file path

                // Reading input file
                image =;

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

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

           return image;

      * writeImage - Method to write an IMAGE
      * @param BufferedImage image
     private static void writeImage(BufferedImage image) {
           try {
                // Output file path
                File output_file = new File("D:\\Python\\OuImage.jpg");

                // Writing to file taking type and path as
                ImageIO.write(image, "jpg", output_file);

                System.out.println("Writing complete.");
           } catch(IOException e) {
                System.out.println("Error: "+e);

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

           // READ Image
           BufferedImage image = readImage(width,height);

           // WRITE Image

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...