Taking photos

In Symbian, you can use the Mobile Media API to take photos with the device camera. Specifically, you can take snapshots of the camera's video input. In Series 40 taking snapshots of the video input is not supported.

To take photos with the device camera:

  1. Create a Player for capturing video input from the device camera, and initialize the Player.

    Player player;
    player = Manager.createPlayer("capture://video");
    player.realize();
    player.prefetch();
    

    In Series 40, use capture://image.

    For more information about video recording locators, see section Recording Sound and Video

  2. To display the video input, create a VideoControl for the Player, and start the Player. The user can now see a live video feed from the device camera.

    VideoControl videoControl;
    videoControl = (VideoControl)player.getControl("VideoControl");
    if (videoControl != null) {
        videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
        videoControl.setVisible(true);
        player.start();
    }
    
  3. Take a snapshot of the video input by calling the getSnapshot method of the VideoControl object. The following code snippet takes a snapshot in the default format.

    byte[] pngImage = videoControl.getSnapshot(null);

    You can also specify the image format and resolution for snapshots. For example:

    videoControl.getSnapshot("encoding=image/jpg"); // JPG format with default resolution
    videoControl.getSnapshot("width=640&height=480"); // default format with 640 x 480 pixel resolution
    videoControl.getSnapshot("encoding=image/jpg&width=640&height=480"); // JPG format with 640 x 480 pixel resolution
    

    For more information about defining image formats and resolutions for snapshots, see the following sections on this page.

  4. Use the snapshot. For example, depending on the purpose of your MIDlet, you can save the snapshot in a record store for later use, send it to a server over an HTTP connection, or display it on the device screen as an Image.

Specifying the image format for snapshots

If you do not want to use the default image format for snapshots, specify a different image format by using the encoding parameter:

videoControl.getSnapshot("encoding=<format suffix>");

For example, to take a snapshot in the BMP format:

videoControl.getSnapshot("encoding=bmp");

Supported image types can be, for example, encoding=png, encoding=bmp, encoding=jpeg, and encoding=gif. JPEG format is the most compact format. The use of compact format reduces memory usage, and is therefore recommended in many cases. To find out which image formats are supported by the device, use the video.snapshot.encodings system property:

String supportedFormats = System.getProperty("video.snapshot.encodings");

You can define image color as a RGB colored image or a Grayscale Image. For example: color=gray8 or color=gray16.

JPEG images have a quality parameter that is defined as an integer between 10 and 100.

You can also specify width and height of the image. The following string specifies the resultant image with JPEG image format, width of 4000, height of 3000 and quality of 100.

video.Control.getSnapshot(“encoding=jpeg&width=4000&height=3000&quality=100”);

Compatible sizes

When using non-default image sizes, the behavior of getSnapshot depends on the dimensions requested. If the requested image falls into platform compatible size it is directly obtained and returned to the user. The compatible sizes supported in Nokia N8 are listed in the table.

Table: Compatible sizes in Nokia N8

Size

Format

640x480

VGA

1024x768

XGA

1280x960

SXGA

1600x1200

UXGA

2048x1536

QXGA

2592x1456

5 MP, 16:9

2592x1944

5 MP, 4:3

3264x1832

8 MP, 16:9

3264x2448

8 MP, 4:3

4000x2248

12 MP, 16:9

4000x3000

12 MP, 4:3

If the requested image has same aspect ratio as of the captured image but the requested size is not same as captured size, then the captured image is scaled up or down to the requested size. For example, the requested size is 400x300, it has an aspect ratio of 4:3 but it is not one of the supported sizes. The nearest size (640x480) is used to capture the image and then it is scaled up to get the requested size.

If the requested image has an aspect ratio other than the aspect ratio of the captured image, scaling and padding is applied to obtain the requested image. For example , the requested size of image is 3000x3000, and aspect ratio of the captured image is 4:3, then the captured image will be scaled up or down to 3000x2250 to best fit into the requested size without losing any image data. The remaining area (3000-2250 = 750) is padded with white pixels from both the sides (375 from each side).

Note: It is advised to use device camera compatible sizes or at least sizes with an aspect ratio of 4:3 in landscape mode and 3:4 in portrait mode. Otherwise, the resultant image will be adjusted with white padding.

From Symbian^3 onwards, if encoding=default is specified, the resultant image uses the maximum supported size and the highest quality available in a device. For example, if a camera is of 12MP then the resultant image size will be 4000x3000 and if it is 5MP then it will be 2592x1944.

Note: In Java Runtime 2.1 for Symbian, the view finder is always fixed to Landscape mode.

For a complete example of a MIDlet that takes photos using this method, see document Camera MIDlet: A Mobile Media Example on Nokia Developer.

For additional examples, see the following articles in the Nokia Developer Wiki: