|
|
@@ -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);
|
|
|
+ }
|
|
|
+}
|