【问题标题】:RuntimeException when trying to use Tess4J in Java EE尝试在 Java EE 中使用 Tess4J 时出现 RuntimeException
【发布时间】:2018-06-24 20:16:51
【问题描述】:

我正在尝试在 Java EE(Payara 服务器)中使用 Tess4J,这可能吗?如果可以,如何?

我得到的确切异常:

e = (net.sourceforge.tess4j.TesseractException) net.sourceforge.tess4j.TesseractException: java.lang.RuntimeException: 需要安装 JAI Image I/O 包。 https://java.net/projects/jai-imageio/

我已将jai-imageio 添加到我的 pom.xml 中,并将其添加到 Payara 的模块中。

文件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>

将 JAR 添加到

`Payara\glassfish\modules`

Tess4J 代码(如果可以对此进行任何改进,我们将不胜感激。

       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);
            }

也尝试了以下 2 个示例。 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);
    }

研究:

找到这个Didnt work / solution

还有didnt work

【问题讨论】:

  • 你有开发部署环境吗?库是否也在部署中安装?
  • 是的,据我所知,您能详细说明我如何准确检查吗?
  • 您是否尝试过使用“Tess4J-3.4.3-src\Tess4J\test\resources\test-data”文件夹中的示例文件?
  • @Daefect91 不,我没有,现在试试,谢谢。
  • @Daefect91 使用测试数据时同样的问题

标签: java maven jakarta-ee tesseract tess4j


【解决方案1】:

由于 JNA RESOURCE_PREFIX 字符串常量不可用导致的运行时异常,Tess4J 以不与 Glassfish 一起工作而闻名。此问题已在最新版本 3.4.9(适用于 Tesseract 3.05.01)和 4.0.2(适用于 Tesseract 4.0.0-beta.1)中得到修复。该库现在可以与 Glassfish 以及 Payara Server 一起使用。

您可能还需要在 OCR 调用之前包含 ImageIO.scanForPlugins(); 语句。这是为了确保适当的ImageReader 可用于读取输入图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多