Prechádzať zdrojové kódy

Aly营业执照识别

ximinghao 1 mesiac pred
rodič
commit
fd66b0b4b8

+ 7 - 7
pom.xml

@@ -121,17 +121,17 @@
             <artifactId>dysmsapi20170525</artifactId>
             <version>4.1.3</version>
         </dependency>
-        <dependency>
-            <groupId>com.github.desoss</groupId>
-            <artifactId>jackson-datatype-jts</artifactId>
-            <version>2.5.1.2</version>
-        </dependency>
         <!--        阿里云地名地址规范化-->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-address-purification</artifactId>
             <version>1.0.1</version>
         </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>ocr_api20210707</artifactId>
+            <version>3.1.3</version>
+        </dependency>
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
@@ -225,8 +225,8 @@
 
     <repositories>
         <repository>
-            <id>jitpack.io</id>
-            <url>https://jitpack.io</url>
+            <id>mvnrepository.com</id>
+            <url>https://mvnrepository.com/artifact</url>
         </repository>
         <repository>
             <id>osgeo</id>

+ 20 - 1
src/main/java/com/skyversation/xjcy/controller/OtherController.java

@@ -7,6 +7,7 @@ import com.google.zxing.common.HybridBinarizer;
 import com.skyversation.xjcy.bean.User;
 import com.skyversation.xjcy.dms.DMSColumn;
 import com.skyversation.xjcy.dms.DMSService;
+import com.skyversation.xjcy.service.AlyOCRService;
 import com.skyversation.xjcy.service.AuthService;
 import com.skyversation.xjcy.service.WeChatService;
 import com.skyversation.xjcy.util.MessageManage;
@@ -36,11 +37,13 @@ public class OtherController {
     private final WeChatService weChatService;
     private final AuthService authService;
     private final DMSService dMSService;
+    private final AlyOCRService alyOCRService;
 
-    public OtherController(WeChatService weChatService, AuthService authService, DMSService dMSService) {
+    public OtherController(WeChatService weChatService, AuthService authService, DMSService dMSService, AlyOCRService alyOCRService) {
         this.weChatService = weChatService;
         this.authService = authService;
         this.dMSService = dMSService;
+        this.alyOCRService = alyOCRService;
     }
 
     @RequestMapping(value = "/wxacode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@@ -105,4 +108,20 @@ public class OtherController {
             return MessageManage.error(-1,"解析二维码失败");
         }
     }
+
+    @RequestMapping(value ="/ocrBusinessLicense")
+    public String ocrBusinessLicense(
+            @RequestParam("file") MultipartFile file
+    ){
+        try {
+            InputStream inputStream = file.getInputStream();
+            Map<String,Object> result =alyOCRService.ocrBusinessLicense(inputStream);
+            if (result.isEmpty()){
+                return MessageManage.error(-1,"解析二维码失败");
+            }
+            return MessageManage.success("成功",result.get("content"));
+        } catch (Exception e) {
+            return MessageManage.error(-1,"解析二维码失败");
+        }
+    }
 }

+ 57 - 0
src/main/java/com/skyversation/xjcy/service/AlyOCRService.java

@@ -0,0 +1,57 @@
+package com.skyversation.xjcy.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.aliyun.ocr_api20210707.Client;
+import com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseRequest;
+import com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseResponse;
+import com.aliyun.tea.TeaException;
+import com.aliyun.teaopenapi.models.Config;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class AlyOCRService {
+
+    @Value("${app.aly.key}")
+    private String key;
+    @Value("${app.aly.secret}")
+    private String secret;
+    @Value("${app.aly.ocr.endpoint}")
+    private String endpoint;
+
+    public Client createClient() throws Exception {
+        Config config = new Config();
+        config.setAccessKeyId(key);
+        config.setAccessKeySecret(secret);
+        config.setEndpoint(endpoint);
+        return new Client(config);
+    }
+
+    public Map<String, Object> ocrBusinessLicense(InputStream is) throws Exception {
+
+        Client client = createClient();
+        RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest();
+        request.setBody(is);
+
+        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
+        try {
+            RecognizeBusinessLicenseResponse resp = client.recognizeBusinessLicenseWithOptions(request, runtime);
+            Map<String,Object> output = new HashMap<>();
+            output.put("content", JSONObject.parseObject(resp.getBody().getData()).getJSONObject("data"));
+            return output;
+        } catch (TeaException error) {
+            System.out.println(error.getMessage());
+            System.out.println(error.getData().get("Recommend"));
+        } catch (Exception _error) {
+            TeaException error = new TeaException(_error.getMessage(), _error);
+            System.out.println(error.getMessage());
+            System.out.println(error.getData().get("Recommend"));
+        }
+        return Collections.emptyMap();
+    }
+}

