You Are Here:

Community: Wiki

This page was last modified on 15 September 2009, at 02:34.

Rotate an image in Java ME

From Forum Nokia Wiki

Atention: This example works only in MIDP 2.x devices.

Here's a useful method to rotate images in J2ME. It currently supports rotations of 90°, 180° and 270° (and, of course, 0° rotations :)).

Image: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);

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHttpE3aE2fE2f217E2e218E2e225E2e2E3a2082E2findeE78E2ehtmlE253FX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