📄 capturer.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 + -