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.Taglet * RobotMIDlet * @author Oleg Lyubchenko */ public class RobotMIDlet extends MIDlet implements CommandListener { public boolean midletPaused = false; private Player player = null; private VideoControl videoControl = null; private boolean active = false; //public StringItem messageItem; byte[] imageByte; javax.microedition.lcdui.Image image; int w = 256; //160; //256; //320; //640; //160; //132; int h = 192; //120; //192; //240; //480; //120; //114; int[] rgbData = new int[w * h]; CallBacks callBacks = CallBacks.getInstance(); //<editor-fold defaultstate="collapsed" desc=" Generated Fields "> private Command exitCommand; private Command doSnapShotCommand; private Command infoItemCommand; private Command itemCommand; private Command screenCommand; private Command getRootsItemCommand; private Form form; private StringItem infoStringItem; //</editor-fold> /** * The RobotMIDlet constructor. */ public RobotMIDlet() { callBacks.robotMIDlet = this; callBacks.discUtils = new DiscUtils(); } //<editor-fold defaultstate="collapsed" desc=" Generated Methods "> //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize "> /** * Initilizes the application. * It is called only once when the MIDlet is started. * The method is called before the <code>startMIDlet</code> method. */ private void initialize() { // write pre-initialize user code here // write post-initialize user code here } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet "> /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() { // write pre-action user code here switchDisplayable(null, getForm()); // write post-action user code here } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet "> /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() { // write pre-action user code here // write post-action user code here } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable "> /** * Switches a current displayable in a display. * The <code>display</code> instance is taken from <code>getDisplay</code> method. * This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; * if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) { // write pre-switch user code here Display display = getDisplay(); if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); } // write post-switch user code here //CameraForm cameraForm = new CameraForm(); //display.setCurrent( cameraForm ); //nextDisplayable); } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables "> /** * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) { // write pre-action user code here if (displayable == form) { if (command == doSnapShotCommand) { // write pre-action user code here // write post-action user code here doSnapShot(); } else if (command == exitCommand) { // write pre-action user code here exitMIDlet(); // write post-action user code here } else if (command == getRootsItemCommand) { // write pre-action user code here // write post-action user code here getRoots(); } } // write post-action user code here } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand "> /** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) { // write pre-init user code here exitCommand = new Command("Exit", Command.EXIT, 0); // write post-init user code here } return exitCommand; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form "> /** * Returns an initiliazed instance of form component. * @return the initialized component instance */ public Form getForm() { if (form == null) { // write pre-init user code here form = new Form("www.RoboHobby.com", new Item[] { getInfoStringItem() }); form.addCommand(getExitCommand()); form.addCommand(getDoSnapShotCommand()); form.addCommand(getGetRootsItemCommand()); form.setCommandListener(this); // write post-init user code here //form.append(infoStringItem); initCamera(); } return form; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: doSnapShotCommand "> /** * Returns an initiliazed instance of doSnapShotCommand component. * @return the initialized component instance */ public Command getDoSnapShotCommand() { if (doSnapShotCommand == null) { // write pre-init user code here doSnapShotCommand = new Command("SnapAndSave", Command.ITEM, 0); // write post-init user code here } return doSnapShotCommand; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand "> /** * Returns an initiliazed instance of itemCommand component. * @return the initialized component instance */ public Command getItemCommand() { if (itemCommand == null) { // write pre-init user code here itemCommand = new Command("Item", Command.ITEM, 0); // write post-init user code here } return itemCommand; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: infoItemCommand "> /** * Returns an initiliazed instance of infoItemCommand component. * @return the initialized component instance */ public Command getInfoItemCommand() { if (infoItemCommand == null) { // write pre-init user code here infoItemCommand = new Command("State: ", Command.ITEM, 0); // write post-init user code here } return infoItemCommand; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: infoStringItem "> /** * Returns an initiliazed instance of infoStringItem component. * @return the initialized component instance */ public StringItem getInfoStringItem() { if (infoStringItem == null) { // write pre-init user code here infoStringItem = new StringItem("Info: ", ""); // write post-init user code here } return infoStringItem; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand "> /** * Returns an initiliazed instance of screenCommand component. * @return the initialized component instance */ public Command getScreenCommand() { if (screenCommand == null) { // write pre-init user code here screenCommand = new Command("Screen", Command.SCREEN, 0); // write post-init user code here } return screenCommand; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Getter: getRootsItemCommand "> /** * Returns an initiliazed instance of getRootsItemCommand component. * @return the initialized component instance */ public Command getGetRootsItemCommand() { if (getRootsItemCommand == null) { // write pre-init user code here getRootsItemCommand = new Command("Roots", Command.ITEM, 0); // write post-init user code here } return getRootsItemCommand; } //</editor-fold> /** * Returns a display instance. * @return the display instance. */ public Display getDisplay () { return Display.getDisplay(this); } /** * Exits MIDlet. */ 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); } /** * Called when MIDlet is started. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. */ public void startApp() { System.gc(); Displayable current = Display.getDisplay(this).getCurrent(); if(current ==null) //first call { startMIDlet(); start(); } else //returning from pauseApp { if(current == form) //callBacks.cameraForm) { start(); } Display.getDisplay(this).setCurrent(current); } /* if (midletPaused) { resumeMIDlet (); } else { initialize (); startMIDlet (); } midletPaused = false; */ } /** * Called when MIDlet is paused. */ public void pauseApp() { midletPaused = true; } /** * Called to signal the MIDlet to terminate. * @param unconditional if true, then the MIDlet has to be * unconditionally terminated and all resources has to be released. */ public void destroyApp(boolean unconditional) { } public void initCamera() { try { player = Manager.createPlayer("capture://video"); player.realize(); //Grap the Video control and set it to the current display 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); } } /////////////////////////////////////////////////////////////// }
Tuesday, August 19, 2014
remember java me camera example
Labels:
Micro Devices
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment