Rotate an image with java Buffered image class..
Hi, today I'm going to show you another image enhancing technique with Java's Buffered image class. That is rotating an image. To rotate an image with Java we use classes called AffineTransform and AffineTransformOp. And let's see how to rotate an image. It is very easy as previous things.

In this post I'm not going to describe basics like creating an buffered image, display a buffered image on a JLabel.. etc .. but to describe methods I wrote in my program.
1). public void rescale() {}
2). private void rorateImg() {}
Let's consider one by one..
public void rescale() {
tx = new AffineTransform();
tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)
}//end rescale()
[caption id="attachment_179" align="aligncenter" width="300" caption="Before rotating the image..."]
[/caption]
In rescale() method I've created an AffineTransform object then called the rotate() method.
tx = new AffineTransform();
double theta, double anchorx, double anchory parameters should be passed into the rotate() method. This method rotates an image around an anchor point which coordinates equals to anchorx,anchory . And the theta should be give in radians. And this procedure equal to these three operations.
translate(anchorx, anchory); // final translation
rotate(theta); // rotate around anchor
translate(-anchorx, -anchory); // translate anchor to origin
double angel=Math.PI/2;
This Math.PI/2 equals to 90 degrees.
int w;// width of buffered image
int h;//height of buffered image
then Buffered Image's getWidth() and getHeight() methods assigns image's width and height into w and h.
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
tx.rotate(angel,w / 2, h / 2);
By assigning w/2 and h/2 into anchorx and anchory I made the image to rotate around it's center point. Then AffineTransformOp object is created. When creating the object an AffineTransform object and Bilinear interpolation type are given as parameters.
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
Then the filter() method is called with AffineTransformOp object. This public final BufferedImage filter(BufferedImage src, BufferedImage dst) method is more likely the filter() in RescaleOp which we discussed. And this method compares source and destination images then returns the filtered buffered image.
bufferedImage=op.filter(bufferedImage,null);
[caption id="attachment_180" align="aligncenter" width="231" caption="After rotating the image..."]
[/caption]
private void rorateImg(){
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
rescale();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}//end rotateImg()
In here I earlier discussed what happens with w= bufferedImage.getWidth(); and h=bufferedImage.getHeight(); . Then rescale() method is called and the other codes describes the displaying the buffered image on a JLabel. :D :D ok that all.
And here is related Java docs.
1) AffineTransform
2) AffineTransformOp
Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Rotate.java and Main.java which is included the main method.
Main.java
/*
* Main.java
*/
package rotate;
/**
*
* @author gihan
*/
public class Main {
public static void main(String[] args) {
Rotate obj=new Rotate();
}
}
Rotate.java
/*
* Rotate.java
*/
package rotate;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
/**
*
* @author gihan
*/
public class Rotate extends JFrame{
// globel variables
BufferedImage bufferedImage;
RescaleOp rescale;
ImageIcon icon;
AffineTransformOp op;
AffineTransform tx;
int w;// width of buffered image
int h;//height of buffered image
double angel=Math.PI/2; /** angle should be given in radian and
* this program rotates the image in 90 degrees */
String path="/home/gihan/Pictures/nfs_bmw.jpg";// image file path
JLabel picLabel=new JLabel();
public Rotate(){
JFrame jf=new JFrame();
JPanel jp=new JPanel();
jf.add(jp);
jp.add(picLabel);
jf.setVisible(true);
jf.setSize(487, 640);
jf.setLocation(200,200);
jf.setTitle("Gihan's Image Processing Test Area.. ");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File file = new File(path);
try {
bufferedImage = ImageIO.read(file); // create a buffered image
} catch (IOException ex) {
Logger.getLogger(Rotate.class.getName()).log(Level.SEVERE, null, ex);
}
rorateImg();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}
private void rorateImg(){//rotates 90 degrees
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
rescale();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}
public void rescale() {
tx = new AffineTransform();
tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)
}
}//end class Rotate
If you have any doubt, feel free to ask.
Thank you
Gihan Malan De Silva @ gihansblog.wordpress.com

In this post I'm not going to describe basics like creating an buffered image, display a buffered image on a JLabel.. etc .. but to describe methods I wrote in my program.
1). public void rescale() {}
2). private void rorateImg() {}
Let's consider one by one..
public void rescale() {
tx = new AffineTransform();
tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)
}//end rescale()
[caption id="attachment_179" align="aligncenter" width="300" caption="Before rotating the image..."]

