Im trying to use Tess4J in Java EE (Payara server), is this possible and if so how?
Exact Exception I'm getting:
e = (net.sourceforge.tess4j.TesseractException) net.sourceforge.tess4j.TesseractException: java.lang.RuntimeException: Need to install JAI Image I/O package. https://java.net/projects/jai-imageio/
I have added the jai-imageio
to my pom.xml, as well as added it to the modules of Payara.
File pom.xml
<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.4.1</version> <!-- used 3.4.2 as well -->
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version>
<scope>runtime</scope> <!-- tried without this as well -->
</dependency>
Added JAR to
`Payara\glassfish\modules`
Tess4J code (If any improvements can be made to this as well it would be appreciated).
ITesseract instance = new Tesseract();
instance.setDatapath(pLangaugePath); // C:\\t
instance.setLanguage(pLanguage); // eng
try {
File[] tifFiles = PdfUtilities.convertPdf2Png(pFile);
if (tifFiles != null) {
for (File tifFile : tifFiles) {
String ocrText = instance.doOCR(tifFile);
if (StringUtils.isNotBlank(ocrText)) {
ret.append(ocrText);
}
}
}
} catch (TesseractException e) {
LOG.error("Could not do ocr on image file created via pdf ", e);
}
Have tried the following 2 examples as well. 1.
try (PDDocument document = PDDocument.load(pFile)) {
int totalPages = document.getNumberOfPages();
PDFRenderer renderer = new PDFRenderer(document);
for (int pi = 0; pi < totalPages; pi++) {
BufferedImage image = renderer.renderImageWithDPI(pi, 75);
String ocrText = instance.doOCR(image);
if (StringUtils.isNotBlank(ocrText)) {
ret.append(ocrText);
}
}
} catch (Exception e) {
LOG.error("Could not do ocr on pdf", e);
}
2.
try {
ITesseract instance = new Tesseract();
instance.setDatapath(pLangaugePath); // C:\\t
instance.setLanguage(pLanguage); // eng
String ocrText = instance.doOCR(pFile);
if (StringUtils.isNotBlank(ocrText)) {
ret.append(ocrText);
}
} catch (Exception e) {
LOG.error("Could not do ocr on image file created via pdf ", e);
}
Research:
Found this Didnt work / solution
as well as didnt work