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

📄 capturer.java

📁 Programming java 2 micro edition on symbian os 一书中作者写的使用了MMAPI的小拼图游戏
💻 JAVA
字号:
/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.html
 */

package picturepuzzle;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.IOException;

/**
 * Creates the VideoPlayer used to capture a photo.
 */
public class Capturer {
    
    private GameMIDlet midlet;
    private CaptureCanvas canvas;
    private Player player = null;
    private VideoControl videoControl = null;
    private boolean active = false;
    
    /**
     * Performs initialization and creates the VideoPlayer instance.
     * @param midlet Facilitates call backs.
     * @param canvas The Canvas onto which the output of the Video Player is rendered.
     * @throws ApplicationException If unable to create the Player.
     */
    public Capturer(GameMIDlet midlet, CaptureCanvas canvas) throws ApplicationException {
        this.midlet = midlet;
        this.canvas = canvas;
        createPlayer();
    }
    
    /**
     * Creates a VideoPlayer and gets an associated VideoControl.
     * @throws ApplicationException If unable to create VideoPlayer.
     */
    public void createPlayer() throws ApplicationException {
        
        try {
            player = Manager.createPlayer("capture://video");
            player.realize();
            // Sets VideoControl to the current display.
            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);
        }
    }
    
    /**
     * Captures the current image.
     * @throws ApplicationException If unable to capture the image.
     * @return The image data.
     */
    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;
    }
    
    /**
     * Closes the VideoPlayer freeing up resorces.
     */
    public void discardPlayer() {
        if(player != null) {
            player.close();
            player = null;
        }
        videoControl = null;
    }
    
    /**
     * Starts VideoPlayer and makes output visible.
     * @throws ApplicationException If unable to start Player.
     */
    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;
        }
        
    }
    
    /**
     * Stops VideoPlayer.
     * @throws ApplicationException If unable to stop Player.
     */
    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 + -