Show Thumbnails.java syntax highlighted
/*
* Copyright (c) 2004
* Helsinki Institute of Physics
* see LICENSE file for details
*
* Thumbnails.java
* Created on Nov 11, 2003
*/
package fi.hip.gb.client;
import java.util.Vector;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStoreException;
import fi.hip.gb.midlet.core.LiteResult;
/**
* Shows all stored images as small thumbnail. By selecting
* one image, it is shows in full size.
* <p>
* Stores all added images using <code>RecordStore</code>.
*
* @author Juho Karppinen
*/
class Thumbnails extends Form implements CommandListener {
/** thumbnails */
private ChoiceGroup thumbs;
/** mappes the thumbnails into correct storage */
private Vector storages = new Vector();
/** command for showing the image on screen */
private Command showCommand = new Command("Show", Command.SCREEN, 1);
/** command for deleting the image from memory */
private Command deleteCommand = new Command("Delete", Command.SCREEN, 1);
/** record store name for images and thumbnails */
public final static String IMAGE_STORAGE = "thumbnails";
public Thumbnails() {
super("Thumbnails");
thumbs = new ChoiceGroup("0 images available", Choice.EXCLUSIVE);
append(thumbs);
addCommand(new Command("Capture new", Command.SCREEN, 2));
addCommand(new Command("Send as parameter", Command.SCREEN, 3));
addCommand(new Command("Back", Command.BACK, 1));
setCommandListener(this);
/*
StorageUtils storage = new StorageUtils();
try {
storage.openRecordStore(IMAGE_STORAGE);
RecordEnumeration re = storage.enumerate();
while(re.hasNextElement()) {
int nextID = re.nextRecordId();
System.out.println("adding id " + nextID);
LiteResult imageResult = new LiteResult(nextID);
System.out.println("name was " + imageResult.getName());
if(imageResult.getSize() >= 0) {
Image image =
Image.createImage(imageResult.readBytes(), 0, imageResult.getSize());
Image thumb = createThumbnail(image, -1);
int index = thumbs.append(imageResult.getName(), thumb);
storages.insertElementAt(new Integer(nextID), index);
} else {
System.out.println("Deleting corrupted image index " + nextID);
StorageUtils.deleteRecord(IMAGE_STORAGE, nextID);
}
}
updateCount();
} catch(RecordStoreException rse) {
rse.printStackTrace();
} finally {
try {
storage.closeRecordStore();
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
}*/
}
public void commandAction(Command c, Displayable disp) {
if ((c.getCommandType() == Command.BACK)) {
MIDui.back(true);
} else if(c.getLabel().equals("Capture new")) {
MIDui.showCamera();
} else {
int currentIndex = thumbs.getSelectedIndex();
int recordID =
((Integer) storages.elementAt(currentIndex)).intValue();
LiteResult image = new LiteResult(recordID);
if(c.getLabel().equals("Show")) {
MIDui.showPicture(image);
} else if(c.getLabel().equals("Send as paramete")) {
MIDui.showDispatch(null, image);
} else if(c.getLabel().equals("Delete")) {
MIDui.deletePicture(image, false);
}
}
}
/*
public static Image scale(Image src, int width, int height) {
long start = System.currentTimeMillis();
int scanline = src.getWidth();
int srcw = src.getWidth();
int srch = src.getHeight();
int buf[] = new int[srcw * srch];
src.getRGB(buf, 0, scanline, 0, 0, srcw, srch);
int buf2[] = new int[width * height];
for (int y = 0; y < height; y++) {
int c1 = y * width;
int c2 = (y * srch / height) * scanline;
for (int x = 0; x < width; x++) {
buf2[c1 + x] = buf[c2 + x * srcw / width];
}
}
Image img = Image.createRGBImage(buf2, width, height, true);
long end = System.currentTimeMillis();
System.out.println(
"Scaled "
+ src.getWidth()
+ "x"
+ src.getHeight()
+ " in "
+ ((end - start) / 1000)
+ " seconds");
return img;
}
*/
/**
* Adds image into thumbnails and store raw image into record store
* @param imageContainer image data inside conteiner
* @return final image
* @throws Exception if image cannot be saved
*/
public Image addImage(LiteResult imageContainer) throws Exception {
Image image =
Image.createImage(imageContainer.readBytes(), 0, imageContainer.getSize());
Image thumb = createThumbnail(image, -1);
int index = thumbs.append(imageContainer.getName(), thumb);
int recordID = imageContainer.store();
storages.insertElementAt(new Integer(recordID), index);
updateCount();
return image;
}
/**
* Deletes the image from record store
* @param image image to be deleted
* @throws RecordStoreException if image cannot be deleted
*/
public void deleteImage(LiteResult image) throws RecordStoreException {
int currentIndex = thumbs.getSelectedIndex();
thumbs.delete(currentIndex);
storages.removeElementAt(currentIndex);
//StorageUtils.deleteRecord(IMAGE_STORAGE, image.getRecordID());
updateCount();
}
/**
* Updates the UI after image count has been changed
*/
private void updateCount() {
if(thumbs.size() > 0) {
addCommand(showCommand);
addCommand(deleteCommand);
} else {
removeCommand(showCommand);
removeCommand(deleteCommand);
}
thumbs.setLabel(thumbs.size() + " images available");
}
/**
* Creates small thumbnail from the image
* @param image original image
* @param thumbWidth width of the target thumbnail
* @return resized thumbnail image
*/
public static Image createThumbnail(Image image, int thumbWidth) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if(thumbWidth <= 0)
thumbWidth = sourceWidth;
int 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;
}
}
See more files for this project here