Namespaces
Variants
Actions

Rotate an image in Java ME

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.

http://www.developer.nokia.com/Community/Wiki/CS001263_-_Rotating_images_in_Java_ME

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, Series 60, Symbian

Article
Keywords: Rotate, Image, createRGBImage
Created: jappit (19 May 2008)
Reviewed: tiviinik (09 Jan 2012)
Last edited: hamishwillee (11 Jan 2012)

Comments

Reviewer-approved.png
29 Sep
2009
Article Review by kalki (20090929)

It sometimes useful to do animation like rotating text or images in our application. This article discusses about rotating an image in j2me. We can rotate images at different angles like 90°, 180° and 270°.

Complete source code is given demonstrating Rotating an image. The given code is ready without any errors.This article is can be become valuable alternative to scalable vector graphics which requires more memory and processing power than doing animation using simple custom method rotateImage() specified in code.

This page was last modified on 11 January 2012, at 00:06.
196 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2012 All rights reserved