我是lwuit的新手,我需要知道如何使用相机捕捉图像。我尝试了以下代码,但它无法正常工作。你可以建议我的代码或任何替代品..
if (checkCameraSupport() == false) {
} else if (checkPngEncodingSupport() == false) {
} else {
try {
createCameraForm();
createCamera();
addCameraToForm();
startCamera();
} catch (IOException ioExc) {
} catch (MediaException mediaExc) {
}
createImageForm();
方法是:
private boolean checkCameraSupport() {
String propValue = System.getProperty("supports.video.capture");
System.out.println("supports.video.capture");
return (propValue != null) && propValue.equals("true");
}
private boolean checkPngEncodingSupport() {
String encodings = System.getProperty("video.snapshot.encodings");
System.out.println("video.snapshot.encodings");
return (encodings != null) && (encodings.indexOf("jpeg") != -1);
}
private void createCameraForm() {
camfrm = new Form("Camera");
command1 = new Command("Capture");
command2 = new Command("Exit");
camfrm.addCommand(command1);
camfrm.addCommand(command2);
camfrm.addCommandListener(this);
}
private void createCamera() throws IOException, MediaException {
player = Manager.createPlayer("capture://image");
player.realize();
player.prefetch();
videoControl = (VideoControl) player.getControl("VideoControl");
}
private void addCameraToForm() {
System.out.println("hai123");
camfrm.addComponent((MediaComponent) videoControl.
initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
"com.sun.lwuit.MediaComponent"));
}
private void startCamera() throws IOException, MediaException {
if (player.getState() == Player.PREFETCHED) {
player.start();
camfrm.show();
}
}
private boolean captureImage() {
try {
byte[] imageData;
imageData = videoControl.getSnapshot("encoding=jpeg");
cpImg = Image.createImage(imageData, 0, imageData.length);
} catch (MediaException exc) {
return false;
} catch (SecurityException secExc) {
return false;
}
return true;
}
private void showCapturedImage() {
imgFrm.removeAll();
if (cpImg != null) {
imgFrm.addComponent(new Label(cpImg));
}
imgFrm.show();
}
private void createImageForm() {
imgFrm = new Form("Captured image");
command3 = new Command("Back");
imgFrm.addCommand(command3);
imgFrm.addCommandListener(this);
}