CaptureApplication.java

00001 package edu.stanford.hci.r3.pen.handwriting;
00002 
00003 import java.io.File;
00004 import java.util.List;
00005 
00006 import edu.stanford.hci.r3.Application;
00007 import edu.stanford.hci.r3.events.handlers.HandwritingRecognizer;
00008 import edu.stanford.hci.r3.events.handlers.InkCollector;
00009 import edu.stanford.hci.r3.paper.Region;
00010 import edu.stanford.hci.r3.paper.Sheet;
00011 import edu.stanford.hci.r3.pattern.coordinates.PatternLocationToSheetLocationMapping;
00012 import edu.stanford.hci.r3.pen.Pen;
00013 import edu.stanford.hci.r3.pen.PenSample;
00014 import edu.stanford.hci.r3.pen.streaming.PenAdapter;
00015 import edu.stanford.hci.r3.pen.streaming.listeners.PenListener;
00016 import edu.stanford.hci.r3.units.PatternDots;
00017 import edu.stanford.hci.r3.util.DebugUtils;
00018 
00034 public class CaptureApplication extends Application {
00035 
00039         private PenSample anchorPointBottomRight;
00040 
00044         private PenSample anchorPointTopLeft;
00045 
00049         private HandwritingCaptureDebugger gui;
00050 
00054         private HandwritingRecognizer handwritingRecognizer;
00055 
00059         private InkCollector inkCollector;
00060 
00061         private long lastTimeNewInkArrived = 0;
00062 
00066         private Sheet mainSheet;
00067 
00071         private Pen pen;
00072 
00073         private Thread recognizeThread = new Thread(new Runnable() {
00074                 public void run() {
00075                         while (true) {
00076                                 try {
00077                                         wait();
00078 
00079                                 } catch (InterruptedException e) {
00080                                         e.printStackTrace();
00081                                 }
00082                         }
00083                 }
00084         });
00085 
00091         public CaptureApplication(HandwritingCaptureDebugger debugger) {
00092                 super("Handwriting Capture");
00093                 addPenInput(getPen());
00094                 gui = debugger;
00095         }
00096 
00100         public void addCalibrationHandler() {
00101                 anchorPointTopLeft = null;
00102                 anchorPointBottomRight = null;
00103 
00104                 final Pen pen = getPen();
00105                 pen.addLivePenListener(new PenAdapter() {
00106                         public void penUp(PenSample sample) {
00107                                 if (anchorPointTopLeft == null) {
00108                                         anchorPointTopLeft = sample;
00109                                         DebugUtils.println("Top Left Point is now set to " + anchorPointTopLeft);
00110                                         gui.showTopLeftPointConfirmation();
00111                                 } else if (anchorPointBottomRight == null) {
00112                                         anchorPointBottomRight = sample;
00113                                         DebugUtils
00114                                                         .println("Bottom Right Point is now set to " + anchorPointBottomRight);
00115                                         gui.showBottomRightPointConfirmation();
00116                                         scaleInkPanelToFit();
00117                                         addOneSheetAndOneRegionForHandwritingCapture();
00118 
00119                                         // after setting the bottom right point, we should remove this pen listener...
00120                                         final PenListener listener = this;
00121                                         // We must modify the listeners from an external thread, as we are
00122                                         // currently iterating through it
00123                                         // This will happen after we have released the lock
00124                                         new Thread(new Runnable() {
00125                                                 public void run() {
00126                                                         pen.removeLivePenListener(listener);
00127                                                 }
00128                                         }).start();
00129                                 }
00130                         }
00131 
00132                 });
00133         }
00134 
00138         private void addOneSheetAndOneRegionForHandwritingCapture() {
00139                 // ask the event engine to remove our sheet and our mappings, if they exist
00140                 if (mainSheet != null) {
00141                         DebugUtils.println("Removing old region...");
00142 
00143                         // remove the sheet from this application
00144                         removeSheet(mainSheet);
00145                         mainSheet = null;
00146                 }
00147 
00148                 // add a new sheet
00149                 final Sheet sheet = getMainSheet();
00150                 final Region region = setupCaptureRegion();
00151                 sheet.addRegion(region);
00152 
00153                 // determine the bounds of the region in pattern space
00154                 // this information was provided by the user
00155                 final double tlX = anchorPointTopLeft.getX();
00156                 final double tlY = anchorPointTopLeft.getY();
00157                 final double brX = anchorPointBottomRight.getX();
00158                 final double brY = anchorPointBottomRight.getY();
00159                 final double width = brX - tlX;
00160                 final double height = brY - tlY;
00161 
00162                 // create this custom mapping object
00163                 final PatternLocationToSheetLocationMapping mapping = new PatternLocationToSheetLocationMapping(
00164                                 sheet);
00165 
00166                 // tie the pattern bounds to this region object
00167                 mapping.setPatternInformationOfRegion(region, //
00168                                 new PatternDots(tlX), new PatternDots(tlY), // 
00169                                 new PatternDots(width), new PatternDots(height));
00170 
00171                 addSheet(sheet, mapping);
00172                 // DebugUtils.println(mapping);
00173         }
00174 
00175         public void clearInk() {
00176                 if (inkCollector != null) {
00177                         inkCollector.clear();
00178                 }
00179                 if (handwritingRecognizer != null) {
00180                         handwritingRecognizer.clear();
00181                 }
00182         }
00183 
00189         private Sheet getMainSheet() {
00190                 if (mainSheet == null) {
00191                         mainSheet = new Sheet(8.5, 11);
00192                 }
00193                 return mainSheet;
00194         }
00195 
00199         public Pen getPen() {
00200                 if (pen == null) {
00201                         pen = new Pen("Main Pen");
00202                 }
00203                 return pen;
00204         }
00205 
00206         public void retrieveAlternatives() {
00207                 // top ten list
00208                 final List<String> topTen = handwritingRecognizer.recognizeHandwritingWithAlternatives();
00209                 gui.setAlternatives(topTen);
00210         }
00211 
00215         public void saveInkToDisk() {
00216                 File file = new File(System.currentTimeMillis() + "_ink.xml");
00217                 DebugUtils.println("Saving as: " + file.getAbsolutePath());
00218                 inkCollector.saveInkToXMLFile(file);
00219         }
00220 
00225         private void scaleInkPanelToFit() {
00226                 final double width = anchorPointBottomRight.x - anchorPointTopLeft.x;
00227                 final double height = anchorPointBottomRight.y - anchorPointTopLeft.y;
00228                 double newScale = 1.0;
00229                 if (width > height) {
00230                         // constrain to the width
00231                         final PatternDots numDots = new PatternDots(width);
00232                         final double definedWidth = numDots.getValueInPixels();
00233                         final double guiWidth = gui.getInkPanel().getSize().getWidth();
00234                         newScale = guiWidth / definedWidth;
00235                 } else {
00236                         // constrain to the height
00237                         final PatternDots numDots = new PatternDots(height);
00238                         final double definedHeight = numDots.getValueInPixels();
00239                         final double guiHeight = gui.getInkPanel().getSize().getHeight();
00240                         newScale = guiHeight / definedHeight;
00241                 }
00242                 // DebugUtils.println("New Scale: " + newScale);
00243                 gui.getInkPanel().setScale(newScale);
00244         }
00245 
00251         private Region setupCaptureRegion() {
00252                 // the actual "physical" size of this region doesn't matter, as we never print it!
00253                 final Region region = new Region("Handwriting Capture", 0, 0, 1, 1);
00254 
00255                 // for displaying ink
00256                 inkCollector = new InkCollector() {
00257                         @Override
00258                         public void contentArrived() {
00259                                 // DebugUtils.println(getInk().getNumStrokes());
00260                                 gui.getInkPanel().addInk(getNewInkOnly());
00261                         }
00262                 };
00263 
00264                 // for recognizing the strokes
00265                 handwritingRecognizer = new HandwritingRecognizer() {
00266 
00267                         @Override
00268                         public void contentArrived() {
00269                                 lastTimeNewInkArrived = System.currentTimeMillis();
00270 
00271                                 String text = recognizeHandwriting();
00272                                 DebugUtils.println("Handwritten Content: " + text);
00273                                 gui.setInfoText(text);
00274                                 // gui.setAlternatives(topTen);
00275                         }
00276                 };
00277 
00278                 region.addEventHandler(inkCollector);
00279                 region.addEventHandler(handwritingRecognizer);
00280                 return region;
00281         }
00282 }

Generated on Sat Apr 14 18:21:33 2007 for R3 Paper Toolkit by  doxygen 1.4.7