Java Reference
In-Depth Information
if (vc != null) {
Item vi = (Item) vc.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE, null);
viewer.append(vi);
}
Display.getDisplay(this).setCurrent(viewer);
player.start();
} catch (MediaException e) {}
catch (IOException e) {}
}
private void stopCamera() {
try {
if (player!=null) {
player.stop();
player.close();
player = null;
}
} catch (MediaException e) {}
}
private void capture() {
if (vc != null) try {
byte[] imageBytes = vc.getSnapshot(null);
// do something with imageBytes...
} catch (MediaException e) {}
}
■
Caution
All of these methods should be invoked on their own thread; running them on the UI thread will
likely block the UI thread for an unacceptably long time, causing application stalls. On some platforms, this
may cause other problems, too, such as hanging the application, especially if the Java platform's security
manager wants to prompt the user to approve the application's use of the
getSnapshot
method. To see
how to do this in the context of an actual multithreaded application, read the upcoming “Putting the MMAPI
and the SVGAPI to Work” section, especially the “Capturing Images” subsection.
By now, you can probably muddle through the purpose of
startCamera
. It gets a new
Player
instance that uses the device's imaging sensor as a video source, realizes and ini-
tializes the instance, and obtains a
VideoControl
instance to display on the screen as a
camera viewfinder. The
stopCamera
method is simple, too; it just tears down the
Player
instance.