【Java】tess4j文字识别
windows
可直接使用tess4j最新的api调用(有maven库),直接跳到第三步验证
转载自 http://www.microshell.com/programming/java/performing-optical-character-recognition-in-java/
mac
下载中文训练集
下载地址 所有语言的训练集
此处直接下载 简体中文训练集,然后将其拷贝到我们的
pom依赖
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.1</version> </dependency>
例子
public static void main(String[] args) throws TesseractException {
ITesseract instance = new Tesseract();
// 指定训练数据集合的路径
instance.setDatapath("/Users/js/Desktop/tessdata-main");
// 指定为中文识别
instance.setLanguage("chi_sim");
// 指定识别图片
File imgDir = new File("/Users/js/Desktop/sb.png");
long startTime = System.currentTimeMillis();
String ocrResult = instance.doOCR(imgDir);
// 输出识别结果
System.out.println("OCR Result: \n" + ocrResult + "\n 耗时:" + (System.currentTimeMillis() - startTime) + "ms");
}
启动报错
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'tesseract': dlopen(libtesseract.dylib, 9): image not found dlopen(libtesseract.dylib, 9): image not found Native library (darwin/libtesseract.dylib) not found in resource path ([file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/charsets.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/cldrdata.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/dnsns.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/jaccess.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/jfxrt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/localedata.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/nashorn.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunec.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunjce_provider.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunpkcs11.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/zipfs.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jce.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jfr.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jfxswt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jsse.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/management-agent.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/resources.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/rt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/ant-javafx.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/dt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/javafx-mx.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/jconsole.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/packager.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/sa-jdi.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/tools.jar, file:/Users/js/IdeaProjects/eu%e7%9a%84%e5%89%af%e6%9c%ac/eu-server/eu-ocr/target/classes/, file:/Users/js/.m2/repository/net/sourceforge/tess4j/tess4j/4.5.1/tess4j-4.5.1.jar, file:/Users/js/.m2/repository/net/java/dev/jna/jna/5.5.0/jna-5.5.0.jar, file:/Users/js/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.4.0/jai-imageio-core-1.4.0.jar, file:/Users/js/.m2/repository/org/ghost4j/ghost4j/1.0.1/ghost4j-1.0.1.jar, file:/Users/js/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar, file:/Users/js/.m2/repository/commons-beanutils/commons-beanutils/1.9.2/commons-beanutils-1.9.2.jar, file:/Users/js/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar, file:/Users/js/.m2/repository/org/apache/xmlgraphics/xmlgraphics-commons/1.4/xmlgraphics-commons-1.4.jar, file:/Users/js/.m2/repository/com/lowagie/itext/2.1.7/itext-2.1.7.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox/2.0.18/pdfbox-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/fontbox/2.0.18/fontbox-2.0.18.jar, file:/Users/js/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox-tools/2.0.18/pdfbox-tools-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox-debugger/2.0.18/pdfbox-debugger-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/jbig2-imageio/3.0.3/jbig2-imageio-3.0.3.jar, file:/Users/js/.m2/repository/commons-io/commons-io/2.6/commons-io-2.6.jar, file:/Users/js/.m2/repository/net/sourceforge/lept4j/lept4j/1.13.0/lept4j-1.13.0.jar, file:/Users/js/.m2/repository/org/jboss/jboss-vfs/3.2.15.Final/jboss-vfs-3.2.15.Final.jar, file:/Users/js/.m2/repository/org/jboss/logging/jboss-logging/3.4.1.Final/jboss-logging-3.4.1.Final.jar, file:/Users/js/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar, file:/Users/js/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar, file:/Users/js/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.30/jcl-over-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.30/log4j-over-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter-openfeign/3.0.6/spring-cloud-starter-openfeign-3.0.6.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter/3.0.5/spring-cloud-starter-3.0.5.jar, file:/Users/js/.m2/repository/org/springframework/security/spring-security-rsa/1.0.10.RELEASE/spring-security-rsa-1.0.10.RELEASE.jar, file:/Users/js/.m2/repository/org/bouncycastle/bcpkix-jdk15on/1.68/bcpkix-jdk15on-1.68.jar, file:/Users/js/.m2/repository/org/bouncycastle/bcprov-jdk15on/1.68/bcprov-jdk15on-1.68.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-openfeign-core/3.0.6/spring-cloud-openfeign-core-3.0.6.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.4.2/spring-boot-autoconfigure-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-aop/2.4.2/spring-boot-starter-aop-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.jar, file:/Users/js/.m2/repository/io/github/openfeign/form/feign-form-spring/3.8.0/feign-form-spring-3.8.0.jar, file:/Users/js/.m2/repository/io/github/openfeign/form/feign-form/3.8.0/feign-form-3.8.0.jar, file:/Users/js/.m2/repository/commons-fileupload/commons-fileupload/1.4/commons-fileupload-1.4.jar, file:/Users/js/.m2/repository/org/springframework/spring-web/5.3.3/spring-web-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-commons/3.0.5/spring-cloud-commons-3.0.5.jar, file:/Users/js/.m2/repository/org/springframework/security/spring-security-crypto/5.4.2/spring-security-crypto-5.4.2.jar, file:/Users/js/.m2/repository/io/github/openfeign/feign-core/10.12/feign-core-10.12.jar, file:/Users/js/.m2/repository/io/github/openfeign/feign-slf4j/10.12/feign-slf4j-10.12.jar, file:/Users/js/IdeaProjects/eu%e7%9a%84%e5%89%af%e6%9c%ac/eu-common/eu-common-core/target/classes/, file:/Users/js/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar, file:/Users/js/.m2/repository/io/jsonwebtoken/jjwt/0.9.1/jjwt-0.9.1.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar, file:/Users/js/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar, file:/Users/js/.m2/repository/org/aspectj/aspectjweaver/1.9.6/aspectjweaver-1.9.6.jar, file:/Users/js/.m2/repository/com/belerweb/pinyin4j/2.5.0/pinyin4j-2.5.0.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.4/jackson-datatype-jsr310-2.11.4.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar, file:/Users/js/.m2/repository/com/baomidou/mybatis-plus-annotation/3.4.2/mybatis-plus-annotation-3.4.2.jar, file:/Users/js/.m2/repository/org/springframework/spring-context-support/5.3.3/spring-context-support-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-context/5.3.3/spring-context-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-config/2021.1/spring-cloud-starter-alibaba-nacos-config-2021.1.jar, file:/Users/js/.m2/repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-client/1.4.1/nacos-client-1.4.1.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-common/1.4.1/nacos-common-1.4.1.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpasyncclient/4.1.4/httpasyncclient-4.1.4.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpcore/4.4.14/httpcore-4.4.14.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpcore-nio/4.4.14/httpcore-nio-4.4.14.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-api/1.4.1/nacos-api-1.4.1.jar, file:/Users/js/.m2/repository/commons-codec/commons-codec/1.15/commons-codec-1.15.jar, file:/Users/js/.m2/repository/io/prometheus/simpleclient/0.5.0/simpleclient-0.5.0.jar, file:/Users/js/.m2/repository/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-context/3.0.5/spring-cloud-context-3.0.5.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-discovery/2021.1/spring-cloud-starter-alibaba-nacos-discovery-2021.1.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-alibaba-commons/2021.1/spring-cloud-alibaba-commons-2021.1.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-actuator/2.4.2/spring-boot-starter-actuator-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter/2.4.2/spring-boot-starter-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot/2.4.2/spring-boot-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.4.2/spring-boot-starter-logging-2.4.2.jar, file:/Users/js/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar, file:/Users/js/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar, file:/Users/js/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-actuator-autoconfigure/2.4.2/spring-boot-actuator-autoconfigure-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-actuator/2.4.2/spring-boot-actuator-2.4.2.jar, file:/Users/js/.m2/repository/io/micrometer/micrometer-core/1.6.3/micrometer-core-1.6.3.jar, file:/Users/js/.m2/repository/org/hdrhistogram/HdrHistogram/2.1.12/HdrHistogram-2.1.12.jar, file:/Users/js/.m2/repository/org/latencyutils/LatencyUtils/2.0.3/LatencyUtils-2.0.3.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter-bootstrap/3.0.5/spring-cloud-starter-bootstrap-3.0.5.jar, file:/Users/js/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar, file:/Users/js/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar, file:/Users/js/.m2/repository/cn/hutool/hutool-all/5.6.5/hutool-all-5.6.5.jar, file:/Users/js/.m2/repository/com/google/guava/guava/29.0-jre/guava-29.0-jre.jar, file:/Users/js/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar, file:/Users/js/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar, file:/Users/js/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar, file:/Users/js/.m2/repository/org/checkerframework/checker-qual/2.11.1/checker-qual-2.11.1.jar, file:/Users/js/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar, file:/Users/js/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar, file:/Applications/IntelliJ%20IDEA.app/Contents/lib/idea_rt.jar, file:/Users/js/Library/Caches/JetBrains/IntelliJIdea2021.1/captureAgent/debugger-agent.jar]) at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:302) at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:455) at com.sun.jna.Library$Handler.<init>(Library.java:192) at com.sun.jna.Native.loadLibrary(Native.java:646) at com.sun.jna.Native.loadLibrary(Native.java:630) at net.sourceforge.tess4j.util.LoadLibs.getTessAPIInstance(LoadLibs.java:85) at net.sourceforge.tess4j.TessAPI.<clinit>(TessAPI.java:42) at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:427) at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:223) at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:195) at com.eu.ocr.TextOcr.main(TextOcr.java:25) Suppressed: java.lang.UnsatisfiedLinkError: dlopen(libtesseract.dylib, 9): image not found at com.sun.jna.Native.open(Native Method) at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191) ... 10 more Suppressed: java.lang.UnsatisfiedLinkError: dlopen(libtesseract.dylib, 9): image not found at com.sun.jna.Native.open(Native Method) at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204) ... 10 more Suppressed: java.io.IOException: Native library (darwin/libtesseract.dylib) not found in resource path ([file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/charsets.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/cldrdata.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/dnsns.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/jaccess.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/jfxrt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/localedata.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/nashorn.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunec.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunjce_provider.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/sunpkcs11.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/ext/zipfs.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jce.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jfr.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jfxswt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/jsse.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/management-agent.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/resources.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/jre/lib/rt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/ant-javafx.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/dt.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/javafx-mx.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/jconsole.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/packager.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/sa-jdi.jar, file:/Users/js/Library/Java/JavaVirtualMachines/corretto-1.8.0_292/Contents/Home/lib/tools.jar, file:/Users/js/IdeaProjects/eu%e7%9a%84%e5%89%af%e6%9c%ac/eu-server/eu-ocr/target/classes/, file:/Users/js/.m2/repository/net/sourceforge/tess4j/tess4j/4.5.1/tess4j-4.5.1.jar, file:/Users/js/.m2/repository/net/java/dev/jna/jna/5.5.0/jna-5.5.0.jar, file:/Users/js/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.4.0/jai-imageio-core-1.4.0.jar, file:/Users/js/.m2/repository/org/ghost4j/ghost4j/1.0.1/ghost4j-1.0.1.jar, file:/Users/js/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar, file:/Users/js/.m2/repository/commons-beanutils/commons-beanutils/1.9.2/commons-beanutils-1.9.2.jar, file:/Users/js/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar, file:/Users/js/.m2/repository/org/apache/xmlgraphics/xmlgraphics-commons/1.4/xmlgraphics-commons-1.4.jar, file:/Users/js/.m2/repository/com/lowagie/itext/2.1.7/itext-2.1.7.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox/2.0.18/pdfbox-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/fontbox/2.0.18/fontbox-2.0.18.jar, file:/Users/js/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox-tools/2.0.18/pdfbox-tools-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/pdfbox-debugger/2.0.18/pdfbox-debugger-2.0.18.jar, file:/Users/js/.m2/repository/org/apache/pdfbox/jbig2-imageio/3.0.3/jbig2-imageio-3.0.3.jar, file:/Users/js/.m2/repository/commons-io/commons-io/2.6/commons-io-2.6.jar, file:/Users/js/.m2/repository/net/sourceforge/lept4j/lept4j/1.13.0/lept4j-1.13.0.jar, file:/Users/js/.m2/repository/org/jboss/jboss-vfs/3.2.15.Final/jboss-vfs-3.2.15.Final.jar, file:/Users/js/.m2/repository/org/jboss/logging/jboss-logging/3.4.1.Final/jboss-logging-3.4.1.Final.jar, file:/Users/js/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar, file:/Users/js/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar, file:/Users/js/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.30/jcl-over-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.30/log4j-over-slf4j-1.7.30.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter-openfeign/3.0.6/spring-cloud-starter-openfeign-3.0.6.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter/3.0.5/spring-cloud-starter-3.0.5.jar, file:/Users/js/.m2/repository/org/springframework/security/spring-security-rsa/1.0.10.RELEASE/spring-security-rsa-1.0.10.RELEASE.jar, file:/Users/js/.m2/repository/org/bouncycastle/bcpkix-jdk15on/1.68/bcpkix-jdk15on-1.68.jar, file:/Users/js/.m2/repository/org/bouncycastle/bcprov-jdk15on/1.68/bcprov-jdk15on-1.68.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-openfeign-core/3.0.6/spring-cloud-openfeign-core-3.0.6.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.4.2/spring-boot-autoconfigure-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-aop/2.4.2/spring-boot-starter-aop-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.jar, file:/Users/js/.m2/repository/io/github/openfeign/form/feign-form-spring/3.8.0/feign-form-spring-3.8.0.jar, file:/Users/js/.m2/repository/io/github/openfeign/form/feign-form/3.8.0/feign-form-3.8.0.jar, file:/Users/js/.m2/repository/commons-fileupload/commons-fileupload/1.4/commons-fileupload-1.4.jar, file:/Users/js/.m2/repository/org/springframework/spring-web/5.3.3/spring-web-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-commons/3.0.5/spring-cloud-commons-3.0.5.jar, file:/Users/js/.m2/repository/org/springframework/security/spring-security-crypto/5.4.2/spring-security-crypto-5.4.2.jar, file:/Users/js/.m2/repository/io/github/openfeign/feign-core/10.12/feign-core-10.12.jar, file:/Users/js/.m2/repository/io/github/openfeign/feign-slf4j/10.12/feign-slf4j-10.12.jar, file:/Users/js/IdeaProjects/eu%e7%9a%84%e5%89%af%e6%9c%ac/eu-common/eu-common-core/target/classes/, file:/Users/js/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar, file:/Users/js/.m2/repository/io/jsonwebtoken/jjwt/0.9.1/jjwt-0.9.1.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar, file:/Users/js/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar, file:/Users/js/.m2/repository/org/aspectj/aspectjweaver/1.9.6/aspectjweaver-1.9.6.jar, file:/Users/js/.m2/repository/com/belerweb/pinyin4j/2.5.0/pinyin4j-2.5.0.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.4/jackson-datatype-jsr310-2.11.4.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar, file:/Users/js/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar, file:/Users/js/.m2/repository/com/baomidou/mybatis-plus-annotation/3.4.2/mybatis-plus-annotation-3.4.2.jar, file:/Users/js/.m2/repository/org/springframework/spring-context-support/5.3.3/spring-context-support-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-context/5.3.3/spring-context-5.3.3.jar, file:/Users/js/.m2/repository/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-config/2021.1/spring-cloud-starter-alibaba-nacos-config-2021.1.jar, file:/Users/js/.m2/repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-client/1.4.1/nacos-client-1.4.1.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-common/1.4.1/nacos-common-1.4.1.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpasyncclient/4.1.4/httpasyncclient-4.1.4.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpcore/4.4.14/httpcore-4.4.14.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpcore-nio/4.4.14/httpcore-nio-4.4.14.jar, file:/Users/js/.m2/repository/org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar, file:/Users/js/.m2/repository/com/alibaba/nacos/nacos-api/1.4.1/nacos-api-1.4.1.jar, file:/Users/js/.m2/repository/commons-codec/commons-codec/1.15/commons-codec-1.15.jar, file:/Users/js/.m2/repository/io/prometheus/simpleclient/0.5.0/simpleclient-0.5.0.jar, file:/Users/js/.m2/repository/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-context/3.0.5/spring-cloud-context-3.0.5.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-discovery/2021.1/spring-cloud-starter-alibaba-nacos-discovery-2021.1.jar, file:/Users/js/.m2/repository/com/alibaba/cloud/spring-cloud-alibaba-commons/2021.1/spring-cloud-alibaba-commons-2021.1.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-actuator/2.4.2/spring-boot-starter-actuator-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter/2.4.2/spring-boot-starter-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot/2.4.2/spring-boot-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.4.2/spring-boot-starter-logging-2.4.2.jar, file:/Users/js/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar, file:/Users/js/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar, file:/Users/js/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-actuator-autoconfigure/2.4.2/spring-boot-actuator-autoconfigure-2.4.2.jar, file:/Users/js/.m2/repository/org/springframework/boot/spring-boot-actuator/2.4.2/spring-boot-actuator-2.4.2.jar, file:/Users/js/.m2/repository/io/micrometer/micrometer-core/1.6.3/micrometer-core-1.6.3.jar, file:/Users/js/.m2/repository/org/hdrhistogram/HdrHistogram/2.1.12/HdrHistogram-2.1.12.jar, file:/Users/js/.m2/repository/org/latencyutils/LatencyUtils/2.0.3/LatencyUtils-2.0.3.jar, file:/Users/js/.m2/repository/org/springframework/cloud/spring-cloud-starter-bootstrap/3.0.5/spring-cloud-starter-bootstrap-3.0.5.jar, file:/Users/js/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar, file:/Users/js/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar, file:/Users/js/.m2/repository/cn/hutool/hutool-all/5.6.5/hutool-all-5.6.5.jar, file:/Users/js/.m2/repository/com/google/guava/guava/29.0-jre/guava-29.0-jre.jar, file:/Users/js/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar, file:/Users/js/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar, file:/Users/js/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar, file:/Users/js/.m2/repository/org/checkerframework/checker-qual/2.11.1/checker-qual-2.11.1.jar, file:/Users/js/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar, file:/Users/js/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar, file:/Applications/IntelliJ%20IDEA.app/Contents/lib/idea_rt.jar, file:/Users/js/Library/Caches/JetBrains/IntelliJIdea2021.1/captureAgent/debugger-agent.jar]) at com.sun.jna.Native.extractFromResourcePath(Native.java:1095) at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:276) ... 10 more
原因是mac jar加载不了dylib依赖。
可手动将dylib依赖打包进jar
通过macports安装Tesseract
- sudo port install Tesseract
将libtesseract.dylib加入jar包
libtesseract.dylib可通过find / -name 查找位置
- mkdir darwin
- jar uf tess4j-xxx.jar darwin
- cp libtesseract.4.dylib darwin/libtesseract.dylib
- jar uf tess4j-xxx.jar darwin/libtesseract.dylib
- jar tf tess4j-xxx.jar
pom依赖本地jar
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.1</version> <scope>system</scope> <systemPath>${basedir}/src/main/resources/jar/tess4j-4.5.1.jar</systemPath> </dependency>
启动报错
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Structure
at com.eu.ocr.TextOcr.main(TextOcr.java:16)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Structure
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 1 more
解决:将Tess4j文件下的lib下的jar添加项目依赖
识别成功