rotate an image

I need to rotate an image. I dont mean rotate a Graphics or Graphics2D object, as Ive been able to find plenty of guides for that, but actually have the Image rotated. Thanks for any advice.
[192 byte] By [Dash_Riprock] at [2007-11-20 9:02:20]
# 1 Re: rotate an image
Not quite sure what you mean - Graphics2D is how you rotate an image with the SDK library... If you just want to have a rotated copy of the image instead of a rotated rendering on a Component, make the target of the Graphics2D rendering an Image not a Component...

It's not something I remember doing myself, but you can get a Graphics object for drawing to an off-screen image by calling getGraphics() on the Image object.

If that's not what you mean, please explain.

The important thing in science is not so much to obtain new facts as to discover new ways of think about them...
W. Bragg
dlorde at 2007-11-10 2:15:09 >
# 2 Re: rotate an image
I also dont get exactly what you mean. Maybe you want to rotate the image by processing the image matrix itself? For example if you need 90 degress rotation you can do this by changing the row->collumt the matrix corresponding to the image:
(W, H: old image size)
(array: image matrix)
(array2: new rotated image)

array2 = new int[W*H]; // same total size
for (int i=0; i< H; i++)
for (int j=0; j< W; j++)
array2[j*H + i] = array[i*W + j];

Image img2 = createImage(new MemoryImageSource(H, W, array2, 0, H));
....
yiannakop at 2007-11-10 2:16:14 >
# 3 Re: rotate an image
Not quite sure what you mean - Graphics2D is how you rotate an image with the SDK library... If you just want to have a rotated copy of the image instead of a rotated rendering on a Component, make the target of the Graphics2D rendering an Image not a Component...

It's not something I remember doing myself, but you can get a Graphics object for drawing to an off-screen image by calling getGraphics() on the Image object.

Yes, thats what I mean. When I try using getGraphics() on the Image, it gives me an exception that says something along the lines of "cannot use getGraphics() on an Image created with Toolkit.createImage()", so I figured I was going at it completely wrong.
.
Dash_Riprock at 2007-11-10 2:17:10 >
# 4 Re: rotate an image
You were going at it wrong - Toolkit is not what you want. The Java Tutorial explains just how to do it: Creating and Drawing to an Image (http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html).

Here's a piece of code I just hacked up that rotates an image 90 degrees clockwise - I'm not sure it's exactly right, but it seems to work:// load image from file
BufferedImage original = ImageIO.read(new File("I:\\Photos\\Air Plants\\IMGP0140.JPG"));

// create another image to draw it on
BufferedImage rotated = new BufferedImage(original.getWidth(), original.getHeight(), Image.SCALE_SMOOTH);

// create graphics to transform
Graphics2D g2d = rotated.createGraphics();

// set a rotate transform of 90 degrees about middle
g2d.rotate(1.57, original.getWidth()/2D, original.getHeight()/2D);

// draw original image rotated into new image
g2d.drawImage(original, 0,0, null);

... // do something with new image 'rotated'.NOTE: exception handling omitted for clarity.

Example isn't another way to teach. It is the only way to teach...
A. Einstein
dlorde at 2007-11-10 2:18:07 >
# 5 Re: rotate an image
Alright, that worked. It still has some problems for what I wanted it for, though.
Dash_Riprock at 2007-11-10 2:19:18 >