You Are Here:

Community: Wiki

This page was last modified on 5 June 2009, at 16:33.

Using eSWT and MMAPI together to take a picture in Java ME

From Forum Nokia Wiki


Overview

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.

Source code

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) {
 
}
}
 
 
}

Postconditions

A PNG image containing the picture is saved under C:\Data\Images.

See also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fUsingE5feSWTE5fandE5fMMAPIE5ftogetherE5ftoE5ftakeE5faE5fpictureE5finE5fJavaE5fME45X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZeswtQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