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 :)).
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);
}
}
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);
No related wiki articles found