Exemplo n.º 1
0
  // -----------------------------------------------------
  // Description: Determine the rotation of the png file
  // and rotate it whereby the side edge is 90 degrees.
  // Return the angle of rotation.
  // -----------------------------------------------------
  double autoAlignRotate(String fileName, String destinationDirectory) {

    String fileName_woext = (fileName.substring(0, fileName.lastIndexOf('.')));

    Spawn.execute(
        Settings.Programs.CONVERT.path(),
        "-deskew",
        "40%",
        "+repage",
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName,
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName);

    return 0.0;
  }
Exemplo n.º 2
0
  // -----------------------------------------------------
  // Description: Normalize the file to have the origin
  // be the top left of the form non white space. We
  // are gettting rid of the whitespace borders.
  // -----------------------------------------------------
  void normalizeByTrimming(String fileName, String destinationDirectory) {

    String fileName_woext = (fileName.substring(0, fileName.lastIndexOf('.')));

    Spawn.execute(
        Settings.Programs.CONVERT.path(),
        "-trim",
        "-fuzz",
        "5%",
        "+repage",
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName,
        destinationDirectory
            + File.separator
            + fileName_woext
            + File.separator
            + fileName_woext
            + "_trim.png");

    pageX = 0;
    pageY = 0;
  }
