Header Sep

Java Tips, Tricks & Code

Score
Login to rate page

April 2004

Taking Snapshots with the Z1010 camera

With a Java MIDlet you can use the video camera in the Z1010 to take snapshots for use in your application.

Taking snapshots with the camera is an optional part of the Mobile Media API (MMAPI, JSR-135). The Z1010 supports this functionality, something that opens up a lot of new possible applications.
Because the camera functionality is part of the MMAPI, using the camera is not much different from using any other device included in that specification. Basically, a Player is used together with a Controller, in this case a VideoController, both for showing the viewfinder and for capturing an image.

The VideoController is used in two different ways to let the viewfinder appear either on a Form or a Canvas, as in this code:

myPlayer = Manager.createPlayer("capture://video");
myPlayer.realize();
            
// Grab the video control and set it to the current display.
vc = (VideoControl)myPlayer.getControl("VideoControl");
if (vc != null) {
              
// Use this to add the viewfinder to the Form myForm
myForm.append((Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null));
// … Or this to add it to the Canvas myCanvas
//vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, myCanvas);
// Set the viewfinder size
      vc.setDisplaySize(120,160);
}

 

To capture an image, you just call:

byte[] byteImage = vc.getSnapshot(null);
Image camImage = Image.createImage(byteImage, 0, byteImage.length);

Important to know is that the size of the captured image will always be size120 * 160. There are two things to note about this:

Firstly, you cannot pass any size parameters to cretePlayer() to change this. For example, the following will not work:

Manager.createPlayer("capture://video?encoding=png&width=80&height=60");

Secondly, the size of the viewfinder has no impact on the size of the captured image.

Besides this, there is really not much more to it. The attached MIDlet is a simple example and shows the functionality. It will display a viewfinder and at the click on the "click" button, it will take a snapshot and show it on a Canvas. 

Download example
Download example

Read more about the Mobile Media API>>

Score
Login to rate page