📄 snappermidlet.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.midlet.MIDlet;
public class SnapperMIDlet extends MIDlet implements CommandListener,Runnable {
private Display mDisplay;
private Form mMainForm;
private Command mExitCommand, mCameraCommand;
private Command mBackCommand, mCaptureCommand;
//创建播放器对象
private Player mPlayer;
//创建视频控制器接口
private VideoControl mVideoControl;
public SnapperMIDlet() {
mExitCommand = new Command("Exit", Command.EXIT, 0);
mCameraCommand = new Command("Camera", Command.SCREEN, 0);
mBackCommand = new Command("Back", Command.BACK, 0);
mCaptureCommand = new Command("Capture", Command.SCREEN, 0);
mMainForm = new Form("Snapper");
mMainForm.addCommand(mExitCommand);
String supports = System.getProperty("video.snapshot.encodings");
if (supports != null && supports.length() > 0) {
mMainForm.append("Ready to take pictures.");
mMainForm.addCommand(mCameraCommand);
}
else
mMainForm.append("Snapper cannot use this " + "device to take pictures.");
mMainForm.setCommandListener(this);
}
public void startApp() {
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mMainForm);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
else if (c == mCameraCommand)
//启动摄像头
showCamera();
else if (c == mBackCommand)
//显示主屏幕届面
mDisplay.setCurrent(mMainForm);
else if (c == mCaptureCommand) {
//创建摄像头捕获图像的线程
new Thread(this).start();
}
}
private void showCamera() {
try {
//创建播放器对象
mPlayer = Manager.createPlayer("capture://video");
//使摄像头处于就绪状态
mPlayer.realize();
//创建视频控制器接口
mVideoControl = (VideoControl)mPlayer.getControl("VideoControl");
//新建Canvas画布对象
Canvas canvas = new CameraCanvas(this, mVideoControl);
canvas.addCommand(mBackCommand);
canvas.addCommand(mCaptureCommand);
canvas.setCommandListener(this);
mDisplay.setCurrent(canvas);
//启动摄像头
mPlayer.start();
}
catch (IOException ioe) { handleException(ioe); }
catch (MediaException me) { handleException(me); }
}
//捕获图像的线程中的方法定义
public void run(){
capture();
}
public void capture() {
try {
// 得到摄像头拍照的图像的字节数组
byte[] raw = mVideoControl.getSnapshot(null);
//将摄像头获取的图像二进制字节数组转换成Image对象
Image image = Image.createImage(raw, 0, raw.length);
//调用图像转换方法
Image thumb = createThumbnail(image);
//将图像显示在屏幕上
if (mMainForm.size() > 0 && mMainForm.get(0) instanceof StringItem)
mMainForm.delete(0);
mMainForm.append(thumb);
mDisplay.setCurrent(mMainForm);
// 关闭播放器
mPlayer.close();
mPlayer = null;
mVideoControl = null;
}
catch (MediaException me) { handleException(me); }
}
private void handleException(Exception e) {
Alert a = new Alert("Exception", e.toString(), null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mMainForm);
}
//图像转换方法
private Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = 64;
int thumbHeight = -1;
if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
}
}
Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -