@Override protected void openWatchDialog() { if (watchDialog == null) { watchDialog = new WatchDialog(this); } watchDialog.setWatchFolder(watchFolder); watchDialog.setOutputFolder(outputFolder); watchDialog.setWatchEnabled(watchEnabled); if (watchDialog.showDialog() == JOptionPane.OK_OPTION) { watchFolder = watchDialog.getWatchFolder(); outputFolder = watchDialog.getOutputFolder(); watchEnabled = watchDialog.isWatchEnabled(); watcher.setPath(new File(watchFolder)); watcher.setEnabled(watchEnabled); } }
public GuiWithWatch() { watchFolder = prefs.get("WatchFolder", System.getProperty("user.home")); outputFolder = prefs.get("OutputFolder", System.getProperty("user.home")); watchEnabled = prefs.getBoolean("WatchEnabled", false); statusFrame = new StatusFrame(); statusFrame.setTitle(bundle.getString("statusFrame.Title")); // watch for new image files final Queue<File> queue = new LinkedList<File>(); watcher = new Watcher(queue, new File(watchFolder)); watcher.setEnabled(watchEnabled); Thread t = new Thread(watcher); t.start(); // autoOCR if there are files in the queue Action autoOcrAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { final File imageFile = queue.poll(); if (imageFile != null) { if (!statusFrame.isVisible()) { statusFrame.setVisible(true); } statusFrame.getTextArea().append(imageFile.getPath() + "\n"); final ArrayList<IIOImage> iioImageList = ImageIOHelper.getIIOImageList(imageFile); if (iioImageList == null) { statusFrame .getTextArea() .append( " ** " + bundle.getString("Cannotprocess") + " " + imageFile.getName() + " **\n"); return; } if (curLangCode == null) { statusFrame .getTextArea() .append(" ** " + bundle.getString("Please_select_a_language.") + " **\n"); // queue.clear(); return; } SwingUtilities.invokeLater( new Runnable() { @Override public void run() { try { OCR ocrEngine = new OCR(tessPath); String result = ocrEngine.recognizeText(iioImageList, -1, curLangCode); // postprocess to correct common OCR errors result = Processor.postProcess(result, curLangCode); BufferedWriter out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File(outputFolder, imageFile.getName() + ".txt")), UTF8)); out.write(result); out.close(); } catch (Exception e) { statusFrame .getTextArea() .append( " ** " + bundle.getString("Cannotprocess") + " " + imageFile.getName() + " **\n"); e.printStackTrace(); } } }); } } }; new Timer(5000, autoOcrAction).start(); }