Categories: Java | Java ME | Code Examples | UI
Since MIDP 1.0 does not natively support image rotating, 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);
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Hang while processing 640x480 image? | jonawebb | Mobile Java Media (Graphics & Sounds) | 1 | 2006-10-17 03:33 |
| Thumbnail of JPEG image for J2me application. | Namdeo | Mobile Java Media (Graphics & Sounds) | 3 | 2008-05-05 10:38 |
| N90 Camera Problem!... | toybox89 | Mobile Java General | 11 | 2006-06-01 16:30 |
| N90 Camera Problem... | toybox89 | Mobile Java Media (Graphics & Sounds) | 0 | 2006-02-26 21:27 |
| Opening a JPEG Image | ummarbhutta | Mobile Java Media (Graphics & Sounds) | 8 | 2007-02-15 07:34 |