Take a snapshot using the built-in camera of a BlackBerry smartphone

by Retired ‎02-16-2010 01:47 PM - edited ‎09-16-2010 03:29 PM (7,739 Views)



This article applies to the following:

  • BlackBerry® Device Software 4.6
  • BlackBerry® Java® Development Environment (BlackBerry JDE) 4.6


The BlackBerry smartphone application programming interfaces (API) of BlackBerry JDE 4.6 enable functionality for capturing image snapshots using the built-in cameras of BlackBerry smartphones running BlackBerry Device Software 4.6 or later. This new functionality was enabled by implementing the VideoControl.getSnapshot(String imageType) as part of the Mobile Media API (JSR135). This article will walk you through an example of how to leverage this new feature.


Initializing the player

Before you can take a snapshot, you need to set up a Player instance in capture mode. You also need to initialize a VideoControl from the Player. The following code shows you how to set up a Player and initialize a VideoControl for taking snapshots:



Player player = Manager.createPlayer("capture://video");
VideoControl vc = (VideoControl) player.getControl("VideoControl");

Displaying the viewfinder

At this point, you need to initialize the viewfinder of the camera and display the live video on the screen. This is done using the initDisplay method of the VideoControl class. This method takes two parameters. The first parameter, mode, represents the mode in which the viewfinder graphical user interface (GUI) is displayed, while the second parameter, arg, is the fully qualified class name of the GUI component that is returned by initDisplay. The following modes are supported:



    In this mode, arg can be the fully qualified class name of primitive GUI components, for example javax.microedition.lcdui.Item or net.rim.device.api.ui.Field. Depending on the value of arg, initDisplay returns an Item or a Field.


    When this mode is used, arg must be the fully qualified class name of Canvas, for example javax.microedition.lcdui.Canvas or a subclass of it. In this case, initDisplay will return an instance of Canvas and the video will be directly rendered on this Canvas.

In our example, you will use USE_GUI_PRIMITIVE as the mode and net.rim.device.api.ui.Field as the arg to initialize and display the viewfinder. Note that the viewfinder must be initialized and completely visible on the screen before you can call getSnapShot to capture the image from it. The following code does just that and adds the returned Field to a Screen called home:



viewFinder = (Field)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");


The viewfinder can be displayed in full screen by calling




Taking the snapshot

Finally, you can call VideoControl.getSnapShot(String imageType) to take a snapshot. This method takes a snapshot according to the specified imageType and returns the raw bytes of the snapshot. The supported imageType(s) are


  • "encoding=jpeg&width=1600&height=1200&quality=superfine"
  • "encoding=jpeg&width=1600&height=1200&quality=fine"
  • "encoding=jpeg&width=1600&height=1200&quality=normal"
  • "encoding=jpeg&width=1024&height=768&quality=superfine"
  • "encoding=jpeg&width=1024&height=768&quality=fine"
  • "encoding=jpeg&width=1024&height=768&quality=normal"
  • "encoding=jpeg&width=640&height=480&quality=superfine"
  • "encoding=jpeg&width=640&height=480&quality=fine"
  • "encoding=jpeg&width=640&height=480&quality=normal"

The supported imageType(s) can also be queried by invoking





Here is an example that takes a snapshot at 1600x1200 resolution, stores the returned bytes in a byte array, and displays the image on a Screen named home.



String imageType = "encoding=jpeg&width=1600&height=1200&quality=superfine";
byte[] imageBytes = vc.getSnapshot(imageType);
Bitmap image = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 5);
BitmapField bitmapField = new BitmapField();

A more detailed sample application can be downloaded below.