Ian*_*Ian 5 java tesseract executable-jar maven tess4j
该项目的最终目标是获取 jar 并将其放入使用 tesseract 的目录中,并输出结果目录和输出 txt 文件。不过,我在使用超正方体时遇到了一些问题。我正在 Java 中使用 tess4j 和 Maven,我想将我的代码放入可执行 jar 中。该项目作为桌面应用程序运行良好,但每当我尝试使用java -jar fileName.jar(导出到 jar 后)运行时,它都会给我错误
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory
Failed loading language 'eng'
...
Run Code Online (Sandbox Code Playgroud)
我在网上查找,但无法真正找到如何为 jar 设置超正方体并获得正确的路径。现在我使用 maven 并在我的 pom 文件(tess4j -v 3.0)中有 Tesseract 依赖项,并且我的项目中有 tessdata。
我对 Maven 和 jar 文件相当陌生,以前从未使用过 tesseract,但据我从互联网上可以看出,我设置正确。
有谁知道如何使 tess4j 指向我项目中的 tessdata 目录并拥有动态路径,以便我可以在多台计算机和位置上移动使用它?
这就是我所说的 Tesseract
Tesseract instance = new Tesseract();
instance.setDatapath("src/main/resources");
String result = instance.doOCR(imageFile);
String fileName = imageFile.getName().replace(".jpg", "");
System.out.println("Parsed Image " + fileName);
return result;
Run Code Online (Sandbox Code Playgroud)
编辑
这就是我尝试在代码中设置环境变量 TESSDATA_PREFIX 的方法
String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir);
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
env.put("TESSDATA_PREFIX", dir + "\\tessdata");
Process p = pb.start();
Run Code Online (Sandbox Code Playgroud)
但这并没有产生明显的效果。我仍然遇到同样的错误
编辑2
根据错误消息我需要将其设置为tessdata的父目录,我也尝试过这个无济于事
编辑3
经过大量搜索并尝试修复它后,我不确定这是否可能。tesseract 中的 doOcr 方法接受缓冲图像或文件,如果我的图像不是动态的,那么我无法真正将它们存储在 jar 中,那么这就没问题。更不用说 TESSDATA_PREFIX 仍然不会设置。如果有人有任何想法,我会洗耳恭听,我会继续寻找解决方案,但我不确定它是否会起作用
It randomly started working when I
put the tessdata folder in the same directory as my jar
changed the setDatapath to the following
Tesseract instance = new Tesseract();
instance.setDatapath(".");
String result = instance.doOCR(imageFile);
String fileName = imageFile.getName().replace(".jpg", "");
System.out.println("Parsed Image " + fileName);
return result;
Run Code Online (Sandbox Code Playgroud)and 3. I exported from eclipse by right clicking the project, selecting java -> runnable jar, then setting the option "Extract Required Libraries into Generated Jars".
(side note, the environment setting like I was doing early does not need to be in the project anymore)
I really thought I tried this but i guess something must have been wrong. I removed tessdata from my project and will have to include that wherever the jar is run. Im not really sure why it started working but im glad it did
| 归档时间: |
|
| 查看次数: |
8514 次 |
| 最近记录: |