📄 options.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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.ChoiceGroup;
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.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import org.movino.connection.MovinoConnection;
import org.movino.connection.StreamInfoPacket;
import org.movino.device.Camera;
import org.movino.device.DeviceCapabilities;
public class Options extends Returnable implements CommandListener, ItemCommandListener, ItemStateListener{
public static final String OPTIONS_RECORD_STORE="movino_options";
public static Camera optionCamera = null;
public static Camera.SnapshotFormat optionSnapshotFormat = null;
public static String optionUserName="";
public static String optionServer="";
public static boolean optionAutoCapture=true;
public static String optionAuthor="Unknown";
public static String optionTitle="Unnamed";
public static boolean optionArchiveStream=true;
public static int optionCaptureDelay=0;
public static final String[] CAPTURE_DELAYS=new String[]{"Synchronized","Minimum","Medium","Maximum"};
public static void loadOptions(){
try {
RecordStore rs = RecordStore.openRecordStore(OPTIONS_RECORD_STORE, false);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
if(re.hasNextElement()){
byte[] rec_data = re.nextRecord();
if(rec_data!=null){
DataInputStream in = new DataInputStream(new ByteArrayInputStream(rec_data));
loadOptions(in);
}
}
} catch (Exception e){
System.out.println("exception: "+e+": "+e.getMessage());
}
}
public static void loadOptions(DataInputStream in) throws IOException{
optionCamera = new Camera(in);
optionSnapshotFormat = optionCamera.getSnapshotFormats()[in.readInt()];
optionUserName = in.readUTF();
optionServer = in.readUTF();
optionAutoCapture = in.readBoolean();
optionAuthor = in.readUTF();
optionArchiveStream = in.readBoolean();
optionCaptureDelay = in.readInt();
}
public static boolean saveOptions(){
if(optionCamera==null || optionSnapshotFormat==null) return true;
try {
RecordStore rs = RecordStore.openRecordStore(OPTIONS_RECORD_STORE, true);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
saveOptions(out);
out.flush();
byte[] barray = bout.toByteArray();
RecordEnumeration re = rs.enumerateRecords(null, null, false);
if(re.hasNextElement()){
int rec_id = re.nextRecordId();
rs.setRecord(rec_id, barray, 0, barray.length);
}
else{
rs.addRecord(barray, 0, barray.length);
}
return true;
} catch (Exception e){
System.out.println("exception : "+e+" "+e.getMessage());
return false;
}
}
public static void saveOptions(DataOutputStream out) throws IOException{
optionCamera.write(out);
for(int i=0;i<optionCamera.getSnapshotFormats().length;i++){
if(optionCamera.getSnapshotFormats()[i]==optionSnapshotFormat){
out.writeInt(i);
break;
}
}
out.writeUTF(optionUserName);
out.writeUTF(optionServer);
out.writeBoolean(optionAutoCapture);
out.writeUTF(optionAuthor);
out.writeBoolean(optionArchiveStream);
out.writeInt(optionCaptureDelay);
}
private Form optionsForm;
private Command backCommand;
private ChoiceGroup cameraChoice;
private ChoiceGroup snapshotFormatChoice;
private ChoiceGroup autoCapture;
private ChoiceGroup captureDelay;
private TextField authorField;
private TextField titleField;
private ChoiceGroup archiveStream;
private StringItem reTestDevice;
private Command reTestCommand;
public Options(){
}
public void show(Display display) {
optionsForm = new Form("Options");
optionsForm.setCommandListener(this);
backCommand = new Command("Back", Command.BACK, 1);
optionsForm.addCommand(backCommand);
//camera related
Camera[] cams = DeviceCapabilities.deviceCameras;
cameraChoice = new ChoiceGroup("Camera", ChoiceGroup.POPUP);
int sel_index=0;
for(int i=0;i<cams.length;i++){
cameraChoice.append(cams[i].getDeviceName(), null);
if(optionCamera!=null){
if(cams[i].getDeviceName().equals(optionCamera.getDeviceName())) sel_index=i;
}
}
if(sel_index<cams.length) cameraChoice.setSelectedIndex(sel_index,true);
optionsForm.append(cameraChoice);
cameraChanged();
autoCapture = new ChoiceGroup("Auto capture", ChoiceGroup.POPUP,new String[]{"on","off"},null);
if(optionAutoCapture) autoCapture.setSelectedIndex(0, true);
else autoCapture.setSelectedIndex(1, true);
optionsForm.append(autoCapture);
captureDelay = new ChoiceGroup("Capture delay", ChoiceGroup.POPUP,CAPTURE_DELAYS,null);
captureDelay.setSelectedIndex(optionCaptureDelay, true);
if(optionAutoCapture) optionsForm.append(captureDelay);
//stream info
authorField = new TextField("Author",optionAuthor,1024,TextField.ANY);
optionsForm.append(authorField);
titleField = new TextField("Title",optionTitle,1024,TextField.ANY);
optionsForm.append(titleField);
archiveStream = new ChoiceGroup("Archive stream", ChoiceGroup.POPUP,new String[]{"yes","no"},null);
if(optionArchiveStream) archiveStream.setSelectedIndex(0, true);
else archiveStream.setSelectedIndex(1, true);
archiveStream.setItemCommandListener(this);
optionsForm.append(archiveStream);
//retest device button
reTestDevice = new StringItem(null,"Retest device",Item.BUTTON);
reTestCommand = new Command("Select", Command.OK, 1);
reTestDevice.setDefaultCommand(reTestCommand);
reTestDevice.setItemCommandListener(this);
reTestDevice.setLayout(Item.LAYOUT_LEFT|Item.LAYOUT_NEWLINE_AFTER);
optionsForm.append(reTestDevice);
optionsForm.setItemStateListener(this);
display.setCurrent(optionsForm);
}
public void reShow(Display display){
display.setCurrent(optionsForm);
}
public void cameraChanged(){
int insert_index = optionsForm.size();
for(int i=0;i<optionsForm.size();i++){
if(optionsForm.get(i)==snapshotFormatChoice){
insert_index = i;
optionsForm.delete(i);
break;
}
}
Camera[] cams = DeviceCapabilities.deviceCameras;
if(cams.length<=0 || cameraChoice.getSelectedIndex()<0) return;
Camera cam = cams[cameraChoice.getSelectedIndex()];
Camera.SnapshotFormat[] sfs = cam.getSnapshotFormats();
snapshotFormatChoice = new ChoiceGroup("Format",ChoiceGroup.POPUP);
int sel_index=0;
for(int i=0;i<sfs.length;i++){
if(optionSnapshotFormat!=null){
if(sfs[i].getFormatString().equals(optionSnapshotFormat.getFormatString())) sel_index = i;
}
snapshotFormatChoice.append(sfs[i].getDescription(), null);
}
if(sel_index<sfs.length) snapshotFormatChoice.setSelectedIndex(sel_index, true);
optionsForm.insert(insert_index, snapshotFormatChoice);
}
public void commandAction(Command cmd, Displayable arg1){
/*if(cmd==applyCommand){
apply();
}
else */if(cmd==backCommand){
apply();
}
}
public void apply(){
Camera[] cams = DeviceCapabilities.deviceCameras;
int sel_index = cameraChoice.getSelectedIndex();
if(sel_index<cams.length && sel_index>=0){
optionCamera = cams[sel_index];
sel_index = snapshotFormatChoice.getSelectedIndex();
if(sel_index<optionCamera.getSnapshotFormats().length && sel_index>=0){
optionSnapshotFormat = optionCamera.getSnapshotFormats()[sel_index];
}
else{
optionSnapshotFormat = null;
}
}
else{
optionCamera = null;
optionSnapshotFormat = null;
}
optionAutoCapture = (autoCapture.getSelectedIndex()==0);
optionCaptureDelay = captureDelay.getSelectedIndex();
optionAuthor = authorField.getString();
optionTitle = titleField.getString();
optionArchiveStream = (archiveStream.getSelectedIndex()==0);
sendInfoPacket(); //send info packet
returnBack();
}
public void sendInfoPacket(){
MovinoConnection mc = MovinoConnection.currentConnection;
Camera.SnapshotFormat sf = optionSnapshotFormat;
if(mc!=null && sf!=null){
mc.sendPacket(new StreamInfoPacket(sf.getWidth(),sf.getHeight(),false,optionAuthor,optionTitle,optionArchiveStream));
}
}
public void commandAction(Command cmd, Item item){
if(item==reTestDevice){
new DeviceCapabilities(this).show(getDisplay(),false);
}
}
public void itemStateChanged(Item item){
if(item==cameraChoice) cameraChanged();
else if(item==autoCapture){
if(autoCapture.getSelectedIndex()==0){
int insert_index = optionsForm.size();
for(int i=0;i<optionsForm.size();i++){
if(optionsForm.get(i)==autoCapture){
insert_index = i+1;
}
else if(optionsForm.get(i)==captureDelay){
return;
}
}
optionsForm.insert(insert_index, captureDelay);
}
else{
for(int i=0;i<optionsForm.size();i++){
if(optionsForm.get(i)==captureDelay){
optionsForm.delete(i);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -