Would you like to tell us how we are doing?

You bet No thanks

Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Working with pictures

The BlackBerry Java SDK lets you capture images in your app.

You can take a picture using the javax.microedition.media.Player class and the associated VideoControl interface. When you create a Player object, you can specify various properties of the picture and the camera, such as the encoding, the focus control mode, and the input scene mode.

The VideoControl.initDisplayMode() method allows you to create a viewfinder. The initDisplay() method returns a Field object that you can add to your UI. You can overlay UI controls on the display by using the AdvancedVideoControl interface.

When your app is ready to take a picture, invoke VideoControl.getSnapshot() . The unprocessed image is returned as a byte array that you can display on the device, save to a file, or process as required.

For more information about controlling the encoding and other camera properties, see Media recording properties.

Code sample: Taking a picture in your app

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.amms.control.camera.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

//Create the application framework by extending the UiApplication class. 
//In main(), create an instance of the new class and invoke 
//enterEventDispatcher() to enable the app to receive events. 
//In the application constructor, invoke pushScreen() to display the 
//custom screen for the app. The ImageCaptureDemoScreen class
//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());
    }   

//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.  
  class ImageCaptureDemoScreen extends MainScreen
  {	
     Player _p;
     VideoControl _videoControl;    
 
     public ImageCaptureDemoScreen()
     {

//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");

//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. 
//Invoke Player.getControl() to retrieve the Player object's VideoControl.  
           _p.realize();
           _videoControl = (VideoControl) _p.getControl("VideoControl");

           if (videoControl != null)
           {

//Create a viewfinder in your application by invoking 
//VideoControl.initDisplayMode(int mode, Object arg) 
//to initialize the mode that a video field uses. Pass a parameter to specify the UI 
//primitive that displays the picture. Cast the returned object as a Field object. 
//Invoke VideoControl.setDisplayFullScreen(true) to set the viewfinder to the full 
//screen of the device. Invoke VideoControl.setVisible(true) to display the viewfinder.  
               Field videoField = (Field) _videoControl.initDisplayMode(
                     VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
               _videoControl.setDisplayFullScreen(true);
               _videoControl.setVisible(true);

//Invoke Player.start() to set the player to the STARTED state.  
               _p.start();

//To enable autofocus for the camera, invoke Player.getContol() to retrieve the 
//Player object's EnhancedFocusControl interface. 
//Invoke EnhancedFocusControl.startAutoFocus(). 
//If the BlackBerry device does not support autofocus, startAutoFocus() has no effect.  
               EnhancedFocusControl efc = (EnhancedFocusControl)p.getControl(
                    "net.rim.device.api.amms.control.camera.EnhancedFocusControl");
               efc.startAutoFocus();

//Check that videoField is not null and invoke add() to add the viewfinder to the screen.
               if(videoField != null)
               {
                   add(videoField);
               }
           } 
       }
       catch(Exception e)
       {
           Dialog.alert(e.toString());
       }
   }

//In ImageCaptureDemoScreen, invoke net.rim.device.api.ui.Screen.invokeAction(). 
//This implementation takes a picture when the user clicks the trackpad, trackball, or 
//touch screen. Return a Boolean that indicates whether the action is consumed.  
   protected boolean invokeAction(int action)
   {
       boolean handled = super.invokeAction(action); 
        
       if(!handled)
       {

//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 previously. The image is returned as a byte array.  
           if(action == ACTION_INVOKE)
           {   
               try
               {                      
                  byte[] rawImage = _videoControl.getSnapshot(null);        
          }
               catch(Exception e);
               {
                   Dialog.alert(e.toString());
               }
          }
       }           
       return handled;                
    }  
  }   
}