views:

5070

answers:

9

I'm looking for an algorithm that rotates an image by some degrees (input).

public Image rotateImage(Image image, int degrees)

(Image instances could be replaced with int[] containing each pixel RGB values, My problem is that i need to implement it for a JavaME MIDP 2.0 project so i must use code runnable on JVM prior to version 1.5 Can anyone help me out with this ?

EDIT: I forgot to mention that i don't have SVG APIs available and that i need a method to rotate by arbitrary degree other than 90 - 180- 270

Also, no java.awt.* packages are available on MIDP 2.0

A: 

Have you tried Processing?

It's very good to handle images, you can find it here --> http://processing.org

fmsf
A: 

Graphics2D and AffineTransform will help you do exactly what you want. Specifically, Graphics2D.drawImage(Image, AffineTransform) and AffineTransform.getRotateInstance. You can also do scaling, translation, and shearing with this. Both classes have been in the runtime since at least 1.4 probably earlier.

basszero
Will this work for his version of JavaME?
Outlaw Programmer
:( unfortunatly Graphics2D is not part of MIDP 2.0 specifications. thanks the same
Stefano Driussi
sorry mate, I learned something to today ;)
basszero
+9  A: 

One of the best pages describing image rotation algorithms I've found on the internet is tied to Dan Bloomberg's excellent leptonica library. While the leptonica library itself is written in C and won't help you, his page on image rotation algorithms:

http://www.leptonica.com/rotation.html

is definitely worth a read. You will most likely want to implement something like the Rotation by Area Mapping algorithm he describes in the second portion of the page.

earino
Thanks, i'm reading it now :)
Stefano Driussi
A: 

Getting Started with Mobile 2D Graphics for J2ME: http://developers.sun.com/mobility/midp/articles/s2dvg/index.html

http://j2mepolish.org/javadoc/j2me/de/enough/polish/util/ImageUtil.html

Ray Tayek
Sorry my mistake. Needed to include also "No SVG available"
Stefano Driussi
+4  A: 

Nokia forums have an article and code on Rotating Images in Java ME

Stephen Denne
+1  A: 

LWUIT can do that and it is opensource. I suggest you find the code there.

Honza
+1  A: 

General solution: For each pixel in the destination image, take the pixel in the source image with coordinates of the destination pixel, rotated in the opposite direction.

Enhancement to solution: The rotation usually won't give exact pixel coordinates. Do a weighted average of the source pixel with its neighbors, according to the percentage it overlaps them.

Faster solution for binary images: Convert the image into "runs" of consecutive foreground pixels. Then rotate the endpoints of these lines and draw them into the destination.

Normally this will produce slight gaps due to integer roundoff, so when one or both endpoints are more than 10% away from an integer, patch by drawing TWO lines for the single source line, using the integer coordinates rounded up and down.

If one endpoint is within 10% and the other isn't, the two lines will form a 'V' shape. If both are off by more than 10%, the two lines will form an 'X' shape.

This can be done with respect to the X axis or the Y axis . Use the one with the smallest angle between the axis and the rotation angle. (I.e. if the rotation angle is between 45 and -45, use the X axis.)

Still faster solution for binary images: If there are fewer background pixels than foreground pixels, fill the destination with foreground, and follow the above algorithm with background pixels.

A: 

You can try http://www.j2mearmyknife.com/ . It features a lot of cool visual effects, including image rotation.

A: 

public Image rotateImage(Image img, float degrees){ BufferedImage sourceBI = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB); sourceBI.getGraphics().drawImage(img,0,0,null); AffineTransform at = new AffineTransform(); at.rotate(degrees*Math.PI/180, sourceBI.getWidth()/2, sourceBI.getHeight()/2); BufferedImageOp bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); return bio.filter(sourceBI, null); }

dave