8.9
luodangjia
2024-09-03 2855ae89d2ab6268e1edc42c3e9d7e73c8e7259e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.ruoyi.admin.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.cvm.v20170312.CvmClient;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
 
import java.util.HashMap;
import java.util.Map;
 
public class DescribeInstances {
 
    public static  JSONObject ocr(String address){
 
        String host = "https://addre.market.alicloudapi.com";
        String path = "/format";
        String method = "GET";
        String appcode = "0efeed6a5fe94240b143fec8910c5053";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("text", address);
 
 
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println();
            String string = EntityUtils.toString(response.getEntity());
 
 
            JSONObject json = JSON.parseObject(string);
 
            // 获取 data 对象
            JSONObject data = json.getJSONObject("data");
 
            // 提取所需的字段
            String address1 = data.getString("address");
            String person1 = data.getString("person");
            String phonenum1 = data.getString("phonenum");
 
            // 打印结果
            System.out.println("Address: " + address1);
            System.out.println("Person: " + person1);
            System.out.println("Phone Number: " + phonenum1);
 
            AddressDto addressDto = new AddressDto();
            addressDto.setAddress(address1);
            addressDto.setName(person1);
            addressDto.setPhone(phonenum1);
            return data;
            //获取response的body
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
 
    }
    public static AddressDto  orcr(String args) {
        try {
            // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中,请参考本文凭证管理章节。
            // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
            // Credential cred = new Credential("SecretId", "SecretKey");
            Credential cred = new Credential("AKIDHVcchrvvtBiRJB25bHFFJc2Laq4NnHsY", "pgUGwX4Z1SrAPXYk1PxgXVem2HlRJG9k");
            OcrClient client = new OcrClient(cred, "ap-shanghai");
 
 
            SmartStructuralOCRV2Request waybillOCRRequest = new SmartStructuralOCRV2Request();
//            waybillOCRRequest.setImageUrl("https://huishou-1323682843.cos.ap-nanjing.myqcloud.com/images/e1bb6062-fc05-4b68-ba35-c70cb8067fff.png");
            waybillOCRRequest.setImageBase64(args);
            waybillOCRRequest.setItemNames(new String[]{"收件人", "手机号", "所在地区","详细地址"});
            SmartStructuralOCRV2Response resp = client.SmartStructuralOCRV2(waybillOCRRequest);
 
            GroupInfo[] structuralList = resp.getStructuralList();
            AddressDto addressDto = new AddressDto();
            for (GroupInfo groupInfo : structuralList) {
                LineInfo[] groups = groupInfo.getGroups();
                for (LineInfo group : groups) {
                    ItemInfo[] lines = group.getLines();
                    for (ItemInfo line : lines) {
                        if (line.getKey().getAutoName().equals("收件人")){
                            System.err.println(line.getValue().getAutoContent());
                            addressDto.setName(line.getValue().getAutoContent());
                        }else if (line.getKey().getAutoName().equals("手机号")){
                            System.err.println(line.getValue().getAutoContent());
                            addressDto.setPhone(line.getValue().getAutoContent());
                        }else if (line.getKey().getAutoName().equals("所在地区")){
                            System.err.println(line.getValue().getAutoContent());
                            addressDto.setAddress(line.getValue().getAutoContent());
                        }else if (line.getKey().getAutoName().equals("详细地址")){
                            System.err.println(line.getValue().getAutoContent());
                            addressDto.setDetail(line.getValue().getAutoContent());
                        }
 
                    }
                }
 
            }
 
 
            return addressDto;
 
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }
        throw new RuntimeException("当前内容无法识别");
    }
}