Ver Fonte

# test:测试RapidOCR的使用

yang yi há 6 meses atrás
pai
commit
7e7601274f
2 ficheiros alterados com 75 adições e 0 exclusões
  1. 6 0
      pom.xml
  2. 69 0
      src/test/java/space/anyi/RapidOCRTest.java

+ 6 - 0
pom.xml

@@ -88,6 +88,12 @@
         <!--    <artifactId>rapidocr-ncnn-platform</artifactId>-->
         <!--    <version>0.0.7</version>-->
         <!--</dependency>-->
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>5.12.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <properties>

+ 69 - 0
src/test/java/space/anyi/RapidOCRTest.java

@@ -0,0 +1,69 @@
+package space.anyi;
+
+import com.benjaminwan.ocrlibrary.OcrInput;
+import com.benjaminwan.ocrlibrary.OcrResult;
+import io.github.mymonstercat.Model;
+import io.github.mymonstercat.ocr.InferenceEngine;
+import io.github.mymonstercat.ocr.config.ParamConfig;
+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 java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+/**
+ * @fileName: RapidOCRTest
+ * @projectName: amount-verification
+ * @package: space.anyi
+ * @author: 杨逸
+ * @date:2026/4/23 15:35
+ * @description:
+ */
+
+public class RapidOCRTest {
+    private static InferenceEngine engine;
+    @BeforeAll
+    static void init(){
+        System.setProperty("java.io.tmpdir", System.getenv("user.dir") + "\\temp");
+        engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
+    }
+    //参数测试,输入"imgs/a.jpg",预期输出"逸"
+    @CsvSource({"imgs/a.jpg,逸"})
+    @ParameterizedTest()
+    void test(String fileName, String expected) {
+        String result;
+        try {
+            //获取资源路径下的图片文件
+            URI uri = Thread.currentThread().getContextClassLoader().getResource(fileName).toURI();
+            String imgUrl = new File(uri).getAbsolutePath();
+            OcrResult ocrResult = engine.runOcr(imgUrl);
+            System.out.println(result=ocrResult.getStrRes().trim());
+        } catch (URISyntaxException e) {
+            throw new RuntimeException(e);
+        }
+        Assertions.assertEquals(expected, result);
+    }
+    @CsvSource({"https://blog.anyi.space/upload/a.jpg,逸"})
+    @ParameterizedTest
+    void testByteData(String url, String expected) {
+        String result;
+        try {
+            byte[] bytes = new URL(url).getFile().getBytes();
+            ParamConfig paramConfig = ParamConfig.getDefaultConfig();
+            paramConfig.setDoAngle(true);
+            paramConfig.setMostAngle(true);
+            OcrInput ocrInput = new OcrInput(bytes);
+            OcrResult ocrResult = engine.runOcr(ocrInput, paramConfig);
+            result = ocrResult.getStrRes().trim();
+        } catch (MalformedURLException e) {
+            throw new RuntimeException(e);
+        }
+        Assertions.assertEquals(expected, result);
+
+    }
+}