|
package com.robohobby.me;
import java.io.IOException;
import java.util.Calendar;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
@Ads
@author
public class RobotMIDlet extends MIDlet implements CommandListener {
public boolean midletPaused = false;
private Player player = null;
private VideoControl videoControl = null;
private boolean active = false;
byte[] imageByte;
javax.microedition.lcdui.Image image;
int w = 256;
int h = 192;
int[] rgbData = new int[w * h];
CallBacks callBacks = CallBacks.getInstance();
private Command exitCommand;
private Command doSnapShotCommand;
private Command infoItemCommand;
private Command itemCommand;
private Command screenCommand;
private Command getRootsItemCommand;
private Form form;
private StringItem infoStringItem;
public RobotMIDlet() {
callBacks.robotMIDlet = this;
callBacks.discUtils = new DiscUtils();
}
<code></code>
private void initialize() {
}
public void startMIDlet() {
switchDisplayable(null, getForm());
}
public void resumeMIDlet() {
}
<code></code> <code></code>
@param
<code></code> <code></code>
@param
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
@param
@param
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == doSnapShotCommand) {
doSnapShot();
} else if (command == exitCommand) {
exitMIDlet();
} else if (command == getRootsItemCommand) {
getRoots();
}
}
}
@return
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
}
return exitCommand;
}
@return
public Form getForm() {
if (form == null) {
form = new Form("www.RoboHobby.com", new Item[] { getInfoStringItem() });
form.addCommand(getExitCommand());
form.addCommand(getDoSnapShotCommand());
form.addCommand(getGetRootsItemCommand());
form.setCommandListener(this);
initCamera();
}
return form;
}
@return
public Command getDoSnapShotCommand() {
if (doSnapShotCommand == null) {
doSnapShotCommand = new Command("SnapAndSave", Command.ITEM, 0);
}
return doSnapShotCommand;
}
@return
public Command getItemCommand() {
if (itemCommand == null) {
itemCommand = new Command("Item", Command.ITEM, 0);
}
return itemCommand;
}
@return
public Command getInfoItemCommand() {
if (infoItemCommand == null) {
infoItemCommand = new Command("State: ", Command.ITEM, 0);
}
return infoItemCommand;
}
@return
public StringItem getInfoStringItem() {
if (infoStringItem == null) {
infoStringItem = new StringItem("Info: ", "");
}
return infoStringItem;
}
@return
public Command getScreenCommand() {
if (screenCommand == null) {
screenCommand = new Command("Screen", Command.SCREEN, 0);
}
return screenCommand;
}
@return
public Command getGetRootsItemCommand() {
if (getRootsItemCommand == null) {
getRootsItemCommand = new Command("Roots", Command.ITEM, 0);
}
return getRootsItemCommand;
}
@return
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
private void getRoots()
{
this.callBacks.discUtils.getRoots();
}
public void doSnapShot()
{
System.out.println( "doSnapShot()" );
captureImage();
Calendar c = Calendar.getInstance();
String fileName = StrCalendar.calendarToStrFs(c);
callBacks.discUtils.writeToDisk( fileName+".jpg", imageByte);
}
public void startApp() {
System.gc();
Displayable current = Display.getDisplay(this).getCurrent();
if(current ==null)
{
startMIDlet();
start();
}
else
{
if(current == form)
{
start();
}
Display.getDisplay(this).setCurrent(current);
}
}
public void pauseApp() {
midletPaused = true;
}
@param
public void destroyApp(boolean unconditional) {
}
public void initCamera()
{
try
{
player = Manager.createPlayer("capture://video");
player.realize();
videoControl = (VideoControl) (player.getControl("VideoControl"));
if (videoControl != null) {
form.append((Item) (videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null)));
} else {
infoStringItem.setText( "No video control" );
}
} catch (IOException ioe) {
infoStringItem.setText( "IOException: " + ioe.getMessage() );
} catch (MediaException me) {
infoStringItem.setText( "Camera() Media Exception: " + me.getMessage() );
} catch (SecurityException se) {
infoStringItem.setText( "Security Exception: " + se.getMessage() );
} catch (Exception e) {
infoStringItem.setText( "Exception: " + e );
}
}
synchronized void start()
{
if (!active) {
try {
if (player != null) {
player.start();
}
if (videoControl != null) {
videoControl.setVisible(true);
}
} catch (MediaException me) {
infoStringItem.setText(
"start() Media Exception: " + me.getMessage());
} catch (SecurityException se) {
infoStringItem.setText(
"start() Security Exception: " + se.getMessage());
} catch (Exception e) {
infoStringItem.setText(
"start() Exception: " + e.getMessage());
}
active = true;
}
}
synchronized void stop()
{
if (active) {
try {
if (videoControl != null) {
videoControl.setVisible(false);
}
if (player != null) {
player.stop();
}
} catch (MediaException me) {
infoStringItem.setText(
"stop() Media Exception: " + me.getMessage());
}
active = false;
}
}
public void captureImage()
{
System.gc();
try {
imageByte = videoControl.getSnapshot("encoding=jpeg&width=" + w + "&height=" + h);
image = javax.microedition.lcdui.Image.createImage(imageByte, 0, imageByte.length);
w = image.getWidth();
h = image.getHeight();
infoStringItem.setText(
"image OK." + " w=" + w + " h=" + h);
} catch (Exception e) {
infoStringItem.setText(
"Capt Error! e=" + e);
}
}
}
| |