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()

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

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. 😀 😀 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. :D
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com

18 Comments

  1. Reply
    garmin nuvi 255w gps July 28, 2011

    Thanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..

  2. Reply
    libertyreserve July 29, 2011

    Thanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..

  3. Reply
    liberty reserve August 1, 2011

    Thanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..

  4. Reply
    android apps August 3, 2011

    I agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., wonderful post.

  5. Reply

    I like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was looking for! .

  6. Reply
    hedge fund August 4, 2011

    Your 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.

  7. Reply
    captain america August 4, 2011

    I like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .

  8. Reply
    invest liberty reserve August 5, 2011

    I like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .

  9. Reply
    mutual funds August 5, 2011

    Your 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.

  10. Reply
    cover letter August 6, 2011

    Your 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.

  11. Reply
    zombieville usa August 7, 2011

    I agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., good post.

  12. Reply
    invest liberty reserve August 7, 2011

    I agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., wonderful post.

  13. Reply
    invest liberty reserve August 7, 2011

    I agree with your Rotate an image with java Buffered image class.. Gihan's Blog.., superb post.

  14. Reply
    field runners hd August 8, 2011

    I like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was searching for! .

  15. Reply
    invest liberty reserve August 9, 2011

    Thanks i love your article about Rotate an image with java Buffered image class.. Gihan's Blog..

  16. Reply
    robo defense August 10, 2011

    I like Your Article about Rotate an image with java Buffered image class.. Gihan's Blog.. Perfect just what I was looking for! .

  17. Reply
    Stephen Nutbrown December 14, 2011

    Hey, great post!
    Thanks 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,

  18. Reply
    Dean January 15, 2013

    Good code examples cheers!

Leave a Reply