+ 6 - 9
src/main/java/com/skyversation/xjcy/service/AlyPhoneMessageSendService.java

@@ -13,13 +13,12 @@ import javax.annotation.PostConstruct;
 
 @Service
 public class AlyPhoneMessageSendService implements PhoneMessageSendService {
-    private Client client;
 
-    @Value("${app.aly.message.key}")
+    @Value("${app.aly.key}")
     private String key;
-    @Value("${app.aly.message.secret}")
+    @Value("${app.aly.secret}")
     private String secret;
-    @Value("${app.aly.endpoint}")
+    @Value("${app.aly.message.endpoint}")
     private String endpoint;
 
     @Value("${app.aly.code.sign}")
@@ -27,15 +26,13 @@ public class AlyPhoneMessageSendService implements PhoneMessageSendService {
     @Value("${app.aly.code.template-code}")
     private String templateCode;
 
-    @PostConstruct
-    public void init() throws Exception {
+    public Client createClient() throws Exception {
         Config config = new Config();
         config.setAccessKeyId(key);
         config.setAccessKeySecret(secret);
         config.setEndpoint(endpoint);
-        this.client = new Client(config);
+        return new Client(config);
     }
-
     @Override
     public boolean sendMessage(String phone, String code) {
         System.out.println(this);
@@ -52,7 +49,7 @@ public class AlyPhoneMessageSendService implements PhoneMessageSendService {
                 .setTemplateCode(templateCode)
                 .setTemplateParam(obj.toJSONString());
         try {
-            SendSmsResponse sendSmsResponse = this.client.sendSms(sendSmsRequest);
+            SendSmsResponse sendSmsResponse = createClient().sendSms(sendSmsRequest);
             return sendSmsResponse.getStatusCode() == 200;
         } catch (Exception e) {
             e.printStackTrace();

+ 6 - 4
src/main/resources/application.yml

@@ -35,13 +35,15 @@ app:
     secret-key: ${XJCY_WECHAT_SECRET:6028cc345cfdbc76224d750a13519762}
     enable: true
   aly:
+    key: ${ALIBABA_CLOUD_ACCESS_KEY_ID:LTAI5tGkCWh7nvNzY5vKNWdZ}
+    secret: ${ALIBABA_CLOUD_ACCESS_KEY_SECRET:YivC1800cacnEpcS3OSQKIcr5Itgal}
     message:
-      key: ${ALIBABA_CLOUD_ACCESS_KEY_ID:LTAI5tGkCWh7nvNzY5vKNWdZ}
-      secret: ${ALIBABA_CLOUD_ACCESS_KEY_SECRET:YivC1800cacnEpcS3OSQKIcr5Itgal}
-    endpoint: ${ALIBABA_CLOUD_ENDPOINT:dysmsapi.aliyuncs.com}
+      endpoint: ${ALIBABA_CLOUD_MESSAGE_ENDPOINT:dysmsapi.aliyuncs.com}
     code:
       sign: ${XJCY_ALIBABA_SIGN:上海元以数智科技}
       template-code: ${XJCY_ALIBABA_CODE_TEMPLATE:SMS_496690245}
+    ocr:
+      endpoint: ${ALIBABA_CLOUD_OCR_ENDPOINT:ocr-api.cn-hangzhou.aliyuncs.com}
 ---
 spring:
   config:
@@ -49,7 +51,7 @@ spring:
       on-profile: "dev"
 app:
   dms:
-    path: http://127.0.0.1:10081/dms
+    path: http://121.43.55.7:10081/dms
   wechat:
     appid: ungiven
     secret-key: ungiven