Java 验证码识别(Tess4J初体验)「建议收藏」

2022-08-13 11:00:06 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

Tess4J官方描述:A Java JNA wrapper for Tesseract OCR API.

demo下载

1.先去官网下载:http://tess4j.sourceforge.net/

2.将下载的文件解压后把下面几个文件夹(图片中选中的)复制到新建的项目中

3.将lib下的jar包加到build path 中。注意:lib里面除了jar包还有别的。

4.根据官网的样例在刚建的项目中使用一下:

The following code example shows common usage of the library. Make sure tessdata folder are in the search path, and the .jar files are in the classpath.注意在第4步之前确保tessdata 文件夹在项目中,jar包在classpath中。前面的2,3两步已经做了。

[java] view plain copy

  1. package net.sourceforge.tess4j.example;
  2. import java.io.File;
  3. import net.sourceforge.tess4j.*;
  4. public class TesseractExample {
  5. public static void main(String[] args) {
  6. File imageFile = new File(“eurotext.tif”);
  7. ITesseract instance = new Tesseract(); // JNA Interface Mapping
  8. // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
  9. try {
  10. String result = instance.doOCR(imageFile);
  11. System.out.println(result);
  12. } catch (TesseractException e) {
  13. System.err.println(e.getMessage());
  14. }
  15. }
  16. }

我稍微改了一下,识别指定文件夹下所有验证码

[java] view plain copy

  1. package blog.csdn.net.dr_guo;
  2. import java.io.File;
  3. import net.sourceforge.tess4j.ITesseract;
  4. import net.sourceforge.tess4j.Tesseract;
  5. import net.sourceforge.tess4j.TesseractException;
  6. /**
  7. * 验证码识别(图片名即为验证码数字)
  8. * @author drguo
  9. *
  10. */
  11. public class VCR {
  12. public static void main(String[] args) {
  13. File root = new File(System.getProperty(“user.dir”) “/imgs”);
  14. ITesseract instance = new Tesseract();
  15. try {
  16. File[] files = root.listFiles();
  17. for (File file : files) {
  18. String result = instance.doOCR(file);
  19. String fileName = file.toString().substring(file.toString().lastIndexOf(“\”) 1);
  20. System.out.println(“图片名:” fileName ” 识别结果:” result);
  21. }
  22. } catch (TesseractException e) {
  23. System.err.println(e.getMessage());
  24. }
  25. }
  26. }

直接运行就行了,但这时可能会报错

[plain] view plain copy

  1. Exception in thread “main” java.lang.UnsatisfiedLinkError: Unable to load library ‘libtesseract304’: Native library (win32-x86-64/libtesseract304.dll) not found in resource path ([file:/G:/Eclipse/Demo/bin/, file:/G:/Eclipse/Demo/lib/commons-beanutils-1.9.2.jar, file:/G:/Eclipse/Demo/lib/commons-io-2.4.jar, file:/G:/Eclipse/Demo/lib/commons-logging-1.2.jar, file:/G:/Eclipse/Demo/lib/ghost4j-1.0.1.jar, file:/G:/Eclipse/Demo/lib/hamcrest-core-1.3.jar, file:/G:/Eclipse/Demo/lib/itext-2.1.7.jar, file:/G:/Eclipse/Demo/lib/jai-imageio-core-1.3.1.jar, file:/G:/Eclipse/Demo/lib/jna-4.2.2.jar, file:/G:/Eclipse/Demo/lib/jul-to-slf4j-1.7.19.jar, file:/G:/Eclipse/Demo/lib/junit-4.12.jar, file:/G:/Eclipse/Demo/lib/lept4j-1.1.2.jar, file:/G:/Eclipse/Demo/lib/log4j-1.2.17.jar, file:/G:/Eclipse/Demo/lib/logback-classic-1.1.6.jar, file:/G:/Eclipse/Demo/lib/logback-core-1.1.6.jar, file:/G:/Eclipse/Demo/lib/rococoa-core-0.5.jar, file:/G:/Eclipse/Demo/lib/slf4j-api-1.7.19.jar, file:/G:/Eclipse/Demo/lib/xmlgraphics-commons-1.5.jar])

注意前面的报错信息,把lib下的win32-x86-64拷到项目中的bin目录下就可以了

准确率还是挺高的。

如果你还觉得麻烦,直接把我的项目下载下来改吧:https://git.oschina.net/drguo/VerificationCodeRecognition.git

注意我的jdk版本是jdk1.8.0_74,如果你的版本低于我的版本可能会报错~

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132599.html原文链接:https://javaforall.cn

0 人点赞