Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Rotate an image in Java ME

From Wiki
Jump to: navigation, search

This article shows how to rotate images in 90° angles using the method createRGBImage(). There is also a predefined method createImage(Image image,int x, int y, int width, int height, int transform) in class javax.microedition.lcdui.Image that can be used to rotate images. See following article for more details.

J2me rotate image.png

Source code: rotateImage method

public static Image rotateImage(Image image, int angle) throws Exception
{
if(angle == 0)
{
return image;
}
else if(angle != 180 && angle != 90 && angle != 270)
{
throw new Exception("Invalid angle");
}
 
int width = image.getWidth();
int height = image.getHeight();
 
int[] rowData = new int[width];
int[] rotatedData = new int[width * height];
 
int rotatedIndex = 0;
 
for(int i = 0; i < height; i++)
{
image.getRGB(rowData, 0, width, 0, i, width, 1);
 
for(int j = 0; j < width; j++)
{
rotatedIndex =
angle == 90 ? (height - i - 1) + j * height :
(angle == 270 ? i + height * (width - j - 1) :
width * height - (i * width + j) - 1
);
 
rotatedData[rotatedIndex] = rowData[j];
}
}
 
if(angle == 90 || angle == 270)
{
return Image.createRGBImage(rotatedData, height, width, true);
}
else
{
return Image.createRGBImage(rotatedData, width, height, true);
}
}

Source code: sample usage

Here's a sample usage on how to use the rotateImage() method:

Image original = Image.createImage("/original_image.png");
 
Image rotated_image = rotateImage(original, 90);
Article Metadata
Tested with
Devices(s): Nokia 7373, Nokia N82
Compatibility
Platform(s):
Series 40
Symbian
Article
Created: jappit (19 May 2008)
Reviewed: tiviinik (09 Jan 2012)
Last edited: hamishwillee (08 Aug 2013)
This page was last modified on 8 August 2013, at 03:56.
88 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×