📄 powercamera.java
字号:
package com.j2medev.ch8.mmapi;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
public class PowerCamera extends MIDlet implements CommandListener{
private Display display = null;
private Displayable resume = null;
private List main = null;
private PowerModel pm = null;
private Player player = null;
private VideoControl vc = null;
private Command exitCommand = new Command("退出",Command.EXIT, 1);
private Command captureCommand = new Command("拍照",Command.OK, 1);
private Command backCommand = new Command("返回",Command.BACK, 1);
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
pm = new PowerModel(this);
main = new List("主菜单", List.IMPLICIT, new String[]{"手机拍照","我的相册"}, null);
main.addCommand(exitCommand);
main.setCommandListener(this);
resume = main;
}
display.setCurrent(resume);
}
public void setCurent(Displayable current){
display.setCurrent(current);
}
public void pauseApp() {
resume = display.getCurrent();
}
public void destroyApp(boolean unconditional) {
//退出程序,释放资源
pm.release();
releasePlayer();
}
private void showCamera(){
try{
//创建播放器
if(player == null){
player = Manager.createPlayer("capture://video");
}
player.realize();
//获得VideoControl
vc = (VideoControl)player.getControl("VideoControl");
//将捕获的数据绘制到Canvas上
CameraCanvas cc = new CameraCanvas(this, vc);
cc.addCommand(captureCommand);
cc.addCommand(backCommand);
cc.setCommandListener(this);
display.setCurrent(cc);
player.start();
}catch(IOException ex){
ex.printStackTrace();
player =null;
vc = null;
} catch(MediaException ex){
ex.printStackTrace();
player =null;
vc = null;
}
}
public void capture(){
try{
//捕获数据 可以通过参数定制编码类型以及图片的长度和高度
byte[] imgData = vc.getSnapshot("encoding=jpeg&width=160&height=120");
ViewImageUI vimage = new ViewImageUI(this, imgData);
//显示图片,并让用户输入标题
display.setCurrent(vimage);
player.close();
player = null;
vc = null;
}catch(MediaException ex){
showInfo(ex.toString(),AlertType.ERROR);
}
}
public void back(){
display.setCurrent(main);
}
public void savePicture(Picture pic){
pm.savePicture(pic);
showInfo("成功保存图片到我的相册", AlertType.CONFIRMATION);
}
//错误提示
public void showInfo(String message,AlertType type){
Alert alert = new Alert("系统提示");
alert.setString(message);
alert.setTimeout(2000);
alert.setType(type);
display.setCurrent(alert, main);
}
//处理和用户交互的逻辑
public void commandAction(Command cmd,Displayable displayable){
if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}else if(cmd == List.SELECT_COMMAND){
String label = main.getString(main.getSelectedIndex());
if(label.equals("手机拍照")){
//判断是否支持MMAPI
String support = PowerModel.isVideoCapture();
if(support.equals("true")){
showCamera();
}else{
showInfo("您的手机不支持拍照功能",AlertType.WARNING);
}
}else if(label.equals("我的相册")){
//一次读取Picture[]数组,这里对内存消耗很大
Picture[] pic = pm.getAllPicture();
if(pic == null){
showInfo("相册为空", AlertType.INFO);
}else{
ImageList list = new ImageList(this);
list.initTitle(pic);
display.setCurrent(list);
}
}
}else if(cmd == captureCommand){
capture();
}else if(cmd == backCommand){
releasePlayer();
back();
}
}
//把播放器资源释放,系统可能需要使用
private void releasePlayer(){
if(player != null){
player.close();
player = null;
}if(vc != null){
vc = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -