Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Image Processing in Java – Creating a Mirror Image

  • Difficulty Level : Hard
  • Last Updated : 14 Nov, 2021

Prerequisite: 

In this set, we will be creating a mirror image. The image of an object as seen in a mirror is its mirror reflection or mirror image. In such an image, the object’s right side appears on the left side and vice versa. A mirror image is therefore said to be laterally inverted, and the phenomenon is called lateral inversion. The main trick is to get the source pixel values from left to right and set the same in the resulting image from right to left.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Algorithm: 

  1. Read the source image in a BufferedImage to read the given image.
  2. Get the dimensions of the given image.
  3. Create another BufferedImage object of the same dimension to hold the mirror image.
  4. Get ARGB (Alpha, Red, Green, and Blue) values from the source image [in left to right fashion].
  5. Set ARGB (Alpha, Red, Green, and Blue) to newly created image [in the right to left fashion].
  6. Repeat steps 4 and 5 for each pixel of the image.

Implementation: 

Java




// Java program to demonstrate
// creation of a mirror image
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class MirrorImage {
    public static void main(String args[])
        throws IOException
    {
        // BufferedImage for source image
        BufferedImage simg = null;
  
        // File object
        File f = null;
  
        // Read source image file
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            simg = ImageIO.read(f);
        }
  
        catch (IOException e) {
            System.out.println("Error: " + e);
        }
  
        // Get source image dimension
        int width = simg.getWidth();
        int height = simg.getHeight();
  
        // BufferedImage for mirror image
        BufferedImage mimg = new BufferedImage(
            width, height, BufferedImage.TYPE_INT_ARGB);
  
        // Create mirror image pixel by pixel
        for (int y = 0; y < height; y++) {
            for (int lx = 0, rx = width - 1; lx < width; lx++, rx--) {
                  
                  // lx starts from the left side of the image
                // rx starts from the right side of the
                // image lx is used since we are getting
                // pixel from left side rx is used to set
                // from right side get source pixel value
                int p = simg.getRGB(lx, y);
  
                // set mirror image pixel value
                mimg.setRGB(rx, y, p);
            }
        }
  
        // save mirror image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(mimg, "png", f);
        }
        catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Output:

Note: This code will not run on online ide since it requires an image in the drive.

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!