public ExitCode setValue( int value){
// A(104), B(203);
switch(value){
case 104: return ExitCode.A;
case 203: return ExitCode.B;
default:
return ExitCode.Unknown //Keep an default or error enum handy
}
}
从调用应用程序
int i = 104;
ExitCode serverExitCode = ExitCode.setValue(i);
public class OuterClass {
public void exit(boolean isTrue){
if(isTrue){
System.exit(ExitCode.A);
}else{
System.exit(ExitCode.B);
}
}
public static class ExitCode{
public static final int A = 203;
public static final int B = 204;
}
}
这个枚举类用于我在 Raspberry Pi 上运行的“服务器”程序。这个程序接收来自客户端的命令,然后执行终端命令,对安装在我的3D 打印机上的摄像头进行调整。
使用 Pi 上的 Linux 程序“ v4l2-ctl”,你可以提取给定附加网络摄像头的所有可能的调整命令,它也提供设置数据类型、最小值和最大值、给定值范围内的值步数等等,所以我把所有这些都放在一个枚举中,创建了一个枚举接口,使得设置和获取每个命令的值都很容易,以及一个简单的方法来获取实际执行的终端命令(使用 Process 和 Runtime 类) ,以便调整设置。
public static void test() {
PICam.setValue(0,127); //Set brightness to 125
PICam.setValue(PICam.SHARPNESS,143); //Set sharpness to 125
String command1 = PICam.getSetCommandStringFor(PICam.BRIGHTNESS); //Get command line string to include the brightness value that we previously set referencing it by enum constant.
String command2 = PICam.getSetCommandStringFor(0); //Get command line string to include the brightness value that we previously set referencing it by index number.
String command3 = PICam.getDefaultCamString(PICam.BRIGHTNESS); //Get command line string with the default value
String command4 = PICam.getSetCommandStringFor(PICam.SHARPNESS); //Get command line string with the sharpness value that we previously set.
String command5 = PICam.getDefaultCamString(PICam.SHARPNESS); //Get command line string with the default sharpness value.
System.out.println(command1);
System.out.println(command2);
System.out.println(command3);
System.out.println(command4);
System.out.println(command5);
}