In rescale() method I've created an AffineTransform object then called the rotate() method.
tx = new AffineTransform();
double theta, double anchorx, double anchory parameters should be passed into the rotate() method. This method rotates an image around an anchor point which coordinates equals to anchorx,anchory . And the theta should be give in radians. And this procedure equal to these three operations.
translate(anchorx, anchory); // final translation
rotate(theta); // rotate around anchor
translate(-anchorx, -anchory); // translate anchor to origin
double angel=Math.PI/2;
This Math.PI/2 equals to 90 degrees.
int w;// width of buffered image
int h;//height of buffered image
then Buffered Image's getWidth() and getHeight() methods assigns image's width and height into w and h.
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
tx.rotate(angel,w / 2, h / 2);
By assigning w/2 and h/2 into anchorx and anchory I made the image to rotate around it's center point. Then AffineTransformOp object is created. When creating the object an AffineTransform object and Bilinear interpolation type are given as parameters.
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
Then the filter() method is called with AffineTransformOp object. This public final BufferedImage filter(BufferedImage src, BufferedImage dst) method is more likely the filter() in RescaleOp which we discussed. And this method compares source and destination images then returns the filtered buffered image.
bufferedImage=op.filter(bufferedImage,null);
[caption id="attachment_180" align="aligncenter" width="231" caption="After rotating the image..."]

private void rorateImg(){
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
rescale();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}//end rotateImg()
In here I earlier discussed what happens with w= bufferedImage.getWidth(); and h=bufferedImage.getHeight(); . Then rescale() method is called and the other codes describes the displaying the buffered image on a JLabel. :D :D ok that all.
And here is related Java docs.
1) AffineTransform
2) AffineTransformOp
Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Rotate.java and Main.java which is included the main method.
Main.java
/*
* Main.java
*/
package rotate;
/**
*
* @author gihan
*/
public class Main {
public static void main(String[] args) {
Rotate obj=new Rotate();
}
}
Rotate.java
/*
* Rotate.java
*/
package rotate;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
/**
*
* @author gihan
*/
public class Rotate extends JFrame{
// globel variables
BufferedImage bufferedImage;
RescaleOp rescale;
ImageIcon icon;
AffineTransformOp op;
AffineTransform tx;
int w;// width of buffered image
int h;//height of buffered image
double angel=Math.PI/2; /** angle should be given in radian and
* this program rotates the image in 90 degrees */
String path="/home/gihan/Pictures/nfs_bmw.jpg";// image file path
JLabel picLabel=new JLabel();
public Rotate(){
JFrame jf=new JFrame();
JPanel jp=new JPanel();
jf.add(jp);
jp.add(picLabel);
jf.setVisible(true);
jf.setSize(487, 640);
jf.setLocation(200,200);
jf.setTitle("Gihan's Image Processing Test Area.. ");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File file = new File(path);
try {
bufferedImage = ImageIO.read(file); // create a buffered image
} catch (IOException ex) {
Logger.getLogger(Rotate.class.getName()).log(Level.SEVERE, null, ex);
}
rorateImg();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}
private void rorateImg(){//rotates 90 degrees
w= bufferedImage.getWidth();
h=bufferedImage.getHeight();
rescale();
icon = new ImageIcon(bufferedImage);
picLabel.setIcon(icon);
}
public void rescale() {
tx = new AffineTransform();
tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)
op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)
}
}//end class Rotate
If you have any doubt, feel free to ask.
Thank you
Gihan Malan De Silva @ gihansblog.wordpress.com
Thanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..
ReplyDeleteThanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..
ReplyDeleteThanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..
ReplyDeleteI agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., wonderful post.
ReplyDeleteI like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was looking for! .
ReplyDeleteYour Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Very good visual appeal on this website , I'd value it 10 10.
ReplyDeleteI like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .
ReplyDeleteI like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .
ReplyDeleteYour Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Really excellent visual appeal on this site, I'd value it 10 10.
ReplyDeleteYour Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Really superb visual appeal on this web site , I'd rate it 10 10.
ReplyDeleteI agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., good post.
ReplyDeleteI agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., wonderful post.
ReplyDeleteI agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., superb post.
ReplyDeleteI like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .
ReplyDeleteThanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..
ReplyDeleteI like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was looking for! .
ReplyDeleteHey, great post!
ReplyDeleteThanks for the help.
I notice however that sometimes if I use this that the edges of the image can get cut off and it's not quite central. Do you know how to fix this?
Thanks,
Good code examples cheers!
ReplyDeletehow to create image processing project in java netbeans
ReplyDeleteThank, your example is great. Thanks again.
ReplyDelete