浏览代码

# test:测试tessdataOCR的使用

yang yi 6 月之前
父节点
当前提交
bf1ac17e61
共有 2 个文件被更改,包括 57 次插入 和 4 次删除
  1. 4 4
      src/main/java/space/anyi/amountVerification/App.java
  2. 53 0
      src/test/java/space/anyi/TessdataTest.java

+ 4 - 4
src/main/java/space/anyi/amountVerification/App.java

@@ -274,10 +274,10 @@ public class App extends Application {
 
     private void startVerification() {
         //如果当前时间大于2025-11-28,则关闭程序
-        //if (System.currentTimeMillis() > 1764341281000L) {
-        //    new Alert(Alert.AlertType.ERROR, "软件已过期,请联系作者获取最新版本").showAndWait();
-        //    Platform.exit();
-        //}
+        if (System.currentTimeMillis() > 1764341281000L) {
+            new Alert(Alert.AlertType.ERROR, "软件已过期,请联系作者获取最新版本").showAndWait();
+            Platform.exit();
+        }
         startButton.setDisable(true);
         String filePath = fileField.getText();
         String folderPath = folderField.getText();

+ 53 - 0
src/test/java/space/anyi/TessdataTest.java

@@ -0,0 +1,53 @@
+package space.anyi;
+
+import net.sourceforge.tess4j.Tesseract;
+import net.sourceforge.tess4j.TesseractException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+/**
+ * @fileName: TessdataTest
+ * @projectName: amount-verification
+ * @package: space.anyi
+ * @author: 杨逸
+ * @date:2026/4/23 16:16
+ * @description:
+ */
+public class TessdataTest {
+    private static Tesseract tesseract;
+    @BeforeAll
+    static void init(){
+        tesseract = new Tesseract();
+        tesseract.setDatapath("tessdata/");
+        tesseract.setLanguage("chi_sim");
+    }
+    @CsvSource({
+            "imgs/a.jpg,逸",
+    })
+    @ParameterizedTest
+    void test(String url, String expect){
+        String result;
+        try {
+            URI uri = Thread.currentThread().getContextClassLoader().getResource(url).toURI();
+            BufferedImage bufferedImage = ImageIO.read(new File(uri));
+            result = tesseract.doOCR(bufferedImage).trim();
+        } catch (TesseractException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } catch (URISyntaxException e) {
+            throw new RuntimeException(e);
+        }
+        Assertions.assertEquals(expect, result);
+        System.out.println(result);
+    }
+}