Commit bc95fd027b63b29692afe29cebecdb29c56b9939
1 parent
e383c0c2
Exists in
master
百度云相关配置添加,token,图片切割接口
Showing
7 changed files
with
234 additions
and
1 deletions
Show diff stats
springboot.iml
... | ... | @@ -175,5 +175,7 @@ |
175 | 175 | <orderEntry type="library" scope="RUNTIME" name="Maven: com.netflix.netflix-commons:netflix-statistics:0.1.1" level="project" /> |
176 | 176 | <orderEntry type="library" name="Maven: io.reactivex:rxjava:1.3.8" level="project" /> |
177 | 177 | <orderEntry type="library" name="Maven: com.netflix.ribbon:ribbon-eureka:2.3.0" level="project" /> |
178 | + <orderEntry type="library" name="Maven: com.baidu.aip:java-sdk:4.12.0" level="project" /> | |
179 | + <orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.25" level="project" /> | |
178 | 180 | </component> |
179 | 181 | </module> |
180 | 182 | \ No newline at end of file | ... | ... |
springboot/pom.xml
... | ... | @@ -130,6 +130,12 @@ |
130 | 130 | <groupId>org.springframework.cloud</groupId> |
131 | 131 | <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> |
132 | 132 | </dependency> |
133 | + <!-- 百度文字识别--> | |
134 | + <dependency> | |
135 | + <groupId>com.baidu.aip</groupId> | |
136 | + <artifactId>java-sdk</artifactId> | |
137 | + <version>4.12.0</version> | |
138 | + </dependency> | |
133 | 139 | </dependencies> |
134 | 140 | |
135 | 141 | <dependencyManagement> | ... | ... |
springboot/src/main/java/com/sincre/springboot/ApiModel/BaiduResult.java
0 → 100644
... | ... | @@ -0,0 +1,53 @@ |
1 | +package com.sincre.springboot.ApiModel; | |
2 | +import com.fasterxml.jackson.annotation.JsonInclude; | |
3 | + | |
4 | +@JsonInclude(JsonInclude.Include.NON_EMPTY) | |
5 | + | |
6 | +public class BaiduResult <T>{ | |
7 | + | |
8 | + public Boolean getSuccess() { | |
9 | + return success; | |
10 | + } | |
11 | + | |
12 | + public void setSuccess(Boolean success) { | |
13 | + this.success = success; | |
14 | + } | |
15 | + | |
16 | + public T getResult() { | |
17 | + return result; | |
18 | + } | |
19 | + | |
20 | + public void setResult(T result) { | |
21 | + this.result = result; | |
22 | + } | |
23 | + | |
24 | + public String getCode() { | |
25 | + return code; | |
26 | + } | |
27 | + | |
28 | + public void setCode(String code) { | |
29 | + this.code = code; | |
30 | + } | |
31 | + | |
32 | + public String getMsg() { | |
33 | + return msg; | |
34 | + } | |
35 | + | |
36 | + public void setMsg(String msg) { | |
37 | + this.msg = msg; | |
38 | + } | |
39 | + | |
40 | + public Long getT() { | |
41 | + return t; | |
42 | + } | |
43 | + | |
44 | + public void setT(Long t) { | |
45 | + this.t = t; | |
46 | + } | |
47 | + | |
48 | + private Boolean success; | |
49 | + private String code; | |
50 | + private String msg; | |
51 | + private Long t; | |
52 | + private T result; | |
53 | +} | ... | ... |
springboot/src/main/java/com/sincre/springboot/ApiPlatform/BaiduYunServiceConfig.java
0 → 100644
springboot/src/main/java/com/sincre/springboot/controller/BaiduController.java
0 → 100644
... | ... | @@ -0,0 +1,59 @@ |
1 | +package com.sincre.springboot.controller; | |
2 | + | |
3 | + | |
4 | +import com.alibaba.fastjson.JSON; | |
5 | +import com.baidu.aip.util.Base64Util; | |
6 | +import com.sincre.springboot.ApiModel.BaiduResult; | |
7 | +import com.sincre.springboot.ApiModel.TuYaResResult; | |
8 | +import com.sincre.springboot.common.ServerResponse; | |
9 | +import com.sincre.springboot.utils.ApiHelper; | |
10 | +import com.sincre.springboot.utils.CacheHelper; | |
11 | +import com.sincre.springboot.utils.ResultUtils; | |
12 | +import io.swagger.annotations.Api; | |
13 | +import io.swagger.annotations.ApiImplicitParam; | |
14 | +import io.swagger.annotations.ApiOperation; | |
15 | +import org.springframework.web.bind.annotation.GetMapping; | |
16 | +import org.springframework.web.bind.annotation.RequestMapping; | |
17 | +import org.springframework.web.bind.annotation.RestController; | |
18 | + | |
19 | +import java.net.URLEncoder; | |
20 | +import java.util.HashMap; | |
21 | +import java.util.Map; | |
22 | + | |
23 | +@RestController | |
24 | +@RequestMapping(value = "/Baidu/*") | |
25 | +@Api(tags = "百度云服务API调用") | |
26 | +public class BaiduController { | |
27 | + | |
28 | + @ApiOperation("根据上传的图片二进制数据进行切图") | |
29 | + @ApiImplicitParam(name="imgPostData",value = "图片二进制数据",required = true) | |
30 | + @GetMapping("general") | |
31 | + public static ServerResponse general(byte[] imgPostData) { | |
32 | + // 请求url | |
33 | + String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"; | |
34 | + try { | |
35 | + byte[] imgData = imgPostData; | |
36 | + String imgStr = Base64Util.encode(imgData); | |
37 | + String imgParam = URLEncoder.encode(imgStr, "UTF-8"); | |
38 | + String param = "image=" + imgParam; | |
39 | + // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。 | |
40 | + String accessToken = CacheHelper.getBaiDuToken(); | |
41 | + Long t = System.currentTimeMillis(); | |
42 | + | |
43 | + Map<String,String> headMap = new HashMap<>(); | |
44 | + headMap.put("Content-Type","application/x-www-form-urlencoded"); | |
45 | + | |
46 | + Map<String,Object> bodyMap = new HashMap<>(); | |
47 | + bodyMap.put("image",param); | |
48 | + bodyMap.put("t",t.toString()); | |
49 | + | |
50 | + String result = ApiHelper.doPost(url+accessToken,headMap,bodyMap); | |
51 | + BaiduResult baiduResult = JSON.parseObject(result,BaiduResult.class); | |
52 | + return ResultUtils.getInstance().returnResultBaidu(baiduResult); | |
53 | + } catch (Exception e) { | |
54 | + e.printStackTrace(); | |
55 | + } | |
56 | + return null; | |
57 | + } | |
58 | + | |
59 | +} | ... | ... |
springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java
... | ... | @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.sincre.springboot.ApiModel.TuYaResResult; |
6 | 6 | import com.sincre.springboot.ApiModel.TuYaResTokenObj; |
7 | +import com.sincre.springboot.ApiPlatform.BaiduYunServiceConfig; | |
7 | 8 | import com.sincre.springboot.ApiPlatform.TuYaCloudService; |
8 | 9 | import com.sincre.springboot.ApiPlatform.YinShiServiceConfig; |
9 | 10 | import com.sincre.springboot.common.EhcacheUtil; |
... | ... | @@ -12,7 +13,13 @@ import org.json.JSONObject; |
12 | 13 | |
13 | 14 | import java.util.HashMap; |
14 | 15 | import java.util.Map; |
15 | - | |
16 | +import org.json.JSONObject; | |
17 | +import java.io.BufferedReader; | |
18 | +import java.io.InputStreamReader; | |
19 | +import java.net.HttpURLConnection; | |
20 | +import java.net.URL; | |
21 | +import java.util.List; | |
22 | +import java.util.Map; | |
16 | 23 | public class CacheHelper { |
17 | 24 | |
18 | 25 | static EhcacheUtil ehcacheUtil = new EhcacheUtil(); |
... | ... | @@ -131,4 +138,73 @@ public class CacheHelper { |
131 | 138 | YinShiServiceConfig.AccessToken = data.optString("accessToken"); |
132 | 139 | } |
133 | 140 | |
141 | + /** | |
142 | + * 获取权限token | |
143 | + * | |
144 | + * @return 返回示例: | |
145 | + * { | |
146 | + * "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567", | |
147 | + * "expires_in": 2592000 | |
148 | + * } | |
149 | + */ | |
150 | + public static String getBaiDuToken() { | |
151 | + // 官网获取的 API Key 更新为你注册的 | |
152 | + String clientId = BaiduYunServiceConfig.client_id; | |
153 | + // 官网获取的 Secret Key 更新为你注册的 | |
154 | + String clientSecret = BaiduYunServiceConfig.client_secret; | |
155 | + return getAuth(clientId, clientSecret); | |
156 | + } | |
157 | + | |
158 | + /** | |
159 | + * 获取API访问token | |
160 | + * 该token有一定的有效期,需要自行管理,当失效时需重新获取. | |
161 | + * | |
162 | + * @param ak - 百度云官网获取的 API Key | |
163 | + * @param sk - 百度云官网获取的 Securet Key | |
164 | + * @return assess_token 示例: | |
165 | + * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567" | |
166 | + */ | |
167 | + public static String getAuth(String ak, String sk) { | |
168 | + | |
169 | + // 获取token地址 | |
170 | + String authHost = "https://aip.baidubce.com/oauth/2.0/token?"; | |
171 | + String getAccessTokenUrl = authHost | |
172 | + // 1. grant_type为固定参数 | |
173 | + + "grant_type=client_credentials" | |
174 | + // 2. 官网获取的 API Key | |
175 | + + "&client_id=" + ak | |
176 | + // 3. 官网获取的 Secret Key | |
177 | + + "&client_secret=" + sk; | |
178 | + try { | |
179 | + URL realUrl = new URL(getAccessTokenUrl); | |
180 | + // 打开和URL之间的连接 | |
181 | + HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); | |
182 | + connection.setRequestMethod("GET"); | |
183 | + connection.connect(); | |
184 | + // 获取所有响应头字段 | |
185 | + Map<String, List<String>> map = connection.getHeaderFields(); | |
186 | + // 遍历所有的响应头字段 | |
187 | + for (String key : map.keySet()) { | |
188 | + System.err.println(key + "--->" + map.get(key)); | |
189 | + } | |
190 | + // 定义 BufferedReader输入流来读取URL的响应 | |
191 | + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
192 | + String result = ""; | |
193 | + String line; | |
194 | + while ((line = in.readLine()) != null) { | |
195 | + result += line; | |
196 | + } | |
197 | + /** | |
198 | + * 返回结果示例 | |
199 | + */ | |
200 | + System.err.println("result:" + result); | |
201 | + JSONObject jsonObject = new JSONObject(result); | |
202 | + String access_token = jsonObject.getString("access_token"); | |
203 | + return access_token; | |
204 | + } catch (Exception e) { | |
205 | + System.err.printf("获取token失败!"); | |
206 | + e.printStackTrace(System.err); | |
207 | + } | |
208 | + return null; | |
209 | + } | |
134 | 210 | } | ... | ... |
springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java
... | ... | @@ -3,6 +3,7 @@ package com.sincre.springboot.utils; |
3 | 3 | |
4 | 4 | import com.alibaba.fastjson.JSON; |
5 | 5 | import com.alibaba.fastjson.JSONArray; |
6 | +import com.sincre.springboot.ApiModel.BaiduResult; | |
6 | 7 | import com.sincre.springboot.ApiModel.TuYaResResult; |
7 | 8 | import com.sincre.springboot.ApiModel.YinShiResResult; |
8 | 9 | import com.sincre.springboot.common.ResponseCode; |
... | ... | @@ -93,4 +94,33 @@ public class ResultUtils { |
93 | 94 | } |
94 | 95 | } |
95 | 96 | |
97 | + /** | |
98 | + * 百度云返回结果 | |
99 | + * @param | |
100 | + * @return | |
101 | + */ | |
102 | + public ServerResponse returnResultBaidu(BaiduResult baiduResResult) { | |
103 | + try { | |
104 | + boolean success = baiduResResult.getSuccess(); | |
105 | + | |
106 | + if (success) { | |
107 | + //data 返回的是数组 | |
108 | + if(JSON.isValidArray(baiduResResult.getResult().toString())){ | |
109 | + int total; | |
110 | + JSONArray jsonArray = JSON.parseArray(baiduResResult.getResult().toString()); | |
111 | + total = jsonArray.size(); | |
112 | + return ServerResponse.createBySuccess(String.valueOf(total), ResponseCode.SUCCESS.getDesc(), baiduResResult.getResult()); | |
113 | + } | |
114 | + //data 返回的是对象 | |
115 | + return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(), baiduResResult.getResult()); | |
116 | + } | |
117 | + | |
118 | + int code = Integer.valueOf(baiduResResult.getCode()); | |
119 | + return ServerResponse.createByErrorCodeMessage(code, baiduResResult.getMsg()); | |
120 | + } | |
121 | + catch (Exception ex){ | |
122 | + return ServerResponse.createByErrorMessage("服务器内部处理出错!"); | |
123 | + } | |
124 | + } | |
125 | + | |
96 | 126 | } | ... | ... |