Image Processing in Java | Set 8 (Creating mirror image)

We strongly recommend to refer below post as a prerequisite of this.

In this set we will be creating mirror image.
The main trick is to just get the source pixel values from left to right and set the same in the result image from right to left.

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 same dimension to hold the mirror image.
  4. Get ARGB (Alpha, Red, Green and Blue) values from source image [in left to right fashion].
  5. Set ARGB (Alpha, Red, Green and Blue) to newly created image [in right to left fashion].
  6. Repeat the steps 4 and 5 for each pixels of the image.

Below is Java implementation of above steps.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate creation of mirror image
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
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("G:\\Inp.jpg");
            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("G:\\Out.jpg");
            ImageIO.write(mimg, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);
        }
    }
}

chevron_right


NOTE:Code will not run on online ide since it requires image in drive.

Output:


Inp.jpg
Inp

Out.jpg
Out

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.