Hi,
I Need some help, I don't know what i wrong, but I know my application crash because of takePicture().
I have eSWT UI thread, in my application I also have a Controller class. The Camera UI is open like this:
The snapperUI look like this:Code:testDryckListUI.setVisible(false); snapperUI.initialize(); snapperUI.open();
Code:package se.ojn.xxx.ui; import java.io.IOException; import javax.microedition.lcdui.Image; import javax.microedition.media.Manager; import javax.microedition.media.MediaException; import javax.microedition.media.Player; import javax.microedition.media.control.GUIControl; import javax.microedition.media.control.VideoControl; import org.eclipse.ercp.swt.mobile.Command; import org.eclipse.ercp.swt.mobile.TimedMessageBox; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Item; import org.eclipse.swt.widgets.Shell; import se.ojn.provning.util.Controller; import se.ojn.provning.util.EventID; import se.ojn.provning.util.UIConstant; public class SnapperUI implements SelectionListener { private static SnapperUI instance; static { instance = new SnapperUI(); } private Shell snapperShell; private Composite snapperComposite; private VideoControl videoControl; private Player mPlayer; private VideoControl mVideoControl; private Image image; private Command takePictureCommand; private Command exitCommand; private Item item; public SnapperUI() { //initialize(); } public static SnapperUI getInstance() { return instance; } public void initialize(){ snapperShell = new Shell(SWT.MAX); snapperShell.setText(UIConstant.INFO); snapperShell.setLayout(new FillLayout(SWT.VERTICAL)); snapperComposite = new Composite(snapperShell, SWT.NONE); snapperComposite.setLayout(new FormLayout()); snapperShell.layout(); exitCommand = new Command(snapperShell, Command.EXIT, 0); exitCommand.setText("Exit"); exitCommand.addSelectionListener(this); takePictureCommand = new Command(snapperShell, Command.GENERAL, 10); takePictureCommand.setText("Snap!"); takePictureCommand.addSelectionListener(this); takePictureCommand.setDefaultCommand(); startCamera(); snapperComposite.layout(); } public void reset() { } /** * Release the acquired system resources to the operating system. */ public void close() { if ((null != snapperShell) && (!snapperShell.isDisposed())) { Display display = snapperShell.getDisplay(); snapperShell.dispose(); display.dispose(); } } public final void open(){ //infoShell.setSize(240, 350); snapperShell.open(); snapperShell.setActive(); Display display = snapperShell.getDisplay(); while (!snapperShell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } public void widgetSelected(SelectionEvent arg0) { if(arg0.widget == takePictureCommand){ takePicture(); if(arg0.widget.equals(exitCommand)){ Controller.handleEvent(EventID.EXIT,null); } } } public void widgetDefaultSelected(SelectionEvent arg0) { throw new UnsupportedOperationException("Not supported yet."); } /** * Let the shell disappear from the screen or show on the screen. * */ public void setVisible(boolean visible){ snapperShell.setVisible(visible); } private void takePicture(){ byte[] snapshot; try { // Get the image. //byte[] raw = mVideoControl.getSnapshot(null); //byte[] raw = mVideoControl.getSnapshot("width=80&height=60"); //byte[] raw = mVideoControl.getSnapshot("width=160&height=120"); snapshot = mVideoControl.getSnapshot("width=240&height=180"); image = Image.createImage(snapshot, 0, snapshot.length); // Shut down the player. mPlayer.close(); mPlayer = null; mVideoControl = null; } catch (MediaException e) { showMessage(e.toString()); } } public void startCamera() { mPlayer = null; try { mPlayer = Manager.createPlayer("capture://video"); //mPlayer = Manager.createPlayer("capture://image"); mPlayer.realize(); mPlayer.prefetch(); mVideoControl = (VideoControl) mPlayer.getControl("VideoControl"); /** No support for flash FlashControl flashCntrl = (FlashControl) mPlayer.getControl("javax.microedition.amms.control.camera.FlashControl"); flashCntrl.setMode(FlashControl.AUTO); */ Control vControl = (Control) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, Control.class.getName()); vControl.setParent(snapperShell); Rectangle r = vControl.getBounds(); videoControl.setDisplaySize(r.width, r.height); videoControl.setVisible(true); /** item = (Item) mVideoControl.initDisplayMode( GUIControl.USE_GUI_PRIMITIVE, null); */ mPlayer.start(); } catch (IOException ioe) { System.out.println("Error: " + ioe); showMessage(ioe.toString()); //errorMessage = "Error: " + ioe; //throw new IOException(errorMessage); } catch (MediaException me) { System.out.println("Error: " + me); showMessage(me.toString()); //errorMessage = "Error: " + me; //throw new MediaException(errorMessage); } catch (Exception me) { System.out.println("Error: " + me); showMessage(me.toString()); //errorMessage = "Error: " + me; //throw new Exception(errorMessage); } } private void showMessage(String message){ TimedMessageBox box = new TimedMessageBox(snapperShell, SWT.ICON_ERROR); box.setMessage(message); box.open(); } }

Reply With Quote

