×
Namespaces

Variants
Actions

Determining the number of cameras in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N73, Nokia E7-00
Compatibility
Platform(s): S60 3rd Edition FP2, Nokia Belle
Symbian
Nokia Belle
Java Runtime 2.3 for Symbian
S60 3rd Edition FP2
Article
Created: dekudin (28 Nov 2008)
Last edited: hamishwillee (29 Jul 2013)

Contents

Overview

This code snippet demonstrates how to determine the number of cameras presented on device.

To retrieve the number of cameras this snippet creates player for each camera. If the player for any camera could not be created, this means that there is no such camera on the phone. So the snippet creates a player for camera devcam0, then (if it was successful), for camera devcam1 and so on until an exception is received because a player for camera devcamN could not be created, meaning that this camera does not exist. So number of cameras = N-1.

Source file: NumberOfCameras.java

import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
 
 
public class NumberOfCameras extends MIDlet implements CommandListener {
 
private Display display;
// Form where number of cameras is placed.
private Form form;
// Command for exiting from midlet.
private Command cmdExit;
 
/**
* Constructor
*/

public NumberOfCameras() {
initializeComponents();
}
 
/**
* Initializes components of midlet
*/

private void initializeComponents() {
// Get display
display = Display.getDisplay(this);
 
// Create form
form = new Form("Cameras avaliable");
cmdExit = new Command("Exit", Command.EXIT, 0);
form.addCommand(cmdExit);
form.setCommandListener(this);
 
// Output number of cameras
int numCameras = getNumberOfCameras();
form.append(new StringItem("Number of cameras", Integer.toString(numCameras)));
}
 
/**
* Gets number of cameras on the phone.
* To retrieve number of cameras this method creates player for each camera.
* If player for some camera can not be created this means that there is
* no such camera on the phone and method already created players for all
* cameras.
* @return number of cameras.
*/

private int getNumberOfCameras() {
int camIndex = 0;
 
try {
for( ; ; ++camIndex) {
String locator = "capture://devcam" + Integer.toString(camIndex);
Player player = Manager.createPlayer(locator);
player.close();
}
}
catch(MediaException mediaExc) {
// This exception is thrown if player can not be created
// from locator.
return camIndex;
}
catch(Exception exc) {
return 0;
}
}
 
/**
* From MIDlet.
* Signals the MIDlet that it has entered the Active state.
*/

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

public void pauseApp() {
// No implementation required
}
 
/**
* From MIDlet.
* Signals the MIDlet to terminate and enter the Destroyed state.
*/

public void destroyApp(boolean unconditional) {
// No implementation required
}
 
/**
* Performs exit from midlet.
*/

private void exitMIDlet() {
notifyDestroyed();
}
 
/**
* From CommandListener.
* Indicates that a command event has occurred on Displayable d.
* @param cmd - a Command object identifying the command.
* @param d - the Displayable on which this event has occurred.
*/

public void commandAction(Command cmd, Displayable d) {
if(cmd == cmdExit) {
exitMIDlet();
}
}
}

Postconditions

After running this midlet the string "Number of cameras" and a count of cameras on the device are displayed. If the device does not have a camera it will show 0.

Supplementary material

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

This page was last modified on 29 July 2013, at 07:54.
51 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×