Rotate an image with java Buffered image class..

14 07 2011

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


Actions

Information

19 responses

28 07 2011
garmin nuvi 255w gps

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

29 07 2011
libertyreserve

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

1 08 2011
liberty reserve

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

3 08 2011
android apps

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

3 08 2011
internship cover letter sample

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

4 08 2011
hedge fund

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.

4 08 2011
captain america

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

5 08 2011
invest liberty reserve

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

5 08 2011
mutual funds

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.

6 08 2011
cover letter

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.

7 08 2011
zombieville usa

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

7 08 2011
invest liberty reserve

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

7 08 2011
invest liberty reserve

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

8 08 2011
field runners hd

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

9 08 2011
invest liberty reserve

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

10 08 2011
robo defense

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

14 12 2011
Stephen Nutbrown

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,

15 01 2013
Dean

Good code examples cheers!

27 02 2013
johnson ezek.j

how to create image processing project in java netbeans

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: