×
Namespaces

Variants
Actions
(Difference between revisions)

How to display the camera & take snapshots in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "Category:Multimedia" to "")
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
Line 1: Line 1:
 
[[Category:Java ME]][[Category:Imaging]][[Category:Code Snippet]][[Category:Camera]]
 
[[Category:Java ME]][[Category:Imaging]][[Category:Code Snippet]][[Category:Camera]]
 
{{Abstract|This Java ME Camera MIDletcode example shows how to display the camera and take snapshots.}}
 
{{Abstract|This Java ME Camera MIDletcode example shows how to display the camera and take snapshots.}}
{{ArticleMetaData <!-- v1.1 -->
+
{{ArticleMetaData <!-- v1.3 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|id= <!-- Article Id (Knowledge base articles only) -->
+
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->

Revision as of 07:11, 26 July 2013

This Java ME Camera MIDletcode example shows how to display the camera and take snapshots.

Article Metadata
Article
Created: wang_shui (20 Mar 2007)
Last edited: hamishwillee (26 Jul 2013)

Source code

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.media.control.VideoControl;
 
public class CameraMIDlet extends MIDlet implements CommandListener {
 
private Display display;
private Form form;
private Command exit,back,capture,camera;
private Player player;
private VideoControl videoControl;
private Video video;
 
public CameraMIDlet() {
 
exit = new Command("Exit", Command.EXIT, 0);
camera = new Command("Camera", Command.SCREEN, 0);
back = new Command("Back", Command.BACK, 0);
capture = new Command("Capture", Command.SCREEN, 0);
 
form = new Form("Capture Video");
form.addCommand(camera);
form.setCommandListener(this);
}
 
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}
 
public void pauseApp() {}
 
public void destroyApp(boolean unconditional) {}
 
public void commandAction(Command c, Displayable s) {
if (c == exit) {
destroyApp(true);
notifyDestroyed();
} else if (c == camera) {
showCamera();
} else if (c == back)
display.setCurrent(form);
else if (c == capture) {
video = new Video(this);
video.start();
}
}
 
public void showCamera() {
try {
player = Manager.createPlayer("capture://video"); // "capture://video" is used for S60 devices
//player = Manager.createPlayer("capture://image"); // "capture://image" is used for Series 40 devices
player.realize();
 
videoControl = (VideoControl)player.getControl("VideoControl");
Canvas canvas = new VideoCanvas(this, videoControl);
canvas.addCommand(back);
canvas.addCommand(capture);
canvas.setCommandListener(this);
display.setCurrent(canvas);
player.start();
} catch (IOException ioe) {} catch (MediaException me) {}
}
 
class Video extends Thread {
CameraMIDlet midlet;
public Video(CameraMIDlet midlet) {
this.midlet = midlet;
}
 
public void run() {
captureVideo();
 
}
 
public void captureVideo() {
try {
byte[] raw = videoControl.getSnapshot(null);
Image image = Image.createImage(raw, 0, raw.length);
form.append(image);
display.setCurrent(form);
 
player.close();
player = null;
videoControl = null;
} catch (MediaException me) { }
}
};
}


The following code is used for displaying camera video using canvas:

import javax.microedition.lcdui.*;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.VideoControl;
 
public class VideoCanvas extends Canvas {
private CameraMIDlet midlet;
 
public VideoCanvas(CameraMIDlet midlet, VideoControl videoControl) {
int width = getWidth();
int height = getHeight();
this.midlet = midlet;
 
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
try {
videoControl.setDisplayLocation(2, 2);
videoControl.setDisplaySize(width - 4, height - 4);
} catch (MediaException me) {}
videoControl.setVisible(true);
}
 
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
 
g.setColor(0x00ff00);
g.drawRect(0, 0, width - 1, height - 1);
g.drawRect(1, 1, width - 3, height - 3);
}
}

Using this code

  1. Create a new project in Sun WTK or in NetBeans as CameraMIDlet.
  2. Copy the two files according to their class name.
  3. Build Project.
  4. Run Project.

TODO: list available modes, and when you need to use a different locator

Helpful links

198 page views in the last 30 days.
×