Tuesday, June 11, 2013
Today I will show you how you can flip an image vertically and horizontally. Flipping an image means reflecting an image along a miiror axis. Mathematically when we flip along horizontal axis x'=x and y'=-y whereas when flipped along vertical axis x'=-x and y'=y, where (x',y') is the transformed point and (x,y) is the original point. Here in our sample code we will first read the image from file as BufferedImage. Since there is no flip function available in Java so we will first perform scaling and then translation. We will use AffineTransformOp to transform the image and call filter() method that returns the transformed image.The transformation will be carried out in following way --->
Horizontal Flip : scale(1.0,-1.0) and translate(0,-image.getHeight())
Vertical Flip : scale(-1.0,1.0) and translate(-image.getWidth(),0)
Flip both ways : scale(-1.0,-1.0) and translate(-image.getWidth(),-image.getHeight())
AffineTransform class : It represents a 2D affine transform that performs a linear mapping from 2D coordinates to other 2D coordinates that preserves the "straightness" and "parallelness" of lines. Affine transformations can be constructed using sequences of translations, scales, flips, rotations, and shears.
AffineTransformOp class : This class uses an affine transform to perform a linear mapping from 2D coordinates in the source image or Raster to 2D coordinates in the destination image or Raster. The type of interpolation that is used is specified through a constructor, either by a RenderingHints object or by one of the integer interpolation types defined in this class.
Screenshots of the transformation
Original image
Image after Vertical flip

Image after Horizontal flip
Image after flipping both ways

--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;


public class FlipImage {
    public BufferedImage flipVertical(BufferedImage src){
    AffineTransform tx=AffineTransform.getScaleInstance(-1.0,1.0);  //scaling
    tx.translate(-src.getWidth(),0);  //translating
    AffineTransformOp tr=new AffineTransformOp(tx,null);  //transforming
   
    return tr.filter(src, null);  //filtering
    }
    
    public BufferedImage flipHorizontal(BufferedImage src){
    AffineTransform tx=AffineTransform.getScaleInstance(1.0,-1.0);  //scaling
    tx.translate(0,-src.getHeight());  //translating
    AffineTransformOp tr=new AffineTransformOp(tx,null);  //transforming
   
    return tr.filter(src, null);  //filtering
    }
    
    public static void main(String[] args)throws Exception {
    FlipImage obj=new FlipImage();
    BufferedImage src=ImageIO.read(new File("src.jpeg"));  //reading image
    BufferedImage dest=obj.flipVertical(src);  //flipping vertically
    ImageIO.write(dest,"jpeg",new File("dest_flipVer.jpg"));
    dest=obj.flipHorizontal(src);  //flipping horizontally
    ImageIO.write(dest,"jpeg",new File("dest_flipHor.jpg"));
    dest=obj.flipVertical(dest); //flupping both ways
    ImageIO.write(dest,"jpeg",new File("dest_flipBoth.jpg"));
    }
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters