Take a picture in a BlackBerry device application

  1. Import the required classes and interfaces.
    import net.rim.device.api.amms.control.camera.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    
    
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The ImageCaptureDemoScreen class, which is described in step 3, represents the custom screen.
    public class ImageCaptureDemo extends UiApplication
    {
         public static void main(String[] args)
         {
              ImageCaptureDemo app = new ImageCaptureDemo();
              app.enterEventDispatcher();
         }
        
         public ImageCaptureDemo()
         {
              pushScreen(new ImageCaptureDemoScreen());
         }
    }
  3. Create the framework for the custom screen by extending the MainScreen class. Declare class variables for the Player and VideoControl classes that you create in the screen's constructor, which is described in step 4.
    class ImageCaptureDemoScreen extends MainScreen
    {	
        Player _p;
        VideoControl _videoControl;    
     
        public ImageCaptureDemoScreen()
        {
        }
    }
  4. In the screen constructor, initialize the camera. In a try/catch block, create an instance of the Player class by invoking Manager.createPlayer(String), passing in the encoding parameter. The jpeg and image/jpeg parameters are supported. If you do not specify the encoding parameter, by default the encoding parameter is image/jpeg.
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
       
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  5. To control an aspect of taking the picture, retrieve the appropriate Control object. Invoke the Player object's realize() method to access the associated Control object. Next, invoke Player.getControl() to retrieve the Player object's VideoControl.
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
        _p.realize();
        _videoControl = (VideoControl) _p.getControl("VideoControl");
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  6. Create a viewfinder in your application by invoking VideoControl.initDisplayMode(int mode, Object arg) and passing in a parameter specifying the UI primitive that displays the picture to initialize the mode that a video field uses. Cast the returned object as a Field object. Invoke VideoControl.setDisplayFullScreen() passing in true to set the viewfinder to take up the full screen of the device. Invoke VideoControl.setVisible() passing in true to display the viewfinder.
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
        _p.realize();
        _videoControl = (VideoControl) _p.getControl("VideoControl");
    
        if (videoControl != null)
        {
            Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
            _videoControl.setDisplayFullScreen(true);
            _videoControl.setVisible(true);
        }
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  7. Invoke Player.start() to set the player to the STARTED state.
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
        _p.realize();
        _videoControl = (VideoControl) _p.getControl("VideoControl");
    
        if (videoControl != null)
        {
            Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
            _videoControl.setDisplayFullScreen(true);
            _videoControl.setVisible(true);
    
            _p.start();
        }
    
        
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  8. To enable autofocus for the camera, invoke Player.getContol() to retrieve the Player object's EnhancedFocusControl interface. Iinvoke EnhancedFocusControl.startAutoFocus().
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
        _p.realize();
        _videoControl = (VideoControl) _p.getControl("VideoControl");
    
        if (videoControl != null)
        {
            Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
            _videoControl.setDisplayFullScreen(true);
            _videoControl.setVisible(true);
    
            _p.start();
    
            EnhancedFocusControl efc = (EnhancedFocusControl)p.getControl("net.rim.device.api.amms.control.camera.EnhancedFocusControl");
            efc.startAutoFocus();
        }    
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  9. Check that videoField is not null and invoke add() to add the viewfinder to the screen.
    try 
    {
        _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
        _p.realize();
        _videoControl = (VideoControl) _p.getControl("VideoControl");
    
        if (videoControl != null)
        {
            Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
            _videoControl.setDisplayFullScreen(true);
            _videoControl.setVisible(true);
    
            _p.start();
    
            EnhancedFocusControl efc = (EnhancedFocusControl)p.getControl("net.rim.device.api.amms.control.camera.EnhancedFocusControl");
            efc.startAutoFocus();
    
            if(videoField != null)
            {
                add(videoField);
            }
        }
    }
    catch(Exception e)
    {
        Dialog.alert(e.toString());
    }
  10. In ImageCaptureDemoScreen, invoke net.rim.device.api.ui.Screen.invokeAction(). This implementation takes the picture when the user clicks the trackpad, trackball, or touch screen. Return a boolean specifying if the action is consumed.
    protected boolean invokeAction(int action)
    {
        boolean handled = super.invokeAction(action); 
            
        if(!handled)
        {
            if(action == ACTION_INVOKE)
            {                         
                                
            }
        }  
                  
        return handled;                
    }
  11. In the if(action == ACTION_INVOKE) statement, in a try/catch block, invoke VideoControl.getSnapshot() to take the picture, passing in null to use the encoding setting that you specified in step 4. The image is returned as a byte array.
    protected boolean invokeAction(int action)
    {
        boolean handled = super.invokeAction(action); 
            
        if(!handled)
        {
            if(action == ACTION_INVOKE)
            {   
                try
                {                      
                    byte[] rawImage = _videoControl.getSnapshot(null);        
                }
                catch(Exception e)
                {
                    Dialog.alert(e.toString());
                }
            }
        }           
        return handled;                
    }
Previous topic: Taking pictures

Was this information helpful? Send us your comments.