×
Namespaces

Variants
Actions
Revision as of 19:25, 27 November 2008 by dekudin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Saving a captured image in Java ME

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): Nokia E70
CompatibilityArticle
Keywords: javax.microedition.media.Manager, javax.microedition.media.Player, javax.microedition.media.control.VideoControl, javax.microedition.media.Manager.createPlayer, javax.microedition.media.Player.getControl, javax.microedition.media.control.VideoControl.getSnapshot
Created: (20 Oct 2008)
Last edited: dekudin (27 Nov 2008)

Overview

This code snippet demonstrates how to take a picture by camera and save it to file.

To take a picture by camera and save it to file following steps should be performed:

  1. Check support of video capturing by calling System.getProperty method with supports.video.capture parameter.
  2. Call Manager.createPlayer method to create camera player.
  3. Initialize and start player.
  4. Get VideoControl object from player and execute VideoControl.getSnaphot method. This method returns array of bytes which represents captured image in specified format.
  5. Open file and send array of bytes to it. After this file should be closed.
  6. On exit from application camera player will be stopped and closed.


Source file: CaptureAndSaveImage.java

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Item;
 
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.MediaException;
 
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import java.io.OutputStream;
 
public class CaptureAndSaveImage extends MIDlet implements CommandListener {
 
private Display display;
 
// Form where camera viewfinder is placed
private Form cameraForm;
 
// Command for capturing image by camera and saving it.
// Placed in cameraForm.
private Command cmdCapture;
// Command for exiting from midlet. Placed in cameraForm.
private Command cmdExit;
 
// Player for camera
private Player player;
// Video control of camera
private VideoControl videoControl;
 
/**
* Constructor.
*/

public CaptureAndSaveImage() {
InitializeComponents();
}
 
/**
* Initializes components of midlet.
*/

private void InitializeComponents() {
display = Display.getDisplay(this);
 
if(checkCameraSupport() == false) {
exitMIDlet();
}
 
createCameraForm();
createCamera();
addCameraToForm();
}
 
/**
* Creates and returns form where the camera control will be placed.
*/

private void createCameraForm() {
// Create camera form
cameraForm = new Form("Camera");
// Create commands for this form
cmdCapture = new Command("Capture image", Command.SCREEN, 0);
cmdExit = new Command("Exit", Command.EXIT, 0);
// Add commands to form
cameraForm.addCommand(cmdCapture);
cameraForm.addCommand(cmdExit);
// Set midlet as command listener for this form
cameraForm.setCommandListener(this);
}
 
/**
* Check camera support.
* @return true if camera is supported, false otherwise.
*/

private boolean checkCameraSupport() {
String propValue = System.getProperty("supports.video.capture");
if(propValue == null) {
return false;
}
if(propValue.equals("true") == true) {
return true;
}
return false;
}
 
/**
* Creates camera control and places it to cameraForm.
*/

private void createCamera() {
try {
player = Manager.createPlayer("capture://video");
player.realize();
player.prefetch();
 
videoControl = (VideoControl)player.getControl("VideoControl");
} catch(Exception e) {
// TODO: handle exception.
exitMIDlet();
}
}
 
/**
* Adds created camera as item to cameraForm.
*/

private void addCameraToForm() {
cameraForm.append((Item)videoControl.
initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));
}
 
/**
* Start camera player
*/

private void startCamera() {
try {
if(player.getState() == Player.PREFETCHED) {
player.start();
}
} catch(Exception e) {
// TODO: handle exception.
exitMIDlet();
}
}
 
/**
* Saves image captured by camera.
*/

private void captureAndSaveImage() {
try {
// Capture image
byte[] capturedImageData =
videoControl.getSnapshot("encoding=jpeg");
 
// Get path to photos folder.
String dirPhotos = System.getProperty("fileconn.dir.photos");
if(dirPhotos == null) {
throw new NullPointerException("Unable get photos folder name");
}
 
String fileName = dirPhotos + "CapturedImage.jpg";
// Open file
FileConnection file = (FileConnection)Connector.open(fileName,
Connector.READ_WRITE);
// If there is no file then create it
if(file.exists() == false) {
file.create();
}
// Write data received from camera while making snapshot to file
OutputStream outStream = file.openOutputStream();
outStream.write(capturedImageData);
// Close file
outStream.close();
file.close();
} catch(Exception e) {
// TODO: handle exception.
}
}
 
/**
* From MIDlet.
* Signals the MIDlet that it has entered the Active state.
*/

public void startApp() {
display.setCurrent(cameraForm);
startCamera();
}
 
/**
* From MIDlet.
* Signals the MIDlet to enter the Paused state.
*/

public void pauseApp() {
// TODO: pause player if it is running.
}
 
/**
* Performs exit from midlet.
*/

public void exitMIDlet() {
notifyDestroyed();
}
 
/**
* From MIDlet.
* Signals the MIDlet to terminate and enter the Destroyed state.
*/

public void destroyApp(boolean unconditional) {
if(player != null) {
try {
player.stop();
player.deallocate();
player.close();
} catch (MediaException ex) {
// TODO: handle exception
}
}
}
 
/**
* From CommandListener.
* Indicates that a command event has occurred on Displayable displayable.
* @param command - a Command object identifying the command.
* @param displayable - the Displayable on which this event has occurred.
*/

public void commandAction(Command command, Displayable displayable) {
// Handles "Capture image" command from cameraForm
if(command == cmdCapture) {
captureAndSaveImage();
}
// Handles "exit" command from forms
if(command == cmdExit) {
exitMIDlet();
}
}
}


Postconditions

Camera viewfinder is shown on display.

By choosing "Capture image" menu command user can make a snapshot and save it to CapturedImage.jpg file in photos folder. Image will be saved to file with jpeg encoding.

Supplementary material

Executables and source files can be found in Media:SavingCapturedImage.zip

171 page views in the last 30 days.
×