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

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.