Exemplo n.º 3
0
  // -----------------------------------------------------
  // Description: Using the specified template, extract
  // the template sections into PNG and TXT corresponding
  // parts.
  // -----------------------------------------------------
  void extractWithTemplate(String templateName, String fileName, String destinationDirectory) {

    // from template
    int intOriginX = 0;
    int intOriginY = 0;

    // from template
    int intX = 0;
    int intY = 0;
    int intWidth = 0;
    int intHeight = 0;

    // calculated
    int viaTemplateX = 0;
    int viaTemplateY = 0;
    int viaTemplatePlusWidth = 0;
    int viaTemplatePlusHeight = 0;

    System.out.println("fileName = " + fileName);
    String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.'));
    String pathPlusFileName_woext =
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName_woext;
    System.out.println("pathPlusFileName_woext = " + pathPlusFileName_woext);

    try {

      File fXmlFile = new File(templateLocation, templateName);
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);
      doc.getDocumentElement().normalize();

      System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

      NodeList nList = doc.getElementsByTagName("section");

      Node nNode;
      for (int temp = 0; temp < nList.getLength(); temp++) {

        nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;

          String myX = getTagValue("x", eElement);
          String myY = getTagValue("y", eElement);
          String myWidth = getTagValue("width", eElement);
          String myHeight = getTagValue("height", eElement);
          String mySuffix = getTagValue("suffix", eElement);

          intX = Integer.parseInt(myX);
          intY = Integer.parseInt(myY);
          intWidth = Integer.parseInt(myWidth);
          intHeight = Integer.parseInt(myHeight);

          viaTemplateX = intX - (intOriginX - pageX);
          viaTemplateY = intY - (intOriginY - pageY);
          viaTemplatePlusWidth = viaTemplateX + intWidth;
          viaTemplatePlusHeight = viaTemplateY + intHeight;

          Spawn.execute(
              Settings.Programs.CONVERT.path(),
              pathPlusFileName_woext + "_trim.png",
              "-crop",
              String.format("%dx%d+%d+%d", intWidth, intHeight, viaTemplateX, viaTemplateY),
              "+repage",
              pathPlusFileName_woext + "_" + mySuffix + ".png");
          Spawn.execute(
              Settings.Programs.CONVERT.path(),
              pathPlusFileName_woext + "_trim.png",
              "-fill",
              "none",
              "-stroke",
              "red",
              "-strokewidth",
              "3",
              "-draw",
              String.format(
                  "rectangle %d,%d %d,%d",
                  viaTemplateX, viaTemplateY, viaTemplatePlusWidth, viaTemplatePlusHeight),
              "+repage",
              pathPlusFileName_woext + "_draw.png");
          Spawn.execute(
              Settings.Programs.TESSERACT.path(),
              pathPlusFileName_woext + "_" + mySuffix + ".png",
              pathPlusFileName_woext + "_" + mySuffix);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 4
0
  // -----------------------------------------------------
  // Description: Determine if a template exists for the
  // given file and return true on success.
  // -----------------------------------------------------
  boolean templateExistFor(String fileName, String destinationDirectory) {

    templateIdentified = false;

    // from template
    int intOriginX = 0;
    int intOriginY = 0;

    // from template
    int intX = 0;
    int intY = 0;
    int intWidth = 0;
    int intHeight = 0;

    // calculated
    int viaTemplateX = 0;
    int viaTemplateY = 0;
    int viaTemplatePlusWidth = 0;
    int viaTemplatePlusHeight = 0;

    String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.'));
    String pathPlusFileName_woext =
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName_woext;

    try {

      File folder = new File(templateLocation);
      File[] listOfFiles = folder.listFiles();

      long startTime = System.currentTimeMillis();

      for (File file : listOfFiles) {

        if (file.isFile()) {

          templateName = "";

          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          Document doc = dBuilder.parse(file);
          doc.getDocumentElement().normalize();

          System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
          NodeList nList = doc.getElementsByTagName("condition");

          for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;

              String myX = getTagValue("x", eElement);
              String myY = getTagValue("y", eElement);
              String myWidth = getTagValue("width", eElement);
              String myHeight = getTagValue("height", eElement);
              String mySuffix = getTagValue("suffix", eElement);
              String myRequirements = getTagValue("required", eElement);

              intX = Integer.parseInt(myX);
              intY = Integer.parseInt(myY);
              intWidth = Integer.parseInt(myWidth);
              intHeight = Integer.parseInt(myHeight);

              viaTemplateX = intX - (intOriginX - pageX);
              viaTemplateY = intY - (intOriginY - pageY);
              viaTemplatePlusWidth = viaTemplateX + intWidth;
              viaTemplatePlusHeight = viaTemplateY + intHeight;

              Spawn.execute(
                  Settings.Programs.CONVERT.path(),
                  pathPlusFileName_woext + "_trim.png",
                  "-crop",
                  String.format("%dx%d+%d+%d", intWidth, intHeight, viaTemplateX, viaTemplateY),
                  "+repage",
                  pathPlusFileName_woext + "_" + mySuffix + ".png");
              Spawn.execute(
                  Settings.Programs.CONVERT.path(),
                  pathPlusFileName_woext + "_trim.png",
                  "-fill",
                  "none",
                  "-stroke",
                  "red",
                  "-strokewidth",
                  "3",
                  "-draw",
                  String.format(
                      "rectangle %d,%d %d,%d",
                      viaTemplateX, viaTemplateY, viaTemplatePlusWidth, viaTemplatePlusHeight),
                  "+repage",
                  pathPlusFileName_woext + "_draw.png");
              Spawn.execute(
                  Settings.Programs.TESSERACT.path(),
                  pathPlusFileName_woext + "_" + mySuffix + ".png",
                  pathPlusFileName_woext + "_" + mySuffix);

              String line = ""; // String that holds current file line
              String accumulate = "";

              try {
                FileReader input = new FileReader(pathPlusFileName_woext + "_" + mySuffix + ".txt");
                BufferedReader bufRead = new BufferedReader(input);

                line = bufRead.readLine();

                while (line != null) {
                  accumulate += line;
                  line = bufRead.readLine();
                }

                bufRead.close();
                input.close();

              } catch (IOException e) {
                e.printStackTrace();
              }

              String[] requirements = myRequirements.split("#");
              for (String requirement : requirements) {
                if (requirement.equals(accumulate.trim())) {
                  templateName = file.getName();
                  templateIdentified = true;
                  return templateIdentified;
                }
              }
            }
          }
        }
      }

      // record end time
      long endTime = System.currentTimeMillis();
      System.out.println(
          "Template Search [" + fileName + "] " + (endTime - startTime) / 1000 + " seconds");

    } catch (Exception e) {
      e.printStackTrace();
    }

    return templateIdentified;
  }