攝像頭操作往往是比較煩人的內容,往往不能用一個統一的代碼來使用所有的手機。
這個測試目前僅包含了基本的功能(預覽和快照)。
但是,我注釋掉的代碼中,還包含了其它一些JSR234中關於攝像頭操作的高級功能,比如自動聚焦,縮放,自動感光等等。有興趣的朋友,可以恢復那些代碼並結合JSR234的文檔,仔細研究研究。此外,我還寫了一個保存照片的DeviceWirteFile函數,可以用來檢測攝像頭的真實拍攝效果(我們的軟件需要對獲取的圖像進行解碼,所以有這樣的需求)。
import Java.util.Vector;
import Javax.microedition.lcdui.Canvas;
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.Graphics;
import Javax.microedition.lcdui.Image;
import Javax.microedition.media.Manager;
import Javax.microedition.media.Player;
import Javax.microedition.media.control.VideoControl;
import Javax.microedition.midlet.MIDlet;
public class CameraMID extends MIDlet implements CommandListener ...{
private MyCanvas canvas;
private Player myPlayer;
private VideoControl videoControl = null;
// private FocusControl focusControl = null;
// private ZoomControl zoomControl = null;
// private ExposureControl exposureControl = null;
private Vector er = new Vector();
private Form form;
private Command cmdPlay;
private Command cmdError;
private Command cmdQuit;
public static CameraMID mid;
public byte[] data;
public Image img;
public CameraMID() ...{
form = new Form("test");
cmdPlay = new Command("play", Command.OK, 0);
cmdError = new Command("show error", Command.ITEM, 1);
cmdQuit = new Command("quit", Command.EXIT, 2);
form.addCommand(cmdPlay);
form.addCommand(cmdError);
form.addCommand(cmdQuit);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
canvas = new MyCanvas();
mid = this;
try ...{
String str = System.getProperty("video.snapshot.encodings");
if (null == str) ...{
str = "SSN";
} else ...{
str = "SSY";
}
er.addElement(str);
} catch (Exception e) ...{
er.addElement(e.toString());
}
}
public void commandAction(Command c, Displayable d) ...{
if (c.equals(cmdPlay)) ...{
Display.getDisplay(this).setCurrent(canvas);
initPlayer();
} else if (c.equals(cmdError)) ...{
form.deleteAll();
for (int i = 0; i < er.size(); i++) ...{
form.append((String) er.elementAt(i));
}
} else if (c.equals(cmdQuit)) ...{
notifyDestroyed();
}
}
private void initPlayer() ...{
new Thread() ...{
public void run() ...{
freePlayer();
try ...{
myPlayer = Manager.createPlayer("capture://video");
myPlayer.realize();
videoControl = (VideoControl) myPlayer
.getControl("VideoControl");
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,
canvas);
myPlayer.start();
videoControl.setVisible(true);
videoControl.setDisplayLocation(20, 18);
videoControl.setDisplaySize(200, 230);
} catch (Exception e) ...{
er.addElement(e.toString());
}
// try {
// focusControl = (FocusControl) myPlayer
.getControl("FocusControl");
// if (focusControl.isMacroSupported()) {
// focusControl.setMacro(true);
// }
// if (focusControl.isAutoFocusSupported()) {
// autoFocusFlg = true;
// } else {
// autoFocusFlg = false;
// }
// } catch (Exception e) {
// er.addElement("focus " + e.toString());
// }
//
// try {
// zoomControl = (ZoomControl) myPlayer
// .getControl("ZoomControl");
//
// zoomControl.setDigitalZoom(100);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// } catch (Exception e) {
// er.addElement("zoom " + e.toString());
// }
//
// try {
// exposureControl = (ExposureControl) myPlayer
// .getControl("ExposureControl");
exposureControl.setISO(0);
// } catch (Exception e) {
// er.addElement("exposure " + e.toString());
// }
}
}.start();
}
private void freePlayer() ...{
try ...{
myPlayer.stop();
myPlayer.deallocate();
} catch (Exception e) ...{
}
try ...{
myPlayer.close();
} catch (Exception e) ...{
}
}
private void snapshot() ...{
new Thread() ...{
public void run() ...{
try ...{
er.addElement("getSnapshot ");
data = videoControl.getSnapshot("encoding=jpeg&width=640&height=480");
er.addElement("getSnapshot end ");
img = Image.createImage(data, 0, data.length);
form.deleteAll();
form.append(img);
Display.getDisplay(mid).setCurrent(form);
er.addElement("end ");
String time = Long.toString(System.currentTimeMillis());
String path = "file:///C:/pictures/" + time + ".png";
DeviceWirteFile(data, path);
} catch (Exception e) ...{
er.addElement("snapshot " + e.toString());
}
}
}.start();
}
private void zoomIn() ...{
// if (zoomControl != null) {
// zoomControl.setDigitalZoom(ZoomControl.NEXT);
// }
}
private void zoomOut() ...{
// if (zoomControl != null) {
// zoomControl.setDigitalZoom(ZoomControl.PREVIOUS);
// }
}
public void autoFocus() ...{
if (autoFocusFlg) ...{
// try {
// focusControl.setFocus(-1000);
// } catch (Exception e) {
// er.addElement("autofocus " + e.toString());
// }
}
}
private boolean autoFocusFlg = false;
private class MyCanvas extends Canvas implements CommandListener ...{
private Command cmdOK;
private Command cmdBack;
private Command cmdFocus;
public MyCanvas() ...{
cmdOK = new Command("snapshot", Command.OK, 0);
cmdBack = new Command("back", Command.BACK, 1);
cmdFocus = new Command("auto focus", Command.ITEM, 2);
addCommand(cmdOK);
addCommand(cmdBack);
addCommand(cmdFocus);
setCommandListener(this);
}
public void paint(Graphics g) ...{
}
public void commandAction(Command c, Displayable d) ...{
if (c.equals(cmdOK)) ...{
autoFocus();
snapshot();
} else if (c.equals(cmdBack)) ...{
freePlayer();
Display.getDisplay(mid).setCurrent(form);
} else if (c.equals(cmdFocus)) ...{
autoFocus();
}
}
public void keyRepeated(int key) ...{
int nk = getGameAction(key);
switch (nk) ...{
& nbsp;case UP:
case LEFT:
zoomIn();
break;
case DOWN:
case RIGHT:
zoomOut();
break;
}
}
}
private void DeviceWirteFile(byte[] data, String fileName) throws Exception ...{
// FileConnection conn = (FileConnection) Connector.open(fileName,
// Connector.READ_WRITE);
// if (conn.exists()) {
// conn.delete();
// conn.create();
// } else {
// conn.create();
// }
// OutputStream os = conn.openOutputStream();
//
// os.write(data);
// os.flush();
// os.close();
// conn.close();
}
protected void destroyApp(boolean arg0) ...{
}
protected void pauseApp() ...{
}
protected void startApp() ...{
}
}