This code snippet demonstrates how to use MMAPI and eSWT together to take a picture using the phone's camera. This code displays the camera viewfinder on an eSWT Control. It also provides an eSWT Command which will take the picture and save it.
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import org.eclipse.ercp.swt.mobile.Command;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MMAPIExample extends MIDlet implements Runnable, SelectionListener {
Thread UIThread;
Display display;
Shell shell;
private Command takePictureCommand;
private VideoControl videoControl;
public void startApp() {
// Create the eSWT UI thread.
if(UIThread == null) {
UIThread = new Thread(this);
UIThread.start();
}
}
public void pauseApp() {
// Here we could reduce the resources but we should keep the Display instance
// and the eSWT UI Thread.
}
public void destroyApp(boolean unconditional) {
// Dispose the remaining resources.
display.dispose();
}
// The eSWT UI Thread
public void run() {
// Create the Display.
display = new Display();
shell = new Shell(display);
shell.open();
Command exitCommand = new Command(shell, Command.EXIT, 0);
exitCommand.setText("Exit");
exitCommand.addSelectionListener(this);
takePictureCommand = new Command(shell, Command.GENERAL, 10);
takePictureCommand.setText("Snap!");
takePictureCommand.addSelectionListener(this);
takePictureCommand.setDefaultCommand();
Player player = null ;
try {
player = Manager.createPlayer("capture://video");
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
Control vControl = (Control) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, Control.class.getName());
vControl.setParent(shell);
Rectangle r = vControl.getBounds();
videoControl.setDisplaySize(r.width, r.height);
videoControl.setVisible(true);
player.start();
} catch (IOException e) {
} catch (MediaException e) {
}
// Execute the eSWT event loop.
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
if (player != null){
player.deallocate();
player.close();
player = null;
}
// Clean up and destroy the MIDlet
destroyApp(true);
notifyDestroyed();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
if(e.widget == takePictureCommand){
takePicture();
}else{
// Exit command selected, exit the event loop.
shell.dispose();
}
}
private void takePicture(){
byte[] snapshot;
try {
snapshot = videoControl.getSnapshot(null);
if (snapshot == null || snapshot.length == 0)return;
String filename = "file:///c:/data/images/I"+System.currentTimeMillis()+".png";
FileConnection fileConnection = (FileConnection) Connector.open(filename,Connector.READ_WRITE);
fileConnection.create();
OutputStream stream = fileConnection.openOutputStream();
stream.write(snapshot);
stream.flush();
stream.close();
fileConnection.close();
} catch (MediaException e) {
} catch (IOException e) {
}
}
}
A PNG image containing the picture is saved under C:\Data\Images.
No related wiki articles found