First of all, we have to be sure that our device supports camera encodings.
For check this use below code.
Code:
private String[] capabilitiesIdx = {
“supports.audio.capture”,
“supports.video.capture”,
“supports.recording”,
“audio.encodings”,
“video.encodings”,
“video.snapshot.encodings”, // “streamable.contents”
};
StringItem[] str = new StringItem[capabilitiesIdx.length];
for (int i = 0; i < capabilitiesIdx.length; ++i) {
str[i] = new StringItem(System.getProperty(capabilitiesIdx[i]), “-” + capabilitiesIdx[i]);
}
if your phone supports video capture now you can control your camera.
Initiliaze Camera
Code:
try {
//creates a new player and set it to realize
player = Manager.createPlayer("capture://video");
player.realize();
//Grap the Video control and set it to the current display
videoControl = (VideoControl) (player.getControl("VideoControl"));
if (videoControl != null) {
append((Item) (videoControl.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE, null)));
captureCommand = new Command("Çek", Command.SCREEN, 1);
addCommand(captureCommand);
} else {
messageItem.setText("No video control");
}
} catch (IOException ioe) {
messageItem.setText("IOException: " + ioe.getMessage());
} catch (MediaException me) {
messageItem.setText("Media Exception: " + me.getMessage());
} catch (SecurityException se) {
messageItem.setText("Security Exception: " + se.getMessage());
}
Start Camera
Code:
try {
if (player != null) {
player.start();
}
if (videoControl != null) {
videoControl.setVisible(true);
}
} catch (MediaException me) {
messageItem.setText("Media Exception: " + me.getMessage());
} catch (SecurityException se) {
messageItem.setText("Security Exception: " + se.getMessage());
}
active = true;
Get SnapShot
Code:
try {
image = videoControl.getSnapshot("encoding=jpeg&width=640&height=480");
} catch (MediaException me) { //yüksek çözünürlük destekle
check = false;
}
if(!check){//hata varsa
image = videoControl.getSnapshot("encoding=png");
}
Stop Camera
Code:
try {
if (videoControl != null) {
videoControl.setVisible(false);
}
if (player != null) {
player.stop();
player.close();
}
} catch (MediaException me) {
messageItem.setText("Media Exception: " + me.getMessage());
}