Pārlūkot izejas kodu

test-feat:OCR识别图像中的文本,通过tess4j调用tessdata进行识别;加入tess4j依赖

yang yi 11 mēneši atpakaļ
vecāks
revīzija
e073b1b132

+ 5 - 0
pom.xml

@@ -36,6 +36,11 @@
             <artifactId>javafx-controls</artifactId>
             <version>21.0.1</version>
         </dependency>
+        <dependency>
+            <groupId>net.sourceforge.tess4j</groupId>
+            <artifactId>tess4j</artifactId>
+            <version>5.7.0</version>
+        </dependency>
     </dependencies>
 
     <properties>

+ 40 - 0
src/main/java/space/anyi/amountVerification/MainServer.java

@@ -0,0 +1,40 @@
+package space.anyi.amountVerification;
+
+/**
+ * @ProjectName: amount-verification
+ * @FileName: MainServer
+ * @Author: 杨逸
+ * @Data:2025/11/28 21:11
+ * @Description:
+ */
+
+import net.sourceforge.tess4j.Tesseract;
+import net.sourceforge.tess4j.TesseractException;
+import java.io.File;
+
+public class MainServer {
+    public static void main(String[] args) throws TesseractException {
+        long start = System.currentTimeMillis();
+        System.out.println("开始OCR文字识图,请稍后...");
+        //加载要识别的图片
+        File image = new File("C:\\Users\\杨逸\\Documents\\amount-verification\\src\\main\\resources\\1.png");
+        //设置配置文件夹微视、识别语言、识别模式
+        Tesseract tesseract = new Tesseract();
+        //File tessdataFolder = new File("classpath:tessdata");
+        //tesseract.setDatapath("D:\\tools\\tessdata\\best");
+        tesseract.setDatapath("D:\\tools\\tessdata\\fast");
+        //设置识别语言为中文简体,(如果要设置为英文可改为"eng")
+        tesseract.setLanguage("chi_sim");
+        //使用 OSD 进行自动页面分割以进行图像处理
+        //tesseract.setPageSegMode(1);
+        //设置引擎模式是神经网络LSTM引擎
+        //tesseract.setOcrEngineMode(1);
+        //开始识别整张图片中的文字
+        String result = tesseract.doOCR(image);
+        long time = System.currentTimeMillis()-start;
+        System.out.println("识别结束,耗时:"+time+" 毫秒,识别结果如下:");
+        System.out.println();
+        System.out.println(result);
+    }
+}
+

BIN
src/main/resources/tessdata/chi_sim.traineddata