How to create thumbnail in Java ME
Article Metadata
Create a thumbnail image from a large image or scale an image in Java ME
private Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = 64;
int thumbHeight = -1;
if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy,
Graphics.LEFT | Graphics.TOP);
}
}
Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}
You may also use http://www.java-tips.org/java-me-tips/midp/how-to-implement-oom-in-and-oom-out.html
If you are using S60 devices, you can load the thumbnails generated automatically by the Gallery application. They are located in the "_PAlbTN" sub-folders of Image.
For more details on this, check Thumbnail_path_for_3rd_edition_devices.


15 Sep
2009
A common feature required by applications which allow users to browse photos on their phone is to provide some sort of preview or thumbnail view of the photo in question. This allows multiple images to be displayed on the screen simultaneously and is usually used to enable users to select a photo to view in full size. However, the drawImage method in the Graphics class does not allow programmers to specify a width and height and so there is no easy way of creating thumbnail images.
This article demonstrates a work-around that allows the creation of thumbnail images by sampling the pixels of the original image 1 pixel at a time through the use of the setClip() method to create a smaller version of the original image. The code provided does not allow the width and height of the thumbnail to be customised, but fixes the width at 64px and ensures that the height is set so that the original aspect ratio is maintained (avoiding distortion). The method shown is effective for creating thumbnail views, but can be slow, particularly on devices with high resolution cameras, where a large image must be scaled down dramatically.
An alternative method is also suggested, which involves using the thumbnails which are generated automatically by the gallery application. This method allows thumbnails to be rendered much more quickly as no extra work is required to first create the thumbnail.