📄 consoleuserinterfacefactory.java
字号:
package com.digitalpersona.onetouch.sampleapp;
import com.digitalpersona.onetouch.*;
import com.digitalpersona.onetouch.capture.DPFPCapture;
import com.digitalpersona.onetouch.capture.DPFPCapturePriority;
import com.digitalpersona.onetouch.capture.event.DPFPDataEvent;
import com.digitalpersona.onetouch.capture.event.DPFPDataListener;
import com.digitalpersona.onetouch.capture.event.DPFPReaderStatusAdapter;
import com.digitalpersona.onetouch.capture.event.DPFPReaderStatusEvent;
import com.digitalpersona.onetouch.processing.DPFPEnrollment;
import com.digitalpersona.onetouch.processing.DPFPFeatureExtraction;
import com.digitalpersona.onetouch.processing.DPFPImageQualityException;
import com.digitalpersona.onetouch.readers.DPFPReaderDescription;
import com.digitalpersona.onetouch.readers.DPFPReadersCollection;
import com.digitalpersona.onetouch.verification.DPFPVerification;
import com.digitalpersona.onetouch.verification.DPFPVerificationResult;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.EnumMap;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Implementation of UserInterface.Factory interface that creates a console-based user interface
*/
public class ConsoleUserInterfaceFactory implements UserInterface.Factory {
/**
* Creates an object implementing UserInterface interface
*
* @param userDatabase user database to be used with the ui
* @return created instance
*/
public UserInterface createUI(UserDatabase userDatabase) {
return new ConsoleUserInterface(userDatabase);
}
/**
* Console-based UserInterface
*/
private static class ConsoleUserInterface
implements UserInterface
{
/**
* keeps a user database
*/
private UserDatabase userDatabase;
/**
* Constructs an instance
*
* @param userDatabase user database to be used with the ui
*/
public ConsoleUserInterface(UserDatabase userDatabase) {
this.userDatabase = userDatabase;
}
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p/>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
public void run() {
System.out.println("\n*** Console UI ***");
String activeReader = null;
boolean readerSelected = false;
int res;
while ((res = MenuShow(mainMenu, MENU_WITH_EXIT)) != exitItem.getValue()) try {
switch (res) {
case MAIN_MENU_ADD:
addUser();
break;
case MAIN_MENU_ENROLL:
if (readerSelected)
register(activeReader);
else
System.out.println("No reader selected");
break;
case MAIN_MENU_VERIFY:
if (readerSelected)
verify(activeReader);
else
System.out.println("No reader selected");
break;
case MAIN_MENU_ENUMERATE:
listReaders();
break;
case MAIN_MENU_SELECT:
try {
activeReader = selectReader(activeReader);
readerSelected = true;
} catch (IndexOutOfBoundsException e) {
System.out.println("There are no readers available");
}
break;
}
} catch (Exception e) { }
}
/**
* Console menu item
*/
private static class MenuItem
{
private String text;
private int value;
/**
* Creates a menu item
*
* @param text item text
* @param value value to return if item is chosen
*/
public MenuItem(String text, int value) {
this.text = text;
this.value = value;
}
/**
* Returns the menu item's text
*
* @return menu item text
*/
public String getText() {
return text;
}
/**
* Returns the menu item's associated value
*
* @return associated value
*/
public int getValue() {
return value;
}
}
/**
* Specifies that menu should be appended with "Back" item
*/
private static final int MENU_WITH_BACK = 2;
/**
* Specifies that menu should be appended with "Exit" item
*/
private static final int MENU_WITH_EXIT = 1;
/**
* "Exit" menu item
*/
private static final MenuItem exitItem = new MenuItem("Exit the application", -1);
/**
* "Back" menu item
*/
private static final MenuItem backItem = new MenuItem("Return to the previous menu", -2);
private static final int MAIN_MENU_ENUMERATE = 101;
private static final int MAIN_MENU_SELECT = 102;
private static final int MAIN_MENU_ADD = 103;
private static final int MAIN_MENU_ENROLL = 104;
private static final int MAIN_MENU_VERIFY = 105;
private static final Vector<MenuItem> mainMenu;
static {
mainMenu = new Vector<MenuItem>();
mainMenu.add(new MenuItem("List all available readers", MAIN_MENU_ENUMERATE));
mainMenu.add(new MenuItem("Select a reader", MAIN_MENU_SELECT));
mainMenu.add(new MenuItem("Add a person to the database", MAIN_MENU_ADD));
mainMenu.add(new MenuItem("Perform fingerprint enrollment", MAIN_MENU_ENROLL));
mainMenu.add(new MenuItem("Perform fingerprint verification", MAIN_MENU_VERIFY));
}
private static final EnumMap<DPFPFingerIndex, String> fingerNames;
static {
fingerNames = new EnumMap<DPFPFingerIndex, String>(DPFPFingerIndex.class);
fingerNames.put(DPFPFingerIndex.LEFT_PINKY, "left pinky");
fingerNames.put(DPFPFingerIndex.LEFT_RING, "left ring");
fingerNames.put(DPFPFingerIndex.LEFT_MIDDLE, "left middle");
fingerNames.put(DPFPFingerIndex.LEFT_INDEX, "left index");
fingerNames.put(DPFPFingerIndex.LEFT_THUMB, "left thumb");
fingerNames.put(DPFPFingerIndex.RIGHT_PINKY, "right pinky");
fingerNames.put(DPFPFingerIndex.RIGHT_RING, "right ring");
fingerNames.put(DPFPFingerIndex.RIGHT_MIDDLE, "right middle");
fingerNames.put(DPFPFingerIndex.RIGHT_INDEX, "right index");
fingerNames.put(DPFPFingerIndex.RIGHT_THUMB, "right thumb");
}
private int MenuShow(Vector<MenuItem> menu, int nMenuFlags) {
int choice = 0;
if (menu == null)
return choice;
while (true) {
System.out.println();
for (int i = 0; i < menu.size(); ++i)
System.out.printf("%d: %s\n", i + 1, menu.elementAt(i).getText());
StringBuilder sb = new StringBuilder();
sb.append(String.format("Choose an option (1 - %d", menu.size()));
if ((nMenuFlags & MENU_WITH_BACK) != 0) {
System.out.printf("\nR: %s\n\n", backItem.getText());
sb.append(", R");
}
if ((nMenuFlags & MENU_WITH_EXIT) != 0) {
System.out.printf("\nE: %s\n\n", exitItem.getText());
sb.append(", E");
}
sb.append("): ");
String userInput = ShowDialog(sb.toString());
if ((nMenuFlags & MENU_WITH_EXIT) != 0 && userInput.equalsIgnoreCase("E")) {
choice = exitItem.getValue();
break;
}
if ((nMenuFlags & MENU_WITH_BACK) != 0 && userInput.equalsIgnoreCase("R")) {
choice = backItem.getValue();
break;
}
int nInput;
try {
nInput = Integer.parseInt(userInput);
} catch (NumberFormatException e) {
System.out.printf("\nInvalid input: \"%s\"\n", userInput);
continue;
}
if (nInput < 1 || nInput > menu.size()) {
System.out.printf("\nIncorrect choice: \"%s\"\n", userInput);
continue;
}
choice = menu.elementAt(nInput - 1).getValue();
break;
}
System.out.println();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -