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.
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");
player.realize();
player.start();
VideoControl vc = (VideoControl) player.getControl("VideoControl");
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:
USE_GUI_PRIMITIVEIn this mode,
argcan be the fully qualified class name of primitive GUI components, for examplejavax.microedition.lcdui.Itemornet.rim.device.api.ui.Field. Depending on the value ofarg,initDisplayreturns anItemor aField.USE_DIRECT_VIDEOWhen this mode is used,
argmust be the fully qualified class name ofCanvas, for examplejavax.microedition.lcdui.Canvasor a subclass of it. In this case,initDisplaywill return an instance ofCanvasand the video will be directly rendered on thisCanvas.
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");
home.add(viewFinder);
The viewfinder can be displayed in full screen by calling
vc.setDisplayFullScreen(true);
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
System.getProperty("video.snapshot.encodings")
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();
bitmapField.setBitmap(image);
home.add(bitmapField);
A more detailed sample application can be downloaded below.