Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Image Rotation

image rotation

  • Please log in to reply
5 replies to this topic

#1 bigdog

bigdog

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts
  • Learning:C, Java, PHP, JavaScript, PL/SQL

Posted 08 April 2012 - 01:16 PM

Hello everyone, I try to show you how to rotate images in the Java. Please note that this is my first tutorial ever so don’t be angry if I explain something bad .
The main goal of this tutorial is to create three methods for rotating images :)
  • Rotate by 180 degrees
  • Rotate by 90 deggres to the left
  • Rotate by 90 degree to the right
So let’s go to the first method

public BufferedImage rotate180( BufferedImage inputImage ) {
//We use BufferedImage because it’s provide methods for pixel manipulation
	int width = inputImage.getWidth(); //the Width of the original image
	int height = inputImage.getHeight();//the Height of the original image

	BufferedImage returnImage = new BufferedImage( width, height, inputImage.getType()  );
//we created new BufferedImage, which we will return in the end of the program
//it set up it to the same width and height as in original image
// inputImage.getType() return the type of image ( if it is in RBG, ARGB, etc. )

	for( int x = 0; x < width; x++ ) {
		for( int y = 0; y < height; y++ ) {
			returnImage.setRGB( width - x – 1, height – y – 1, inputImage.getRGB( x, y  )  );
		}
	}
//so we used two loops for getting information from the whole inputImage
//then we use method setRGB by whitch we sort the pixel of the return image
//the first two parametres is the X and Y location of the pixel in returnImage and the last one is the //source pixel on the inputImage
//why we put width – x – 1 and height –y – 1 is hard to explain for me, but when you rotate image by //180degree the pixel with location [0, 0] will be in [ width, height ]. The -1 is for not to go out of
//Array size ( remember you always start from 0 so the last index is lower by 1 in the width or height
//I enclose Picture for better imagination  ... hope it help you
	return returnImage;
//and the last return the rotated image

}

Ok let’s finish the others

public BufferedImage rotate90ToLeft( BufferedImage inputImage ){
//The most of code is same as before
	int width = inputImage.getWidth();
	int height = inputImage.getHeight();
	BufferedImage returnImage = new BufferedImage( height, width , inputImage.getType()  );
//We have to change the width and height because when you rotate the image by 90 degree, the
//width is height and height is width <img src='http://forum.codecall.net/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />

	for( int x = 0; x < width; x++ ) {
		for( int y = 0; y < height; y++ ) {
			returnImage.setRGB(y, width – x - 1, inputImage.getRGB( x, y  )  );
//Again check the Picture for better understanding
		}
		}
	return returnImage;

}

public BufferedImage rotate90ToRight( BufferedImage inputImage ){
	int width = inputImage.getWidth();
	int height = inputImage.getHeight();
	BufferedImage returnImage = new BufferedImage( height, width , inputImage.getType()  );

	for( int x = 0; x < width; x++ ) {
		for( int y = 0; y < height; y++ ) {
			returnImage.setRGB( height – y -1, x, inputImage.getRGB( x, y  )  );
//Again check the Picture for better understanding
		}
	}
	return returnImage;
}

Now we can use this method in our program.

OK that’s all. I hope i didn’t mess up anything and help you a bit. Feel free to ask and comment :) . So have a nice day.

Attached Thumbnails

  • rotate180.jpg
  • rotate90ToLeft.jpg
  • rotate90ToRight.jpg

  • 0

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 08 April 2012 - 02:04 PM

Thanks for adding your tutorial to the CodeCall. =)

What are the advantages of using this compared to say an AffineTransform object?
  • 0

#3 bigdog

bigdog

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts
  • Learning:C, Java, PHP, JavaScript, PL/SQL

Posted 08 April 2012 - 11:09 PM

Oh I haven't heard about that object. I was looking for how to rotate images in java, but without result. It's good to know there is such good object. Thanks for tip i'll try it. :)
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 April 2012 - 01:23 AM

Thanks for adding your tutorial to the CodeCall. =)

What are the advantages of using this compared to say an AffineTransform object?

Since AffineTransform can handle any angle of rotation you want, there's likely more overhead over there, and this method may result in faster rotations if you only need 90°,180° or 270° rotations.
Needs testing, but I assume this method is faster.
  • 0

#5 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 13 April 2012 - 10:04 PM

Thanks for adding your tutorial to the CodeCall. =)

What are the advantages of using this compared to say an AffineTransform object?


A transform is easier. :)
  • 0

Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#6 captindance

captindance

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 28 December 2016 - 05:51 PM

Your code was very useful for me. I used it as a template to flip a LIBGDX tiled map. I found lots of examples using other advanced methods, but this did exactly what I needed as a template. Thank you for your help. 


  • 0