gpt4 book ai didi

java - 创建 TessBaseAPI 时出现 UnsatisfiedLinkError : no jnilept in java. library.path

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:42 28 4
gpt4 key购买 nike

我是 java cpp 和 tesseract-ocr 的新手。我在几个小时内遇到了一个问题。我在创建 TessBaseAPI 时收到 UnsatisfiedLinkError: no jnilept in java.library.path。下面是我的代码片段。

public static void tesseractForPdf(String filePath) throws Exception {
BytePointer outText;

TessBaseAPI api = new TessBaseAPI();//getting the UnsatisfiedLinkError exception here.
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(".", "ENG") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}

// Open input image with leptonica library
PIX image = pixRead(filePath);
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
String string = outText.getString();
System.out.println("OCR output:\n" + string);

// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}

异常我正在使用 TessBaseAPI api = new TessBaseAPI();行

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnilept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:500)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.lept.<clinit>(lept.java:10)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:472)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.tesseract$TessBaseAPI.<clinit>(tesseract.java:3648)
at om.practiceproblems.BasicTesseractExampleTest.givenTessBaseApi_whenImageOcrd_thenTextDisplayed(BasicTesseractExampleTest.java:35)
at com.practiceproblems.BasicTesseractExampleTest.main(BasicTesseractExampleTest.java:22)
Caused by: java.lang.UnsatisfiedLinkError: no liblept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:491)
... 9 more

在我的示例中,我使用 java 预设库 tesseract-3.04.01-1.2 和 leptonica-1.73-1.2.jar 以及 javacpp-1.2.1。我有 Windows 操作系统。 我确实看到了这个https://github.com/bytedeco/javacpp-presets/issues/46以及关于 SO 和 github 的一些讨论指出这个问题在 jacacpp-1.1 本身中已修复。但我使用的是 javacpp1.2。
如果能帮助解决问题或找到根本原因,我将不胜感激。

最佳答案

您可以克隆或下载该项目:

https://github.com/bytedeco/javacpp-presets#the-cppbuildsh-scripts

然后构建模块:Tesseract 的 JavaCPP 预设和 Leptonica 的 JavaCPP 预设;

(要构建 leptonica 项目,您可能需要安装 nasm https://www.nasm.us/ )

(要构建整个 javacpp-presets 项目,您还必须安装 cmake)

这将创建原生库:

libjnilept.so 和 libjnitesseract.so

然后你必须指定jni.library.path

你可以这样做:

System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
/* Optionally add these two lines */
System.setProperty("jna.library.path", tmpDirName);
System.setProperty("jni.library.path", tmpDirName);
final Field fieldSysPath;

fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);

fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);

(您可以改为在虚拟机选项上指定 -Djava.library.path=)

你只需要把生成的文件:libjnilept.so 和 libjnitesseract.so 在某个文件夹中并设置此路径:jni.library.path

<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract</artifactId>
<version>4.0.0-1.4.4</version>
</dependency>

<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica</artifactId>
<version>1.77.0-1.4.4</version>
</dependency>

你也可以尝试添加

<dependency>    
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica-platform</artifactId>
<version>1.77.0-1.4.4</version>
</dependency>

<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-platform</artifactId>
<version>4.0.0-1.4.4</version>
</dependency>

并在构建中添加一个 maven-assembly-plugin

<build>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!-- new -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>


此外,您还可能会遇到这样的错误:

sscanf(line, "%" QUOTED_TOKENSIZE "s %" QUOTED_TOKENSIZE "s %f %f",
linear_token, essential_token, &ParamDesc[i].Min, &ParamDesc[i].Max) == 4
:Error:Assert failed:in file clusttool.cpp, line 73
#
# A fatal error has been detected by the Java Runtime Environment:

Due to Tesseract's locale requirements, export LC_ALL=C is required before running any client programs.

所以:

       <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<environmentVariables>
<LC_ALL>C</LC_ALL>
</environmentVariables>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>${classpath}</argument>
</arguments>
</configuration>
</plugin>

source:
    - https://github.com/nguyenq/tess4j/issues/106
    - https://github.com/sirfz/tesserocr/issues/165

关于java - 创建 TessBaseAPI 时出现 UnsatisfiedLinkError : no jnilept in java. library.path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39435993/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com