Beispiel #1
0
 /** Test of TessBaseAPISetVariable method, of class TessAPI1. */
 @Test
 public void testTessBaseAPISetVariable() {
   logger.info("TessBaseAPISetVariable");
   String name = "tessedit_create_hocr";
   String value = "1";
   int expResult = 1;
   int result = TessAPI1.TessBaseAPISetVariable(handle, name, value);
   assertEquals(expResult, result);
 }
Beispiel #2
0
 /** Test of TessBaseAPIGetBoolVariable method, of class TessAPI1. */
 @Test
 public void testTessBaseAPIGetBoolVariable() {
   logger.info("TessBaseAPIGetBoolVariable");
   String name = "tessedit_create_hocr";
   TessAPI1.TessBaseAPISetVariable(handle, name, "1");
   IntBuffer value = IntBuffer.allocate(1);
   int result = -1;
   if (TessAPI1.TessBaseAPIGetBoolVariable(handle, "tessedit_create_hocr", value) == TRUE) {
     result = value.get(0);
   }
   int expResult = 1;
   assertEquals(expResult, result);
 }
Beispiel #3
0
  /**
   * Test of ChoiceIterator.
   *
   * @throws Exception
   */
  @Test
  public void testChoiceIterator() throws Exception {
    logger.info("TessResultIteratorGetChoiceIterator");
    String filename = String.format("%s/%s", this.testResourcesDataPath, "eurotext.tif");
    File tiff = new File(filename);
    BufferedImage image =
        ImageIO.read(new FileInputStream(tiff)); // require jai-imageio lib to read TIFF
    ByteBuffer buf = ImageIOHelper.convertImageData(image);
    int bpp = image.getColorModel().getPixelSize();
    int bytespp = bpp / 8;
    int bytespl = (int) Math.ceil(image.getWidth() * bpp / 8.0);
    TessAPI1.TessBaseAPIInit3(handle, datapath, language);
    TessAPI1.TessBaseAPISetImage(
        handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
    TessAPI1.TessBaseAPISetVariable(handle, "save_blob_choices", "T");
    TessAPI1.TessBaseAPISetRectangle(handle, 37, 228, 548, 31);
    ETEXT_DESC monitor = new ETEXT_DESC();
    ProgressMonitor pmo = new ProgressMonitor(monitor);
    pmo.start();
    TessAPI1.TessBaseAPIRecognize(handle, monitor);
    logger.info("Message: " + pmo.getMessage());
    TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handle);
    int level = TessPageIteratorLevel.RIL_SYMBOL;

    if (ri != null) {
      do {
        Pointer symbol = TessAPI1.TessResultIteratorGetUTF8Text(ri, level);
        float conf = TessAPI1.TessResultIteratorConfidence(ri, level);
        if (symbol != null) {
          logger.info(String.format("symbol %s, conf: %f", symbol.getString(0), conf));
          boolean indent = false;
          TessChoiceIterator ci = TessAPI1.TessResultIteratorGetChoiceIterator(ri);
          do {
            if (indent) {
              System.out.print("\t");
            }
            System.out.print("\t- ");
            String choice = TessAPI1.TessChoiceIteratorGetUTF8Text(ci);
            logger.info(
                String.format("%s conf: %f", choice, TessAPI1.TessChoiceIteratorConfidence(ci)));
            indent = true;
          } while (TessAPI1.TessChoiceIteratorNext(ci) == TessAPI1.TRUE);
          TessAPI1.TessChoiceIteratorDelete(ci);
        }
        logger.info("---------------------------------------------");
        TessAPI1.TessDeleteText(symbol);
      } while (TessAPI1.TessResultIteratorNext(ri, level) == TessAPI1.TRUE);
    }

    assertTrue(true);
  }
Beispiel #4
0
 /**
  * Test of TessBaseAPIPrintVariables method, of class TessAPI1.
  *
  * @throws Exception while persisting variables into a file.
  */
 @Test
 public void testTessBaseAPIPrintVariablesToFile() throws Exception {
   logger.info("TessBaseAPIPrintVariablesToFile");
   String var = "tessedit_char_whitelist";
   String value = "0123456789";
   TessAPI1.TessBaseAPISetVariable(handle, var, value);
   String filename = "printvar.txt";
   TessAPI1.TessBaseAPIPrintVariablesToFile(
       handle, filename); // will crash if not invoked after some method
   File file = new File(filename);
   BufferedReader input = new BufferedReader(new FileReader(file));
   StringBuilder strB = new StringBuilder();
   String line;
   String EOL = System.getProperty("line.separator");
   while ((line = input.readLine()) != null) {
     strB.append(line).append(EOL);
   }
   input.close();
   file.delete();
   assertTrue(strB.toString().contains(var + "\t" + value));
 }