📄 snappermidlet.java
字号:
/*
* 抓图
*/
package snapper;
import java.io.IOException;
import java.io.InputStream;
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 {
private Display display;
private Form mainForm;
private Command exitCommand, cameraCommand;
private Command backCommand, captureCommand;
private Player player;
private VideoControl videoControl;
public SnapperMidlet() {
exitCommand = new Command("退出", Command.EXIT, 0);
cameraCommand = new Command("播放", Command.SCREEN, 0);
backCommand = new Command("返回", Command.BACK, 0);
captureCommand = new Command("抓图", Command.SCREEN, 0);
mainForm = new Form("抓图");
mainForm.addCommand(exitCommand);
//判断设备是否支持抓图
String supports = System.getProperty("video.snapshot.encodings");
if (supports != null && supports.length() > 0) {
mainForm.append("设备允许播放视频的时候抓图");
mainForm.addCommand(cameraCommand);
} else
mainForm.append("设备不支持抓图 ");
mainForm.setCommandListener(this);
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
if (c == cameraCommand) {
Thread t = new Thread() {
public void run() {
showCamera();
}
};
t.start();
}
if (c == backCommand)
display.setCurrent(mainForm);
if (c == captureCommand) {
Thread t = new Thread() {
public void run() {
capture();
}
};
t.start();
}
}
private void showCamera() {
Form f = new Form("视频演示程序");
f.addCommand(backCommand);
f.addCommand(captureCommand);
f.setCommandListener(this);
try {
InputStream ins = getClass().getResourceAsStream("/test-mpeg.mpg");
player = Manager.createPlayer(ins, "video/mpeg");
player.realize();
Item videoItem = null;
if ((videoControl = (VideoControl) player
.getControl("VideoControl")) != null)
videoItem = (Item) videoControl.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE, null);
display.setCurrent(f);
player.start();
f.append(videoItem);
} catch (IOException ioe) {
handleException(ioe);
} catch (MediaException me) {
handleException(me);
}
}
public void capture() {
try {
// 获得Image对象,参数尾null表示格式为png类型
byte[] raw = videoControl.getSnapshot(null);
Image image = Image.createImage(raw, 0, raw.length);
Image thumb = createThumbnail(image);
//放置到mainForm中
if (mainForm.size() > 0 && mainForm.get(0) instanceof StringItem)
mainForm.delete(0);
mainForm.append(thumb);
//返回mainForm
display.setCurrent(mainForm);
//关闭Player.
player.close();
player = null;
videoControl = null;
} catch (MediaException me) {
handleException(me);
}
}
private void handleException(Exception e) {
Alert a = new Alert("Exception", e.toString(), null, null);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a, mainForm);
}
//绘制缩小的图片
private Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
//设置缩小图的宽度
int thumbWidth = 64;
int thumbHeight = -1;
//如果为-1则表示长宽按照比例缩小,计算缩小图的高度
if (thumbHeight == -1)
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
//创建一个空的离屏Image对象
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 + -