net.sourceforge.tess4j.TesseractException Java Examples

The following examples show how to use net.sourceforge.tess4j.TesseractException. 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 check out the related API usage on the sidebar.
Example #1
Source File: TextRecognizer.java    From SikuliX1 with 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 File: Tess4JExample.java    From tutorials with 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 File: Tess4jV1.java    From ocr-tess4j-rest with 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 File: Tess4jV1.java    From ocr-tess4j-rest with 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 File: TessOcr.java    From MillionHero with 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 File: OCR.java    From justtestlah with 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 File: TessrJExample.java    From Java-for-Data-Science with 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 File: Tess4jUtil.java    From javautils with 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 File: TextFinder.java    From SikuliNG with 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 File: OCRTest.java    From justtestlah with 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");
}