Java Code Examples for net.sourceforge.tess4j.TesseractException

The following examples show how to use net.sourceforge.tess4j.TesseractException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: SikuliX1   Source File: TextRecognizer.java    License: MIT License 6 votes vote down vote up
protected <SFIRBS> String doRead(SFIRBS from) {
  try {
    String text = "";
    if (from instanceof Mat) {
      Mat img = ((Mat) from).clone();
      if (!img.empty()) {
        img = SXOpenCV.optimize(img, options.factor(), options.resizeInterpolation());
        byte[] bytes = new byte[img.width() * img.height()];
        int n = img.get(0, 0, bytes);
        text = getTesseractAPI().doOCR(img.width(), img.height(), ByteBuffer.wrap(bytes), null, 8);
      } else {
        return "";
      }
    } else {
      BufferedImage bimg = SXOpenCV.optimize(Element.getBufferedImage(from), options.factor(), options.resizeInterpolation());
      text = getTesseractAPI().doOCR(bimg);
    }
    return text.trim().replace("\n\n", "\n");
  } catch (TesseractException e) {
    Debug.error("OCR: read: Tess4J: doOCR: %s", e.getMessage());
    return "";
  }
}
 
Example 2
Source Project: tutorials   Source File: Tess4JExample.java    License: MIT License 6 votes vote down vote up
public static void main(String[] args) {
    String result = null;
    try {
        File image = new File("src/main/resources/images/baeldung.png");
        Tesseract tesseract = new Tesseract();
        tesseract.setLanguage("spa");
        tesseract.setPageSegMode(1);
        tesseract.setOcrEngineMode(1);
        tesseract.setHocr(true);
        tesseract.setDatapath("src/main/resources/tessdata");
        result = tesseract.doOCR(image, new Rectangle(1200, 200));
    } catch (TesseractException e) {
        e.printStackTrace();
    }
    System.out.println(result);
}
 
Example 3
Source Project: ocr-tess4j-rest   Source File: Tess4jV1.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "ocr/v1/convert", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Text convertImageToText(@RequestBody final Image image) throws Exception {

    File tmpFile = File.createTempFile("ocr_image", image.getExtension());
    try {
        FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
        Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
        String imageText = tesseract.doOCR(tmpFile);
        LOGGER.debug("OCR Image Text = " + imageText);
        return new Text(imageText);
    } catch (Exception e) {
        LOGGER.error("Exception while converting/uploading image: ", e);
        throw new TesseractException();
    } finally {
        tmpFile.delete();
    }
}
 
Example 4
Source Project: ocr-tess4j-rest   Source File: Tess4jV1.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "ocr/v1/upload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Status doOcr(@RequestBody Image image) throws Exception {
    try {
        //FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(image.getImage()));
        Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
        String imageText = tesseract.doOCR(ImageIO.read(bis));
        image.setText(imageText);
        repository.save(image);
        LOGGER.debug("OCR Result = " + imageText);
    } catch (Exception e) {
        LOGGER.error("TessearctException while converting/uploading image: ", e);
        throw new TesseractException();
    }

    return new Status("success");
}
 
Example 5
Source Project: MillionHero   Source File: TessOcr.java    License: MIT License 5 votes vote down vote up
@Override
public String getOCR(File file) {
    Long start = System.currentTimeMillis();
    String result = null;
    try {
        result = instance.doOCR(file);
    } catch (TesseractException e) {
        System.err.println("tessOCR提取图片文字信息失败");
    }
    float time=(System.currentTimeMillis()-start)/(1000f);
    System.out.println("tessOCR提取信息成功,耗时:"+time+"s");
    return result;
}
 
Example 6
Source Project: justtestlah   Source File: OCR.java    License: Apache License 2.0 5 votes vote down vote up
private String getText(File file) {
  LOG.info("Peforming OCR on file {}", file);
  try {
    return ocr.doOCR(file).trim();
  } catch (TesseractException exception) {
    LOG.warn("Error performing OCR", exception);
    return null;
  }
}
 
Example 7
Source Project: Java-for-Data-Science   Source File: TessrJExample.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
    ITesseract instance = new Tesseract();
    instance.setLanguage("eng");
    try {
        String result;
        result = instance.doOCR(new File("OCRExample.png"));
        System.out.println(result);
    } catch (TesseractException e) {
        System.err.println(e.getMessage());
    }
}
 
Example 8
Source Project: javautils   Source File: Tess4jUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 识别图片文件中的文字
 * @param instance
 * @param imageFile
 * @return
 */
private static String getOCRText(ITesseract instance, File imageFile){
    String result = null;
    try {
        result = instance.doOCR(imageFile);
    } catch (TesseractException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 9
Source Project: SikuliNG   Source File: TextFinder.java    License: MIT License 5 votes vote down vote up
public String read(Picture picture) {
  if (valid) {
    try {
      return tess.doOCR(picture.get());
    } catch (TesseractException e) {
      log.error("read: %s", e.getMessage());
    }
  } else {
    log.error("read: TextFinder not valid");
  }
  return "did not work";
}
 
Example 10
Source Project: justtestlah   Source File: OCRTest.java    License: Apache License 2.0 4 votes vote down vote up
public void helloWorldTest() throws TesseractException {
  TakesScreenshot driver = mock(TakesScreenshot.class);
  target = new OCR(new Tesseract()).withDriver(driver);
  when(driver.getScreenshotAs(OutputType.FILE)).thenReturn(getPath("helloworld.png"));
  assertThat(target.getText().trim()).isEqualTo("hello world");
}