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("当前内容无法识别");
|
}
|
}
|