This commit is contained in:
parent
0c4949f672
commit
0864b70fb1
|
|
@ -58,7 +58,7 @@ public class ProAuthorizeServiceImpl implements ProAuthorizeService {
|
||||||
/** 应用2 */
|
/** 应用2 */
|
||||||
public static final String SECRET_KEY_2 = "w1jfLoSLFpxdIJpQriJXWpoNF1YOWTdJ";
|
public static final String SECRET_KEY_2 = "w1jfLoSLFpxdIJpQriJXWpoNF1YOWTdJ";
|
||||||
|
|
||||||
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
// static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ProAuthorizeMapper mapper;
|
private ProAuthorizeMapper mapper;
|
||||||
|
|
@ -118,52 +118,52 @@ public class ProAuthorizeServiceImpl implements ProAuthorizeService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult uploadPhoto(String filePath, Long type) {
|
public AjaxResult uploadPhoto(String filePath, Long type) {
|
||||||
try {
|
// try {
|
||||||
String base64 = encodeImageFromUrlToBase64(filePath);
|
// String base64 = encodeImageFromUrlToBase64(filePath);
|
||||||
String types="";
|
// String types="";
|
||||||
if (type == 1){
|
// if (type == 1){
|
||||||
types="front";
|
// types="front";
|
||||||
} else if (type == 2){
|
// } else if (type == 2){
|
||||||
types="back";
|
// types="back";
|
||||||
}
|
// }
|
||||||
if (!StringHelper.isNullOrEmptyString(base64)) {
|
// if (!StringHelper.isNullOrEmptyString(base64)) {
|
||||||
String image = URLEncoder.encode(base64, "utf-8");
|
// String image = URLEncoder.encode(base64, "utf-8");
|
||||||
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
// MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
||||||
RequestBody body = RequestBody.create(mediaType, "image=" + image + "&id_card_side="+types);
|
// RequestBody body = RequestBody.create(mediaType, "image=" + image + "&id_card_side="+types);
|
||||||
Request request = new Request.Builder()
|
// Request request = new Request.Builder()
|
||||||
.url("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getAccessToken())
|
// .url("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getAccessToken())
|
||||||
.method("POST", body)
|
// .method("POST", body)
|
||||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
// .addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||||
.addHeader("Accept", "application/json")
|
// .addHeader("Accept", "application/json")
|
||||||
.build();
|
// .build();
|
||||||
Response response = HTTP_CLIENT.newCall(request).execute();
|
// Response response = HTTP_CLIENT.newCall(request).execute();
|
||||||
String jsonString = response.body().string();
|
// String jsonString = response.body().string();
|
||||||
System.err.println(jsonString);
|
// System.err.println(jsonString);
|
||||||
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
// JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
||||||
JSONObject resultObject = jsonObject.getJSONObject("words_result");
|
// JSONObject resultObject = jsonObject.getJSONObject("words_result");
|
||||||
ProAuthorizeDetails bean = new ProAuthorizeDetails();
|
// ProAuthorizeDetails bean = new ProAuthorizeDetails();
|
||||||
if (type==1){
|
// if (type==1){
|
||||||
String name = resultObject.getJSONObject("姓名").getString("words");
|
// String name = resultObject.getJSONObject("姓名").getString("words");
|
||||||
String idNumber = resultObject.getJSONObject("公民身份号码").getString("words");
|
// String idNumber = resultObject.getJSONObject("公民身份号码").getString("words");
|
||||||
String sex = resultObject.getJSONObject("性别").getString("words");
|
// String sex = resultObject.getJSONObject("性别").getString("words");
|
||||||
String birth = resultObject.getJSONObject("出生").getString("words");
|
// String birth = resultObject.getJSONObject("出生").getString("words");
|
||||||
String address = resultObject.getJSONObject("住址").getString("words");
|
// String address = resultObject.getJSONObject("住址").getString("words");
|
||||||
String nation = resultObject.getJSONObject("民族").getString("words");
|
// String nation = resultObject.getJSONObject("民族").getString("words");
|
||||||
bean.setName(name);
|
// bean.setName(name);
|
||||||
bean.setIdNumber(idNumber);
|
// bean.setIdNumber(idNumber);
|
||||||
bean.setFrontUrl(filePath);
|
// bean.setFrontUrl(filePath);
|
||||||
} else if (type==2){
|
// } else if (type==2){
|
||||||
String time = resultObject.getJSONObject("失效日期").getString("words");
|
// String time = resultObject.getJSONObject("失效日期").getString("words");
|
||||||
bean.setBackUrl(filePath);
|
// bean.setBackUrl(filePath);
|
||||||
}
|
// }
|
||||||
return AjaxResult.success(bean);
|
// return AjaxResult.success(bean);
|
||||||
} else {
|
// } else {
|
||||||
return AjaxResult.error("图片识别失败,请重试");
|
// return AjaxResult.error("图片识别失败,请重试");
|
||||||
}
|
// }
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
log.error("图片识别失败", e.getMessage());
|
// log.error("图片识别失败", e.getMessage());
|
||||||
return AjaxResult.error("图片识别失败,请重试");
|
return AjaxResult.error("图片识别失败,请重试");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -410,30 +410,30 @@ public class ProAuthorizeServiceImpl implements ProAuthorizeService {
|
||||||
* @return 鉴权签名(Access Token)
|
* @return 鉴权签名(Access Token)
|
||||||
* @throws IOException IO异常
|
* @throws IOException IO异常
|
||||||
*/
|
*/
|
||||||
static String getAccessToken() throws IOException {
|
// static String getAccessToken() throws IOException {
|
||||||
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
// MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
|
||||||
RequestBody body=null;
|
// RequestBody body=null;
|
||||||
//生成随机数获取应用key
|
// //生成随机数获取应用key
|
||||||
Random random = new Random();
|
// Random random = new Random();
|
||||||
int i = random.nextInt(2);
|
// int i = random.nextInt(2);
|
||||||
if (i == 0){
|
// if (i == 0){
|
||||||
body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
|
// body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
|
||||||
+ "&client_secret=" + SECRET_KEY);
|
// + "&client_secret=" + SECRET_KEY);
|
||||||
} else {
|
// } else {
|
||||||
body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY_2
|
// body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY_2
|
||||||
+ "&client_secret=" + SECRET_KEY_2);
|
// + "&client_secret=" + SECRET_KEY_2);
|
||||||
}
|
// }
|
||||||
Request request = new Request.Builder()
|
// Request request = new Request.Builder()
|
||||||
.url("https://aip.baidubce.com/oauth/2.0/token")
|
// .url("https://aip.baidubce.com/oauth/2.0/token")
|
||||||
.method("POST", body)
|
// .method("POST", body)
|
||||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
// .addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||||
.build();
|
// .build();
|
||||||
Response response = HTTP_CLIENT.newCall(request).execute();
|
// Response response = HTTP_CLIENT.newCall(request).execute();
|
||||||
String jsonString = response.body().string();
|
// String jsonString = response.body().string();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
// JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
||||||
String accessToken = jsonObject.getString("access_token");
|
// String accessToken = jsonObject.getString("access_token");
|
||||||
return accessToken;
|
// return accessToken;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件base64编码
|
* 获取文件base64编码
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue