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