RuntimeException when trying to use Tess4J in Java EE

≡放荡痞女 提交于 2019-12-10 22:36:17

问题


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


回答1:


Tess4J was known for not working with Glassfish due to the run-time exception caused by the unavailability of JNA RESOURCE_PREFIX string constant. This issue has been fixed in the latest releases 3.4.9 (for Tesseract 3.05.01) and 4.0.2 (for Tesseract 4.0.0-beta.1). The library can now be used with Glassfish, and perhaps Payara Server.

You may also need to include ImageIO.scanForPlugins(); statement before the OCR call. That is meant to ensure the appropriate ImageReader be available to read input images.



来源:https://stackoverflow.com/questions/48275883/runtimeexception-when-trying-to-use-tess4j-in-java-ee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!