×
Namespaces

Variants
Actions

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

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

ESWT is a deprecated UI framework. It is not present on Series 40 and on Symbian there are better maintained UI frameworks for this functionality (e.g. LWUIT).

Article Metadata
Compatibility
Platform(s):
Symbian
MIDP 2.0
Article
Created: gorkem.ercan (31 Mar 2009)
Last edited: hamishwillee (31 Jul 2013)


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);
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

This page was last modified on 31 July 2013, at 09:09.
58 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×