|
@@ -0,0 +1,114 @@
|
|
|
|
+package com.qmth.paper.library.common.util;
|
|
|
|
+
|
|
|
|
+import com.baidu.aip.ocr.AipOcr;
|
|
|
|
+import com.qmth.paper.library.common.contant.SystemConstant;
|
|
|
|
+import com.qmth.paper.library.common.domain.OcrDomain;
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: ocr工具
|
|
|
|
+ * @Param:
|
|
|
|
+ * @return:
|
|
|
|
+ * @Author: wangliang
|
|
|
|
+ * @Date: 2022/10/12
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+public class OcrUtil {
|
|
|
|
+
|
|
|
|
+ private OcrDomain ocrDomain;
|
|
|
|
+
|
|
|
|
+ private AipOcr client;
|
|
|
|
+
|
|
|
|
+ private HashMap<String, String> options;
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public OcrDomain ocrDomainEnv(OcrDomain ocrDomain) {
|
|
|
|
+ this.ocrDomain = new OcrDomain(ocrDomain.getAppId(),
|
|
|
|
+ ocrDomain.getAppKey(),
|
|
|
|
+ ocrDomain.getAppSecret());
|
|
|
|
+ client = new AipOcr(ocrDomain.getAppId(), ocrDomain.getAppKey(), ocrDomain.getAppSecret());
|
|
|
|
+ // 可选:设置网络连接参数
|
|
|
|
+ client.setConnectionTimeoutInMillis(SystemConstant.OCR_CONNECTION_TIMEOUT_IN_MILLIS);
|
|
|
|
+ client.setSocketTimeoutInMillis(SystemConstant.OCR_SOCKET_TIMEOUT_IN_MILLIS);
|
|
|
|
+ options = new HashMap<String, String>();
|
|
|
|
+ options.put("probability", "true");
|
|
|
|
+ return this.ocrDomain;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public OcrDomain getOcrDomain() {
|
|
|
|
+ return ocrDomain;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AipOcr getClient() {
|
|
|
|
+ return client;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Map<String, String> getOptions() {
|
|
|
|
+ return options;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr通用文字识别基础接口
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callBasicGeneral(String path) {
|
|
|
|
+ return client.basicGeneral(path, options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr通用文字识别高精度版接口
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callBasicAccurateGeneral(String path) {
|
|
|
|
+ return client.basicAccurateGeneral(path, options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr通用文字识别接口(含位置信息版)
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callGeneral(String path) {
|
|
|
|
+ return client.general(path, options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr通用文字识别接口(含位置高精度版)
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callAccurateGeneral(String path) {
|
|
|
|
+ return client.accurateGeneral(path, options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr手写文字识别接口
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callHandwriting(String path) {
|
|
|
|
+ return client.handwriting(path, options);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ocr手写文字识别接口
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JSONObject callDocAnalysis(String path) {
|
|
|
|
+ return client.docAnalysis(path, options);
|
|
|
|
+ }
|
|
|
|
+}
|