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

📄 capturer.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
package picturepuzzle;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.IOException;

/**
 * 创建视频播放器用来捕获图像的类
 */
public class Capturer {
    
    private GameMIDlet midlet;
    private CaptureCanvas canvas;
    private Player player = null;
    private VideoControl videoControl = null;
    private boolean active = false;
    
    public Capturer(GameMIDlet midlet, CaptureCanvas canvas) throws ApplicationException {
        this.midlet = midlet;
        this.canvas = canvas;
        createPlayer();
    }
    
    public void createPlayer() throws ApplicationException {
        
        try {
			//创建视频播放器对象
            player = Manager.createPlayer("capture://video");
            player.realize();
            // 获得视频控制器借口
            videoControl = (VideoControl)(player.getControl("VideoControl"));
            if (videoControl == null) {
                discardPlayer();
            } else {
                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
                int cWidth = canvas.getWidth();
                int cHeight = canvas.getHeight();
                int dWidth = 160;
                int dHeight = 120;
                videoControl.setDisplaySize(dWidth, dHeight);
                videoControl.setDisplayLocation((cWidth - dWidth)/2, (cHeight - dHeight)/2);
            }
        } catch (IOException ioe) {
            discardPlayer();
            throw new ApplicationException("Unable to access camera", ioe);
        } catch (MediaException me) {
            discardPlayer();
            throw new ApplicationException("Unable to access camera", me);
        } catch(SecurityException se) {
            discardPlayer();
            throw new ApplicationException("Unable to access camera", se);
        }
    }
    
    /**
     *使用摄像头捕获图片
     */
    public byte[] takeSnapshot() throws ApplicationException {
        byte[] pngImage = null;
        if (videoControl == null) {
            throw new ApplicationException("Unable to capture photo: VideoControl null");
        }
        try {
			//捕获图像
            pngImage = videoControl.getSnapshot(null);
        }catch(MediaException me) {
            throw new ApplicationException("Unable to capture photo", me);
        }
        
        return pngImage;
    }
    
    /**
     * 关闭视频控制器
     */
    public void discardPlayer() {
        if(player != null) {
            player.close();
            player = null;
        }
        videoControl = null;
    }
    
    /**
     * 启动视频控制器
     */
    public void startPlayer() throws ApplicationException {
        if ((player != null) && !active) {
            try {
                player.start();
                videoControl.setVisible(true);
            } catch(MediaException me) {
                throw new ApplicationException("Unable to start video player", me);
            } catch(SecurityException se) {
                throw new ApplicationException("Unable to start video player", se);
            }
            active = true;
        }
        
    }
    
    /**
     * 暂停视频控制器
     */
    public void stopPlayer() throws ApplicationException {
        if ((player != null) && active) {
            try {
                videoControl.setVisible(false);
                player.stop();
            } catch (MediaException me) {
                throw new ApplicationException("Unable to stop video player", me);
            }
            active = false;
        }
    }
    
}


⌨️ 快捷键说明

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