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
--------------------------------------------------------------------------------------------------------------------------
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"));
}
}
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
--------------------------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------------------
Labels:Graphics2D,Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment