Kaynağa Gözat

ocr本地调用

xiatian 8 ay önce
ebeveyn
işleme
ff5cbbc2c5

+ 44 - 36
src/main/java/cn/com/qmth/am/config/InitData.java

@@ -13,46 +13,54 @@ import org.springframework.stereotype.Component;
 import com.qmth.boot.core.solar.model.OrgInfo;
 import com.qmth.boot.core.solar.service.SolarService;
 
+import cn.com.qmth.am.service.OcrService;
 import cn.com.qmth.am.service.StudentScoreService;
 import cn.com.qmth.am.service.StudentService;
 
 @Component
 public class InitData implements CommandLineRunner {
-	private static final Logger log = LoggerFactory.getLogger(InitData.class);
-	@Autowired
-	private SysProperty sysProperty;
-	@Autowired
-	private SolarService solarService;
-	
-	@Autowired
-	private StudentScoreService studentScoreService;
-	
-	@Autowired
-	private StudentService studentService;
-	
-	@Override
-	public void run(String... args) throws Exception {
-		List<OrgInfo> orgs = null;
-		try {
-			orgs = solarService.getOrgList();
-		} catch (Exception e) {
-			log.error("激活授权失败");
-			System.exit(1);
-		}
-		if(CollectionUtils.isEmpty(orgs)) {
-			log.error("授权信息有误");
-			System.exit(1);
-		}
-		File dataDir=new File(sysProperty.getDataDir());
-		if(!dataDir.exists()) {
-			dataDir.mkdir();
-		}
-		resetTaskStatus();
-	}
-	
-	private void resetTaskStatus() {
-		studentScoreService.resetStatus();
-		studentService.resetStatus();
-	}
+
+    private static final Logger log = LoggerFactory.getLogger(InitData.class);
+
+    @Autowired
+    private SysProperty sysProperty;
+
+    @Autowired
+    private SolarService solarService;
+
+    @Autowired
+    private StudentScoreService studentScoreService;
+
+    @Autowired
+    private StudentService studentService;
+
+    @Autowired
+    private OcrService ocrService;
+
+    @Override
+    public void run(String... args) throws Exception {
+        List<OrgInfo> orgs = null;
+        try {
+            orgs = solarService.getOrgList();
+        } catch (Exception e) {
+            log.error("激活授权失败");
+            System.exit(1);
+        }
+        if (CollectionUtils.isEmpty(orgs)) {
+            log.error("授权信息有误");
+            System.exit(1);
+        }
+        File dataDir = new File(sysProperty.getDataDir());
+        if (!dataDir.exists()) {
+            dataDir.mkdir();
+        }
+        resetTaskStatus();
+        // ocrService.ocr();
+    }
+
+    private void resetTaskStatus() {
+        studentScoreService.resetStatus();
+        studentService.resetStatus();
+    }
 
 }

+ 10 - 0
src/main/java/cn/com/qmth/am/service/OcrService.java

@@ -0,0 +1,10 @@
+package cn.com.qmth.am.service;
+
+/**
+ * 类注释
+ */
+public interface OcrService {
+
+    void ocr();
+
+}

+ 117 - 0
src/main/java/cn/com/qmth/am/service/impl/OcrServiceImpl.java

@@ -0,0 +1,117 @@
+package cn.com.qmth.am.service.impl;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+import org.apache.commons.io.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.qmth.boot.core.ai.client.OcrApiClient;
+import com.qmth.boot.core.ai.model.ocr.OcrType;
+import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.core.retrofit.exception.RetrofitResponseError;
+import com.qmth.boot.core.retrofit.utils.SignatureInfo;
+import com.qmth.boot.core.retrofit.utils.UploadFile;
+import com.qmth.boot.core.solar.model.OrgInfo;
+import com.qmth.boot.core.solar.service.SolarService;
+
+import cn.com.qmth.am.bean.StudentScoreImageDto;
+import cn.com.qmth.am.service.OcrService;
+
+@Service
+public class OcrServiceImpl implements OcrService {
+
+    private static final Logger log = LoggerFactory.getLogger(OcrService.class);
+
+    @Autowired
+    private OcrApiClient ocrApiClient;
+
+    @Autowired
+    private SolarService solarService;
+
+    @Override
+    public void ocr() {
+        File dir = new File("d:/ocr");
+        OrgInfo org = solarService.getOrgList().get(0);
+        disposeFile(dir, org);
+        log.warn("OcrService ocr finish*************");
+    }
+
+    private void disposeFile(File file, OrgInfo org) {
+        if (file.isFile() && file.getName().toLowerCase().endsWith(".jpg")) {
+            StudentScoreImageDto dto = new StudentScoreImageDto();
+            dto.setImage(fileToByte(file));
+            String content = ocrDispose(dto, org);
+            String name = file.getName().substring(0, file.getName().lastIndexOf("."));
+            File txt = new File(file.getParentFile().getAbsolutePath() + "/" + name + ".txt");
+            if (txt.exists()) {
+                txt.delete();
+            }
+            try {
+                FileUtils.write(txt, content, "utf-8");
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            if (file.isDirectory()) {
+                for (File subFile : file.listFiles()) {
+                    disposeFile(subFile, org);
+                }
+            }
+        }
+    }
+
+    private byte[] fileToByte(File img) {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            BufferedImage bi;
+            bi = ImageIO.read(img);
+            ImageIO.write(bi, "jpg", baos);
+            byte[] bytes = baos.toByteArray();
+            return bytes;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                baos.close();
+            } catch (IOException e) {
+            }
+        }
+    }
+
+    private String ocrDispose(StudentScoreImageDto dto, OrgInfo org) {
+        SignatureInfo signature = SignatureInfo.secret(org.getAccessKey(), org.getAccessSecret());
+        try {
+            return ocrApiClient.forImage(signature, OcrType.HANDWRITING, UploadFile.build("image", "", dto.getImage()));
+        } catch (Exception e) {
+            log.error("ocr异常", e);
+            if (e instanceof RetrofitResponseError) {
+                RetrofitResponseError tem = (RetrofitResponseError) e;
+                if (tem.getCode() == 503) {
+                    if (dto.getRetry() <= 3) {
+                        try {
+                            Thread.sleep(3000);
+                        } catch (InterruptedException e1) {
+                        }
+                        dto.setRetry(dto.getRetry() + 1);
+                        return ocrDispose(dto, org);
+                    } else {
+                        throw new StatusException("重试次数过多");
+                    }
+                } else {
+                    throw e;
+                }
+            } else {
+                throw e;
+            }
+        }
+    }
+
+}