课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——捕获视频


  共有13559人关注过本帖树形打印复制链接

主题:程序代码——捕获视频

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——捕获视频  发帖心情 Post By:2008/11/15 15:20:43 [只看该作者]

import java.io.InputStream;

import javax.microedition.lcdui.Alert;
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;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class Exec extends MIDlet implements CommandListener {
        Display display = null;
        DrawPanel dp = new DrawPanel(this);
        Form form = new Form("浏览图片");
        Command start = new Command("播放视频", Command.OK, 1);
        Command browse = new Command("浏览图片", Command.BACK, 1);
        Command back = new Command("后退", Command.BACK, 1);
        int board = 10;
        VideoControl vc;
        Player player;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                form.addCommand(back);
                form.setCommandListener(this);
                dp.addCommand(start);
                dp.addCommand(browse);
                dp.setCommandListener(this);
                display.setCurrent(dp);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command com, Displayable arg1) {
                if (com == start) {
                        try {
                                InputStream is = getClass().getResourceAsStream(
                                                "/videos/phantom.mpg");
                                player = Manager.createPlayer(is, "video/mpeg");
                                player.realize();
                                vc = (VideoControl) player.getControl("VideoControl");
                                if (vc != null) {
                                        vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, dp);
                                        vc.setDisplayLocation(board, board);
                                        vc.setDisplaySize(dp.getWidth() - board * 2, dp.getHeight()
                                                        - board * 2);
                                        vc.setVisible(true);
                                }
                                player.start();

                                // 显示摄像头的视频信息
                                // Player player = Manager.createPlayer("capture://video");
                                // player.realize();
                                // VideoControl vc = (VideoControl)
                                // player.getControl("VideoControl");
                                // if (vc != null) {
                                // vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, dp);
                                // vc.setDisplayLocation(board, board);
                                // vc.setDisplaySize(dp.getWidth() - board * 2, dp.getHeight()
                                // - board * 2);
                                // vc.setVisible(true);
                                // }
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == browse) {
                        try {
                                player.stop();
                                form.deleteAll();
                                RecordStore rs = RecordStore.openRecordStore("Images", true);
                                RecordEnumeration re = rs.enumerateRecords(null, null, false);
                                while (re.hasNextElement()) {
                                        byte[] imageByte = re.nextRecord();
                                        Image image = Image.createImage(imageByte, 0,
                                                        imageByte.length);
                                        form.append(image);
                                }
                                display.setCurrent(form);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == back) {
                        display.setCurrent(dp);
                        try {
                                player.start();
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }

        public void capture() {
                String supports = System.getProperty("supports.video.capture");
                if (supports == null) {
                        Alert alert = new Alert("警告");
                        alert.setTimeout(1000);
                        alert.setString("不支持视频捕获!");
                        display.setCurrent(alert);
                        return;
                }
                Thread thread = new Thread(new NewThread());
                thread.start();
        }

        class NewThread implements Runnable {
                public void run() {
                        try {
                                byte[] imageByte = vc.getSnapshot("encoding=jpeg");
                                storeImage(imageByte);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }

        public void storeImage(byte[] imageByte) {
                try {
                        RecordStore rs = RecordStore.openRecordStore("Images", true);
                        rs.addRecord(imageByte, 0, imageByte.length);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }
}

class DrawPanel extends Canvas {
        Exec exec;

        public DrawPanel(Exec exec) {
                this.exec = exec;
        }

        public void paint(Graphics g) {
                g.setColor(0, 0, 255);
                g.fillRect(0, 0, getWidth(), getHeight());
        }

        protected void keyPressed(int keyCode) {
                int action = getGameAction(keyCode);
                if (action == FIRE)
                        exec.capture();
        }
}

[此贴子已经被作者于2010-12-12 18:46:15编辑过]

 回到顶部