Sfoglia il codice sorgente

feat:实现使用OCR对图片解析的功能

yang yi 10 mesi fa
parent
commit
3ca30b1160
1 ha cambiato i file con 49 aggiunte e 8 eliminazioni
  1. 49 8
      src/main/java/space/anyi/amountVerification/App.java

+ 49 - 8
src/main/java/space/anyi/amountVerification/App.java

@@ -17,6 +17,8 @@ import javafx.scene.layout.VBox;
 import javafx.stage.DirectoryChooser;
 import javafx.stage.FileChooser;
 import javafx.stage.Stage;
+import net.sourceforge.tess4j.Tesseract;
+import net.sourceforge.tess4j.TesseractException;
 import org.apache.pdfbox.Loader;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.text.PDFTextStripper;
@@ -24,6 +26,8 @@ import space.anyi.amountVerification.model.InputModel;
 import space.anyi.amountVerification.model.OutputErrorModel;
 import space.anyi.amountVerification.model.OutputModel;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.math.BigDecimal;
@@ -94,10 +98,26 @@ public class App extends Application {
          * 代码快初始化匹配金额的正则表达式对象
          */
         //String regex = "理赔金额合计:¥(?<money>\\d+\\.\\d{2})";
-        String regex = "理赔金额合计[^\\d]{1,2}[¥¥](?<money>\\d+\\.\\d{2})";
+        String regex = "理赔金额合计[^\\d]{1,2}[¥¥#Y半]?(?<money>\\d+\\.\\d{2})";
         this.pattern = Pattern.compile(regex);
     }
 
+    /**
+     * OCR使用的对象
+     * @see Tesseract
+     */
+    private Tesseract tesseract;
+    {
+        /**
+         * 代码块初始化OCR使用的对象
+         */
+        this.tesseract = new Tesseract();
+        String workDir = System.getProperty("user.dir") + "/tessdata";
+        this.tesseract.setDatapath(workDir);
+        //设置识别语言为中文简体,(如果要设置为英文可改为"eng")
+        this.tesseract.setLanguage("chi_sim");
+    }
+
     @Override
     public void start(Stage primaryStage) throws Exception {
         this.primaryStage = primaryStage;
@@ -273,7 +293,7 @@ public class App extends Application {
             return;
         }
         //开启一个新的线程处理
-        new Thread(()->{
+        Thread thread = new Thread(() -> {
             try {
                 // 初始化进度条
                 progressBar.setProgress(0);
@@ -292,13 +312,13 @@ public class App extends Application {
                 updateProgress("Excel文件读取完成,正在扫描文件夹...", 0.25);
                 //读取文件夹
                 readFolder(folderPath);
-                updateProgress("文件夹扫描完成,开始解析PDF文件...", 0.5);
+                updateProgress("文件夹扫描完成,开始解析工单文件...", 0.5);
                 // 解析PDF文件
                 handler(errorImeiList);
-                updateProgress("PDF文件解析完成,正在生成输出文件...", 0.9);
+                updateProgress("工单文件解析完成,正在生成输出文件...", 0.9);
                 //输出最终文件
                 String outputFileName = filePath.substring(0, filePath.lastIndexOf(".")) + "_" + System.currentTimeMillis() + "_output.xlsx";
-                outputFileHandler(outputFileName,errorImeiList);
+                outputFileHandler(outputFileName, errorImeiList);
                 updateProgress("验证完成!输出文件: " + outputFileName, 1.0);
 
                 // 显示完成信息
@@ -324,7 +344,9 @@ public class App extends Application {
                 outputModelList = null;
                 workOrderFileMap = null;
             }
-        }).start();
+        });
+        thread.setDaemon(true);
+        thread.start();
     }
 
     private void outputFileHandler(String outputFileName,List<OutputErrorModel> errorImeiList) {
@@ -401,7 +423,25 @@ public class App extends Application {
      */
     private String imageHandler(File workOrderFile) {
         String text = null;
-        //todo:OCR识别
+        //OCR识别
+        BufferedImage bufferedImage = null;
+        try {
+            //开始识别整张图片中的文字
+            bufferedImage = ImageIO.read(workOrderFile);
+            //直接处理二进制数据,避免因为文件扩展名错误导致的处理失败
+            text = tesseract.doOCR(bufferedImage);
+
+        } catch (TesseractException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }finally {
+            if (!Objects.isNull(bufferedImage)) {
+                //释放资源
+                bufferedImage.flush();
+                bufferedImage = null;
+            }
+        }
         return text;
     }
 
@@ -439,7 +479,8 @@ public class App extends Application {
                 continue;
             }
             File[] listFiles = file.listFiles();
-            String imei = file.getName().split("-")[2];
+            String[] split = file.getName().split("-");
+            String imei = split[split.length-1];
             //拿到文件夹里的工单文件(文件类型可能是PDF或JPG)
             for (File workOrderFile : listFiles) {
                 if (workOrderFile.getName().contains("工单照片")) {