📄 camera.java
字号:
/* * Movino J2ME Client * Copyright (C) 2007 Johannes Berg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package org.movino.device;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.util.Vector;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 org.movino.Capture;public class Camera { public static final String[] CAMERA_DEVICES = new String[]{ "image", "video", "devcam0", "devcam1", "devcam2", "cam", "cam0", "cam1", }; public static final String[] SNAPSHOT_FORMATS=new String[]{ "encoding=jpeg", "encoding=jpeg&width=160&height=120", "encoding=jpeg&width=128&height=96", "encoding=jpeg&width=176&height=144", "encoding=jpeg&width=352&height=288", }; public static String[] getSnapshotFormatStrings(){ String encs = System.getProperty("video.snapshot.encodings"); encs = encs.trim(); int enc_counter=0; String[] encodings=new String[10]; while(encs.indexOf(' ')!=-1 && enc_counter<9){ encodings[enc_counter] = encs.substring(0, encs.indexOf(' ')); enc_counter++; encs = encs.substring(encs.indexOf(' ')+1).trim(); } if(encs.indexOf(' ')==-1){ encodings[enc_counter] = encs; enc_counter++; } String[] ret_encs = new String[enc_counter]; System.arraycopy(encodings, 0, ret_encs, 0, enc_counter); return ret_encs; } private String deviceName; private boolean isAvailable; private SnapshotFormat[] snapshotFormats; public Camera(String device_name){ deviceName = device_name; isAvailable = true; snapshotFormats = new SnapshotFormat[0]; } public Camera(DataInputStream in) throws IOException{ //read camera data and snapshot formats deviceName = in.readUTF(); isAvailable = true; int sf_count = in.readInt(); snapshotFormats = new SnapshotFormat[sf_count]; for(int i=0;i<snapshotFormats.length;i++){ snapshotFormats[i] = new SnapshotFormat(in); } } public void write(DataOutputStream out) throws IOException{ out.writeUTF(deviceName); out.writeInt(snapshotFormats.length); for(int i=0;i<snapshotFormats.length;i++){ snapshotFormats[i].write(out); } } public String testCamera(){ isAvailable=false; String mess = "Testing camera "+deviceName+"."; try { Player player = Manager.createPlayer(Capture.PROTOCOL+"://"+deviceName); player.realize(); player.start(); VideoControl vc = (VideoControl)player.getControl("VideoControl"); if(vc!=null){ vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null); isAvailable=true; player.stop(); player.deallocate(); player.close(); player=null; vc=null; mess += testFormats()+"\n"; } } catch (Exception e){ return "Camera "+deviceName+" failed test. "+e+": "+e.getMessage(); } return mess; } public SnapshotFormat[] getSnapshotFormats(){ return snapshotFormats; } public String testFormats(){ String mess = ""; Vector snapshot_formats = new Vector(); for(int i=0;i<SNAPSHOT_FORMATS.length;i++){ SnapshotFormat sf = new SnapshotFormat(SNAPSHOT_FORMATS[i]); mess += sf.testFormat()+"\n"; if(sf.isAvailable()){ snapshot_formats.addElement(sf); } } String[] formats = getSnapshotFormatStrings(); for(int i=0;i<formats.length;i++){ SnapshotFormat sf = new SnapshotFormat(formats[i]); mess += sf.testFormat()+"\n"; if(sf.isAvailable()){ snapshot_formats.addElement(sf); } } //cleanup for(int i=0;i<snapshot_formats.size();i++){ SnapshotFormat sf = (SnapshotFormat)snapshot_formats.elementAt(i); if(sf.imageFormat==SnapshotFormat.IMAGE_FORMAT_UNKNOWN){ snapshot_formats.removeElementAt(i); i--; continue; } for(int i2=i+1;i2<snapshot_formats.size();i2++){ SnapshotFormat sf2 = (SnapshotFormat)snapshot_formats.elementAt(i2); if(sf2.compareTo(sf)==0){ snapshot_formats.removeElementAt(i2); i2--; } } } snapshotFormats = new SnapshotFormat[snapshot_formats.size()]; for(int i=0;i<snapshot_formats.size();i++){ snapshotFormats[i] = (SnapshotFormat)snapshot_formats.elementAt(i); } return mess; } public boolean hasSnapshots() { return snapshotFormats.length>0; } public boolean isAvailable() { return isAvailable; } public class SnapshotFormat{ public static final String IMAGE_FORMAT_UNKNOWN="?"; public static final String IMAGE_FORMAT_JPEG="jpeg"; private String imageFormat; private String formatString; private boolean isAvailable; private int actualWidth; private int actualHeight; public SnapshotFormat(String format){ formatString = format; isAvailable=true; actualWidth=10; actualHeight=10; imageFormat = IMAGE_FORMAT_UNKNOWN; } public SnapshotFormat(DataInputStream in) throws IOException { formatString = in.readUTF(); actualWidth = in.readInt(); actualHeight = in.readInt(); setImageFormat(); } public void write(DataOutputStream out) throws IOException { out.writeUTF(formatString); out.writeInt(actualWidth); out.writeInt(actualHeight); } public int getWidth(){ return actualWidth; } public int getHeight(){ return actualHeight; } public String getFormatString(){ return formatString; } public boolean isAvailable(){ return isAvailable; } public String getDescription(){ return actualWidth+"x"+actualHeight+" "+imageFormat; } public String testFormat(){ isAvailable=false; Player player=null; try { System.gc(); player = Manager.createPlayer(Capture.PROTOCOL+"://"+deviceName); player.realize(); player.start(); VideoControl vc = (VideoControl)player.getControl("VideoControl"); vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null); byte[] img_bytes = vc.getSnapshot(formatString); if(img_bytes!=null){ Image img = Image.createImage(img_bytes,0,img_bytes.length); if(img!=null){ setImageFormat(); actualWidth = img.getWidth(); actualHeight = img.getHeight(); isAvailable = true; img=null; img_bytes=null; } else{ return "Format "+formatString+" passed test, but could not load image."; } } else{ return "Format "+formatString+" passed test, but could not get image bytes."; } } catch (Throwable e){ return "Format "+formatString+" failed test. "+e+": "+e.getMessage(); } finally{ if(player!=null){ try { player.stop(); } catch (MediaException e){} player.deallocate(); player.close(); } } return "Format "+formatString+"("+actualWidth+"x"+actualHeight+") passed test."; } private void setImageFormat() { if(formatString.indexOf("jpeg")!=-1 || formatString.indexOf("jpg")!=-1){ imageFormat=IMAGE_FORMAT_JPEG; } else imageFormat=IMAGE_FORMAT_UNKNOWN; } public int compareTo(Object o) { SnapshotFormat sf = (SnapshotFormat)o; if(sf.actualHeight==actualHeight && sf.actualWidth==actualWidth && sf.imageFormat==imageFormat){ return 0; } return -1; } } public String getDeviceName() { return deviceName; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -