⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powercamera.java

📁 手机游戏对战平台。自己做的一个东东。 支持多个手机联网打游戏;采用socket链接通信。 平台:J2ME&JAVA
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package mmapi;import java.io.IOException;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.media.Manager;import javax.microedition.media.MediaException;import javax.microedition.media.Player;import javax.microedition.media.control.VideoControl;/** * @author XJX */public class PowerCamera extends MIDlet implements CommandListener,Runnable{    private boolean midletPaused = false;        private Display display = null;    private Displayable resume = null;    private List main = null;    private PowerModel pm = null;    private Player player = null;    private VideoControl vc = null;    private Command exitCommand = new Command("退出",Command.EXIT, 1);    private Command captureCommand = new Command("拍照",Command.OK, 1);    private Command backCommand = new Command("返回",Command.BACK, 1);    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|//</editor-fold>//GEN-END:|fields|0|    /**     * The VisualMIDlet constructor.     */    public PowerCamera() {    }//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|//</editor-fold>//GEN-END:|methods|0|//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize/** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */private void initialize () {//GEN-END:|0-initialize|0|0-preInitialize        // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize        // write post-initialize user code here}//GEN-BEGIN:|0-initialize|2|//</editor-fold>//GEN-END:|0-initialize|2|//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction/** * Performs an action assigned to the Mobile Device - MIDlet Started point. */public void startMIDlet () {//GEN-END:|3-startMIDlet|0|3-preAction        // write pre-action user code here//GEN-LINE:|3-startMIDlet|1|3-postAction        // write post-action user code here}//GEN-BEGIN:|3-startMIDlet|2|//</editor-fold>//GEN-END:|3-startMIDlet|2|//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction/** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */public void resumeMIDlet () {//GEN-END:|4-resumeMIDlet|0|4-preAction        // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction        // write post-action user code here}//GEN-BEGIN:|4-resumeMIDlet|2|//</editor-fold>//GEN-END:|4-resumeMIDlet|2|//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch/** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */public void switchDisplayable (Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch        // write pre-switch user code hereDisplay display = getDisplay ();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitchif (alert == null) {display.setCurrent (nextDisplayable);} else {display.setCurrent (alert, nextDisplayable);}//GEN-END:|5-switchDisplayable|1|5-postSwitch        // write post-switch user code here}//GEN-BEGIN:|5-switchDisplayable|2|//</editor-fold>//GEN-END:|5-switchDisplayable|2|    /**     * Returns a display instance.     * @return the display instance.     */    public Display getDisplay () {        return Display.getDisplay(this);    }    /**     * Exits MIDlet.     */    public void exitMIDlet() {        switchDisplayable (null, null);        destroyApp(true);        notifyDestroyed();    }        public void capture(){        try{            //捕获数据 可以通过参数定制编码类型以及图片的长度和高度            byte[] imgData = vc.getSnapshot("encoding=jpeg&width=160&height=120");            ViewImageUI vimage = new ViewImageUI(this, imgData);            //显示图片,并让用户输入标题            display.setCurrent(vimage);            player.close();            player = null;            vc = null;        }catch(MediaException ex){            showInfo(ex.toString(),AlertType.ERROR);        }    }        //把播放器资源释放,系统可能需要使用    private void releasePlayer(){        if(player != null){            player.close();            player = null;        }if(vc != null){            vc = null;        }    }    public void back(){        display.setCurrent(main);    }     private void showCamera(){        try{            //创建播放器            if(player == null){                player = Manager.createPlayer("capture://video");            }            player.realize();            //获得VideoControl            vc = (VideoControl)player.getControl("VideoControl");            //将捕获的数据绘制到Canvas上            CameraCanvas cc = new CameraCanvas(this, vc);            cc.addCommand(captureCommand);            cc.addCommand(backCommand);            cc.setCommandListener(this);            display.setCurrent(cc);            player.start();        }catch(IOException ex){            ex.printStackTrace();            player =null;            vc = null;        } catch(MediaException ex){            ex.printStackTrace();            player =null;            vc = null;        }    }       public void savePicture(Picture pic){        pm.savePicture(pic);        showInfo("成功保存图片到我的相册", AlertType.CONFIRMATION);    } //错误提示    public void showInfo(String message,AlertType type){        Alert alert = new Alert("系统提示");        alert.setString(message);        alert.setTimeout(2000);        alert.setType(type);        display.setCurrent(alert, main);    }        public void setCurent(Displayable current){        display.setCurrent(current);    }    /**     * Called when MIDlet is started.     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.     */    public void startApp() {        if (midletPaused) {            resumeMIDlet ();        } else {            initialize ();            startMIDlet ();        }                midletPaused = false;         if(display == null){            display = Display.getDisplay(this);            pm = new PowerModel(this);            main = new List("主菜单", List.IMPLICIT, new String[]{"手机拍照","我的相册"}, null);            main.addCommand(exitCommand);            main.setCommandListener(this);            resume = main;        }        display.setCurrent(resume);    }    /**     * Called when MIDlet is paused.     */    public void pauseApp() {        midletPaused = true;        resume = display.getCurrent();    }    /**     * Called to signal the MIDlet to terminate.     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.     */    public void destroyApp(boolean unconditional) {         //退出程序,释放资源        pm.release();        releasePlayer();     }    public void commandAction(Command cmd,Displayable displayable) {           if(cmd == exitCommand){            destroyApp(false);            notifyDestroyed();        }else if(cmd == List.SELECT_COMMAND){            String label = main.getString(main.getSelectedIndex());            if(label.equals("手机拍照")){                //判断是否支持MMAPI                String support = PowerModel.isVideoCapture();                if(support.equals("true")){                    showCamera();                }else{                    showInfo("您的手机不支持拍照功能",AlertType.WARNING);                }            }else if(label.equals("我的相册")){                //一次读取Picture[]数组,这里对内存消耗很大                Picture[] pic = pm.getAllPicture();                if(pic == null){                    showInfo("相册为空", AlertType.INFO);                }else{                    ImageList list = new ImageList(this);                    list.initTitle(pic);                    display.setCurrent(list);                }            }        }else if(cmd == captureCommand){            Thread t=new Thread(this);            t.start();        }else if(cmd == backCommand){            releasePlayer();            back();        }    }    public void run() {       capture();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -