Home of www.RoboHobby.com Java robotics project www.CubesAndCrosses.com
Train your logic skills in geometry
Create and delete one-color crosses
Play for free right in your browser!
Home-made CNC machine
Our new project -
How to make home-made CNC router
PICkit2 - Tutorials, Experiments
PICkit 2 - Small size USB programmer
from Microchip.
How to use it as a demo device for
USB programming in C language




www.RoboHobby.com
Home of www.RoboHobby.com
Java robotics project


Portable Notebook Table
How to make Portable Notebook Table.
Do it yourself!

Java SE SWING JSR-296 Example
How to run background task in
JSR-296 SWING application
with 'busy animation' in the status bar.
Useful addition to Netbeans template.

Java ME Code Examples
Java ME Source code examples
for robotics projects


Run Java ME Code Examples
How to run our Java ME code examples
for robotics projects



www.RoboHobby.com
/*
 * J2ME code example.  Disc Command. Midlet.
 * Part of www.RoboHobby.com Java robotics project.
 * www.RoboHobby.com
 */


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 Command timerItemCommand;
    private Form form;
    private StringItem infoStringItem;
    private StringItem timerStringItem;
    //</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 iscalled only once whenthe MIDletis started.
     * The method is calledbefore 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();
            } else if (command == timerItemCommand) {                                          
                // write pre-action user code here
                                           
                // write post-action user code here
                doStartStopTimer();
            }                                                  
        }                                                
        // 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(), getTimerStringItem() });                                    
            form.addCommand(getExitCommand());
            form.addCommand(getDoSnapShotCommand());
            form.addCommand(getGetRootsItemCommand());
            form.addCommand(getTimerItemCommand());
            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>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: timerStringItem ">                                   
    /**
     * Returns an initiliazed instance of timerStringItem component.

     * @return the initialized component instance
     */
    public StringItem getTimerStringItem() {
        if (timerStringItem == null) {                                 
            // write pre-init user code here

            timerStringItem = new StringItem("Timer:", "");                                   
            // write post-init user code here
        }                         
        return timerStringItem;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: timerItemCommand ">                                   
    /**
     * Returns an initiliazed instance of timerItemCommand component.

     * @return the initialized component instance
     */
    public Command getTimerItemCommand() {
        if (timerItemCommand == null) {                                 
            // write pre-init user code here

            timerItemCommand = new Command("Timer Start/Stop", Command.ITEM, 0);                                   
            // write post-init user code here
        }                         
        return timerItemCommand;
    }
    //</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);
        }
    }
    ///////////////////////////////////////////////////////////////

    boolean timerIsRunning = false;
    java.util.Timer timer = null;
    int timerDelay = 2000;
    int keyCounter = 0;

    RobotTimerTask robotTimerTask = null;

    public void doStartStopTimer()
    {
        System.out.println( "timerIsRunning="+timerIsRunning + " keyCounter="+keyCounter );
        keyCounter++;

        if ( timerIsRunning )
        {
            System.out.println( "Stopping Timer." );
            try

            {
                this.infoStringItem.setText( "Stopped" );
                robotTimerTask.doStop();
            }
            catch ( Exception e )
            {
                System.out.println( "Stopping Timer Error e=" + e );
            }
            timerIsRunning = false;
        }
        else

        {
            System.out.println( "Starting Timer:" );
            try
            {
                this.infoStringItem.setText( "Started" );
                robotTimerTask = new RobotTimerTask();
                timer = null;

                timer = new java.util.Timer();
                timer.schedule( robotTimerTask, 0L, timerDelay );
            }
            catch ( Exception e )
            {
                System.out.println( "Starting Timer Error e=" + e );
            }
            timerIsRunning = true;
        }
    }

}









Google
 



Contact us:


My Skype name is oleg_skp
Start Skype text chatStart Skype text chat
View Skype profileView Skype profile
Start Skype voice callStart Skype voice call
Add me to your Skype contact listAdd to Skype contact list
Leave voicemail using SkypeLeave voicemail using Skype
Send a file using SkypeSend a file using Skype
Note: To use these links
the Skype program should
be installed on your computer.
You can load it for free from
their official site:
Contact via Skypewww.Skype.com
------------------