package com.zeph.j2me;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class CameraControlMIDlet extends MIDlet implements CommandListener {
private Display display;
private CameraCanvas cameraCanvas;
private Form picForm;
private Command captureCommand;
private Command exitCommand;
private Command backCommand;
private Player player;
private VideoControl videoControl;
public CameraControlMIDlet() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
try {
player = Manager.createPlayer("capture://video");
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
cameraCanvas = new CameraCanvas(player, videoControl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
picForm = new Form("图像显示窗口");
captureCommand = new Command("拍照", Command.OK, 0);
exitCommand = new Command("退出", Command.EXIT, 0);
backCommand = new Command("返回摄像头", Command.OK, 0);
cameraCanvas.addCommand(captureCommand);
cameraCanvas.addCommand(exitCommand);
picForm.addCommand(backCommand);
picForm.setCommandListener(this);
cameraCanvas.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(cameraCanvas);
}
public void commandAction(Command command, Displayable displayable) {
// TODO Auto-generated method stub
if (command == captureCommand) {
new Snapshot().start();//拍照时需要使用线程启动
} else if (command == exitCommand) {
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
} else if (command == backCommand) {
display.setCurrent(cameraCanvas);
}
}
class Snapshot extends Thread {//使用内部类可以很方便的使用VideoControl和Player
public Image getSnapshot() {
Image image = null;
if (player != null) {
byte[] imageData;
try {
imageData = videoControl.getSnapshot(null);
image = Image.createImage(imageData, 0, imageData.length);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return image;
}
public void run() {
Image image = getSnapshot();
picForm.append(image);
display.setCurrent(picForm);
}
}
}
package com.zeph.j2me;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
public class CameraCanvas extends Canvas {
public CameraCanvas(Player player, VideoControl videoControl) {
try {
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
videoControl.setDisplayLocation(0, 0);
videoControl.setDisplaySize(getWidth(), getHeight());
player.start();
videoControl.setVisible(true);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
}
}
在测试S403rd的系统时,需要把
player = Manager.createPlayer("capture://video");
更改为
player = Manager.createPlayer("capture://image");