捕获视频截图

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leeshuqing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值