J2ME Polish
J2ME Polish 2.4 Documentation
Enough Software

Using SnapshotScreens

A SnapshotScreen.
The SnapshotScreen allows you to take camera snapshots on phones that support image capturing with the Multimedia API (polish.api.mmapi).

Design

You can only design the background, title and menubar of a SnapshotScreen.

You can use following general purpose CSS attributes for SnapshotScreens:

Programming

Taking snapshots is a breeze using SnapshotScreen. Note that the SnapshotScreen requires MMAPI support, so you must not even import it when there is no MMAPI present in your target device.

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import de.enough.polish.event.ThreadedCommandListener;
//#if polish.api.mmapi
	import de.enough.polish.ui.SnapshotScreen;
//#endif

public class SnapshotMidlet extends MIDlet CommandListener {
	
	private Command cmdTakeScreenshot = new Command("Take Screenshot", Command.OK, 3 );
	private Command cmdQuit = new Command("Quit", Command.EXIT, 9);
	private Display display;
	
	public SnapshotMidlet(){
		// see startApp	
	}

	protected void startApp() throws MIDletStateChangeException {
		this.display = Display.getDisplay( this );
		//#if polish.api.mmapi
			//#style mainScreen
			SnapshotScreen screen = new SnapshotScreen( "Snapshot" );
			screen.addCommand( this.cmdQuit );
			screen.addCommand( this.cmdTakeScreenshot );
			screen.setCommandListener( new ThreadedCommandListener( this ) );
			this.display.setCurrent( screen );
		//#else
			//#style mainScreen
			Form form = new Form("No MMAPI");
			form.addCommand( this.cmdQuit );
			form.setCommandListener( this );
			this.display.setCurrent( form );			
		//#endif
	}
	
	protected void pauseApp() {
		// ignore
	}

	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		// nothing the clean up
	}

	public void commandAction(Command cmd, Displayable dis) {
		if (cmd == this.cmdQuit ) {
			notifyDestroyed();
		//#if polish.api.mmapi
		} else if (cmd == this.cmdTakeScreenshot) {
			SnapshotScreen screen = (SnapshotScreen) dis;
			try {
				Image image = screen.getSnapshotImage();
				// now use image, e.g. send it to a server using RMI
			} catch (MediaException e) {
				Alert alert = new Alert( "Media-Error", e.toString(), null, AlertType.ERROR );
				this.display.setCurrent( alert );
			}
		//#endif
		}
		
	}

}

You can use following methods of the SnapshotScreen:

  • SnapshotScreen(String title): Creates a new SnapshotScreen, can be used with a style preprocessing directive.
  • static String[] getSnapshotEncodings(): Retrieves all supported snapshot encodings as a String array.
  • Image getSnapshotImage() throws MediaException: Retrieves a snapshot image in the default encoding.
  • Image getSnapshotImage(String encoding) throws MediaException: Retrieves a snapshot image in the specified encoding.
  • byte[] getSnapshot(String encoding) throws MediaException: Retrieves snapshot data in the specified encoding.

Have a look at the "rmi" sample application that uses the SnapshotScreen.

JavaDoc

back to top