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