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