コード例 #1
0
ファイル: TessAPI1Test.java プロジェクト: zhaojianping/tess4j
 @After
 public void tearDown() {
   TessAPI1.TessBaseAPIDelete(handle);
 }
コード例 #2
0
ファイル: TessAPI1Test.java プロジェクト: zhaojianping/tess4j
  /**
   * Test of ResultRenderer method, of class TessAPI1.
   *
   * @throws java.lang.Exception
   */
  @Test
  public void testResultRenderer() throws Exception {
    logger.info("TessResultRenderer");
    String image = String.format("%s/%s", this.testResourcesDataPath, "eurotext.tif");
    String output = "capi-test.txt";
    int set_only_init_params = TessAPI.FALSE;
    int oem = TessOcrEngineMode.OEM_DEFAULT;
    PointerByReference configs = null;
    int configs_size = 0;

    String[] params = {"load_system_dawg", "tessedit_char_whitelist"};
    String vals[] = {"F", ""}; // 0123456789-.IThisalotfpnex
    PointerByReference vars_vec = new PointerByReference();
    vars_vec.setPointer(new StringArray(params));
    PointerByReference vars_values = new PointerByReference();
    vars_values.setPointer(new StringArray(vals));
    NativeSize vars_vec_size = new NativeSize(params.length);

    TessAPI1.TessBaseAPISetOutputName(handle, output);

    int rc =
        TessAPI1.TessBaseAPIInit4(
            handle,
            datapath,
            language,
            oem,
            configs,
            configs_size,
            vars_vec,
            vars_values,
            vars_vec_size,
            set_only_init_params);

    if (rc != 0) {
      TessAPI1.TessBaseAPIDelete(handle);
      logger.error("Could not initialize tesseract.");
      return;
    }

    String outputbase = "target/test-classes/test-results/outputbase1";
    TessResultRenderer renderer = TessAPI1.TessHOcrRendererCreate(outputbase);
    TessAPI1.TessResultRendererInsert(renderer, TessAPI1.TessBoxTextRendererCreate(outputbase));
    TessAPI1.TessResultRendererInsert(renderer, TessAPI1.TessTextRendererCreate(outputbase));
    String dataPath = TessAPI1.TessBaseAPIGetDatapath(handle);
    TessAPI1.TessResultRendererInsert(
        renderer, TessAPI1.TessPDFRendererCreate(outputbase, dataPath));
    TessAPI1.TessResultRendererBeginDocument(renderer, image);
    int result = TessAPI1.TessBaseAPIProcessPages(handle, image, null, 0, renderer);
    TessAPI1.TessResultRendererEndDocument(renderer);

    //        if (result == FALSE) {
    //            logger.error("Error during processing.");
    //            return;
    //        }
    for (; renderer != null; renderer = TessAPI1.TessResultRendererNext(renderer)) {
      String ext = TessAPI1.TessResultRendererExtention(renderer).getString(0);
      logger.info(
          String.format(
              "TessResultRendererExtention: %s\nTessResultRendererTitle: %s\nTessResultRendererImageNum: %d",
              ext,
              TessAPI1.TessResultRendererTitle(renderer).getString(0),
              TessAPI1.TessResultRendererImageNum(renderer)));
    }

    TessAPI1.TessDeleteResultRenderer(renderer);
    assertTrue(new File(outputbase + ".pdf").exists());
  }