Tutorialspoint.dev

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.

// 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);
        }
    }
}

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

Output:

Inp.jpg
Inp

Out.jpg
Out

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter