ApiUtil.java
4.53 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.sincere.haikangface.utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class ApiUtil {
private String youdaoApiUrl = "https://openapi.youdao.com/cut_question";
private String appKey = "2105186434efa2fc";
private String appSecret = "YWd4s7kgchkdQGjS0zL64yhbC3h7hr4S";
private static ApiUtil apiUtil;
public static ApiUtil getInstance() {
if (null == apiUtil) {
synchronized (ApiUtil.class) {
if (null == apiUtil) apiUtil = new ApiUtil();
}
}
return apiUtil;
}
public JSONArray youDaoAPi(File imgFile) throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
String base64Img = loadAsBase64(imgFile.getAbsolutePath());
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("q", base64Img);
multiValueMap.add("imageType", "1");
multiValueMap.add("appKey", appKey);
multiValueMap.add("salt", getUUid());
multiValueMap.add("docType", "json");
multiValueMap.add("signType", "v3");
multiValueMap.add("curtime", System.currentTimeMillis() / 1000);
multiValueMap.add("sign", getDigest(appKey + truncate(base64Img) + getUUid() + (System.currentTimeMillis() / 1000) + appSecret));
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(multiValueMap, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(youdaoApiUrl, requestEntity, String.class);
JSONArray regions = null;
try {
JSONObject jsonObject = new JSONObject(responseEntity.getBody());
JSONObject Result = jsonObject.optJSONObject("Result");
regions = Result.optJSONArray("regions");
} catch (JSONException e) {
e.printStackTrace();
}
return regions;
}
public static String loadAsBase64(String imgFile) {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
File file = new File(imgFile);
if (!file.exists()) {
return null;
}
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
return Base64.getEncoder().encodeToString(data);//返回Base64编码过的字节数组字符串
}
private String getUUid() {
return String.valueOf(System.currentTimeMillis());
}
public String truncate(String q) {
if (q == null) {
return null;
}
int len = q.length();
String result;
return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
}
/**
* 生成加密字段
*/
public String getDigest(String string) {
if (string == null) {
return null;
}
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] btInput = string.getBytes(StandardCharsets.UTF_8);
try {
MessageDigest mdInst = MessageDigest.getInstance("SHA-256");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}