|
@ -25,10 +25,12 @@ public class BaiDuUtil { |
|
|
private RedisUtil redisUtil; |
|
|
private RedisUtil redisUtil; |
|
|
private static BaiDuUtil util; |
|
|
private static BaiDuUtil util; |
|
|
/**token_appKey*/ |
|
|
/**token_appKey*/ |
|
|
private static final String tokenKey = "baidu_token_"; |
|
|
private static final String TOKEN_KEY = "baidu_token_"; |
|
|
|
|
|
|
|
|
private static final String authUrl = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}"; |
|
|
private static final String AUTH_URL = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}"; |
|
|
private static final String generalBasicUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}"; |
|
|
private static final String GENERAL_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}"; |
|
|
|
|
|
/**通用文字识别(高精度版)*/ |
|
|
|
|
|
private static final String ACCURATE_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token={}"; |
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
@PostConstruct |
|
|
public void init(){ |
|
|
public void init(){ |
|
@ -44,13 +46,13 @@ public class BaiDuUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static String getToken(String appKey, String secretKey){ |
|
|
public static String getToken(String appKey, String secretKey){ |
|
|
log.info("获取百度认证:{},{}", appKey, secretKey); |
|
|
log.info("获取百度认证:{},{}", appKey, secretKey); |
|
|
String key = tokenKey + appKey; |
|
|
String key = TOKEN_KEY + appKey; |
|
|
String token = (String) util.redisUtil.get(key); |
|
|
String token = (String) util.redisUtil.get(key); |
|
|
log.info("缓存token:{}", token); |
|
|
log.info("缓存token:{}", token); |
|
|
if (StrUtil.isNotEmpty(token)) { |
|
|
if (StrUtil.isNotEmpty(token)) { |
|
|
return token; |
|
|
return token; |
|
|
} |
|
|
} |
|
|
String url = StrUtil.format(authUrl, appKey, secretKey); |
|
|
String url = StrUtil.format(AUTH_URL, appKey, secretKey); |
|
|
String result = RestTemplateUtil.postUrlEncode(url, null); |
|
|
String result = RestTemplateUtil.postUrlEncode(url, null); |
|
|
log.info("获取百度认证:{}", result); |
|
|
log.info("获取百度认证:{}", result); |
|
|
JSONObject json = JSONObject.parseObject(result); |
|
|
JSONObject json = JSONObject.parseObject(result); |
|
@ -67,20 +69,47 @@ public class BaiDuUtil { |
|
|
return token; |
|
|
return token; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 调用百度通用文字识别(高精度版) |
|
|
|
|
|
* @param appKey appKey |
|
|
|
|
|
* @param secretKey secretKey |
|
|
|
|
|
* @param basic 图像 |
|
|
|
|
|
* @return 识别结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static BaiDuVo.GeneralBasic accurateBasic(String appKey, String secretKey, BaiDuDto.GeneralBasic basic) { |
|
|
|
|
|
return getGeneralBasic(appKey, secretKey, basic, ACCURATE_BASIC_URL); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static BaiDuVo.GeneralBasic generalBasic(String appKey, String secretKey, BaiDuDto.GeneralBasic basic) { |
|
|
/** |
|
|
|
|
|
* 调用百度通用文字识别 |
|
|
|
|
|
* @param appKey appKey |
|
|
|
|
|
* @param secretKey secretKey |
|
|
|
|
|
* @param basic 图像 |
|
|
|
|
|
* @param accurateBasicUrl 路径 |
|
|
|
|
|
* @return 识别结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
private static BaiDuVo.GeneralBasic getGeneralBasic(String appKey, String secretKey, BaiDuDto.GeneralBasic basic, String accurateBasicUrl) { |
|
|
String token = getToken(appKey, secretKey); |
|
|
String token = getToken(appKey, secretKey); |
|
|
String url = StrUtil.format(generalBasicUrl, token); |
|
|
String url = StrUtil.format(accurateBasicUrl, token); |
|
|
// String result = RestTemplateUtil.postBody(url, basic);
|
|
|
|
|
|
String result = RestTemplateUtil.postUrlEncode(url, basic); |
|
|
String result = RestTemplateUtil.postUrlEncode(url, basic); |
|
|
log.info("调用通用文字识别(标准版)结果:{}", result); |
|
|
log.info("百度orc调用{}的结果是:{}", url, result); |
|
|
if (result.contains(Code.GENERAL_BASIC_ERROR)) { |
|
|
if (result.contains(Code.GENERAL_BASIC_ERROR)) { |
|
|
CodeEnum thirdError = CodeEnum.THIRD_ERROR; |
|
|
CodeEnum thirdError = CodeEnum.THIRD_ERROR; |
|
|
thirdError.setMsg(result); |
|
|
thirdError.setMsg(result); |
|
|
throw new BaseException(thirdError); |
|
|
throw new BaseException(thirdError); |
|
|
} |
|
|
} |
|
|
BaiDuVo.GeneralBasic basicVo = JSONObject.parseObject(result, BaiDuVo.GeneralBasic.class); |
|
|
return JSONObject.parseObject(result, BaiDuVo.GeneralBasic.class); |
|
|
return basicVo; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 调用百度通用文字识别(标准版) |
|
|
|
|
|
* @param appKey appKey |
|
|
|
|
|
* @param secretKey secretKey |
|
|
|
|
|
* @param basic 图像 |
|
|
|
|
|
* @return 识别结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static BaiDuVo.GeneralBasic generalBasic(String appKey, String secretKey, BaiDuDto.GeneralBasic basic) { |
|
|
|
|
|
return getGeneralBasic(appKey, secretKey, basic, GENERAL_BASIC_URL); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static class Code{ |
|
|
private static class Code{ |
|
|