浏览 2605 次
锁定老帖子 主题:J2ME测试小工具(camera)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-28
最后修改:2010-01-26
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.media.Manager; import javax.microedition.media.Player; import javax.microedition.media.control.VideoControl; import javax.microedition.midlet.MIDlet; public class CameraMID extends MIDlet implements CommandListener ...{ private MyCanvas canvas; private Player myPlayer; private VideoControl videoControl = null; // private FocusControl focusControl = null; // private ZoomControl zoomControl = null; // private ExposureControl exposureControl = null; private Vector er = new Vector(); private Form form; private Command cmdPlay; private Command cmdError; private Command cmdQuit; public static CameraMID mid; public byte[] data; public Image img; public CameraMID() ...{ form = new Form("test"); cmdPlay = new Command("play", Command.OK, 0); cmdError = new Command("show error", Command.ITEM, 1); cmdQuit = new Command("quit", Command.EXIT, 2); form.addCommand(cmdPlay); form.addCommand(cmdError); form.addCommand(cmdQuit); form.setCommandListener(this); Display.getDisplay(this).setCurrent(form); canvas = new MyCanvas(); mid = this; try ...{ String str = System.getProperty("video.snapshot.encodings"); if (null == str) ...{ str = "SSN"; } else ...{ str = "SSY"; } er.addElement(str); } catch (Exception e) ...{ er.addElement(e.toString()); } } public void commandAction(Command c, Displayable d) ...{ if (c.equals(cmdPlay)) ...{ Display.getDisplay(this).setCurrent(canvas); initPlayer(); } else if (c.equals(cmdError)) ...{ form.deleteAll(); for (int i = 0; i < er.size(); i++) ...{ form.append((String) er.elementAt(i)); } } else if (c.equals(cmdQuit)) ...{ notifyDestroyed(); } } private void initPlayer() ...{ new Thread() ...{ public void run() ...{ freePlayer(); try ...{ myPlayer = Manager.createPlayer("capture://video"); myPlayer.realize(); videoControl = (VideoControl) myPlayer .getControl("VideoControl"); videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas); myPlayer.start(); videoControl.setVisible(true); videoControl.setDisplayLocation(20, 18); videoControl.setDisplaySize(200, 230); } catch (Exception e) ...{ er.addElement(e.toString()); } // try { // focusControl = (FocusControl) myPlayer // .getControl("FocusControl"); // if (focusControl.isMacroSupported()) { // focusControl.setMacro(true); // } // if (focusControl.isAutoFocusSupported()) { // autoFocusFlg = true; // } else { // autoFocusFlg = false; // } // } catch (Exception e) { // er.addElement("focus " + e.toString()); // } // // try { // zoomControl = (ZoomControl) myPlayer // .getControl("ZoomControl"); // // zoomControl.setDigitalZoom(100); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // zoomControl.setDigitalZoom(ZoomControl.NEXT); // } catch (Exception e) { // er.addElement("zoom " + e.toString()); // } // // try { // exposureControl = (ExposureControl) myPlayer // .getControl("ExposureControl"); // exposureControl.setISO(0); // } catch (Exception e) { // er.addElement("exposure " + e.toString()); // } } }.start(); } private void freePlayer() ...{ try ...{ myPlayer.stop(); myPlayer.deallocate(); } catch (Exception e) ...{ } try ...{ myPlayer.close(); } catch (Exception e) ...{ } } private void snapshot() ...{ new Thread() ...{ public void run() ...{ try ...{ er.addElement("getSnapshot "); data = videoControl.getSnapshot("encoding=jpeg&width=640&height=480"); er.addElement("getSnapshot end "); img = Image.createImage(data, 0, data.length); form.deleteAll(); form.append(img); Display.getDisplay(mid).setCurrent(form); er.addElement("end "); String time = Long.toString(System.currentTimeMillis()); String path = "file:///C:/pictures/" + time + ".png"; DeviceWirteFile(data, path); } catch (Exception e) ...{ er.addElement("snapshot " + e.toString()); } } }.start(); } private void zoomIn() ...{ // if (zoomControl != null) { // zoomControl.setDigitalZoom(ZoomControl.NEXT); // } } private void zoomOut() ...{ // if (zoomControl != null) { // zoomControl.setDigitalZoom(ZoomControl.PREVIOUS); // } } public void autoFocus() ...{ if (autoFocusFlg) ...{ // try { // focusControl.setFocus(-1000); // } catch (Exception e) { // er.addElement("autofocus " + e.toString()); // } } } private boolean autoFocusFlg = false; private class MyCanvas extends Canvas implements CommandListener ...{ private Command cmdOK; private Command cmdBack; private Command cmdFocus; public MyCanvas() ...{ cmdOK = new Command("snapshot", Command.OK, 0); cmdBack = new Command("back", Command.BACK, 1); cmdFocus = new Command("auto focus", Command.ITEM, 2); addCommand(cmdOK); addCommand(cmdBack); addCommand(cmdFocus); setCommandListener(this); } public void paint(Graphics g) ...{ } public void commandAction(Command c, Displayable d) ...{ if (c.equals(cmdOK)) ...{ autoFocus(); snapshot(); } else if (c.equals(cmdBack)) ...{ freePlayer(); Display.getDisplay(mid).setCurrent(form); } else if (c.equals(cmdFocus)) ...{ autoFocus(); } } public void keyRepeated(int key) ...{ int nk = getGameAction(key); switch (nk) ...{ case UP: case LEFT: zoomIn(); break; case DOWN: case RIGHT: zoomOut(); break; } } } private void DeviceWirteFile(byte[] data, String fileName) throws Exception ...{ // FileConnection conn = (FileConnection) Connector.open(fileName, // Connector.READ_WRITE); // if (conn.exists()) { // conn.delete(); // conn.create(); // } else { // conn.create(); // } // OutputStream os = conn.openOutputStream(); // // os.write(data); // os.flush(); // os.close(); // conn.close(); } protected void destroyApp(boolean arg0) ...{ } protected void pauseApp() ...{ } protected void startApp() ...{ } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |