首先去百度申请接口
地址:https://login.bce.baidu.com/
有免费的额度
public static String getAuth() {
try{
String url = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = url
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ""
// 3. 官网获取的 Secret Key
+ "&client_secret=" + "";
HttpGet httpGet = new HttpGet(getAccessTokenUrl);
HttpClientBuilder httpClientBuilder = HttpClients.custom();
CloseableHttpClient httpClient = httpClientBuilder.build();
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpResponseEntity = httpResponse.getEntity();
String responseEntityStr = EntityUtils.toString(httpResponseEntity);
httpResponse.close();
JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
String access_token = jsonObject.getString("access_token");
return access_token;
}catch (Exception e){
log.error("获取百度ORC授权失败:"+e.getMessage());
}
return null;
}
/**
* 识别身份证
* @param access_token 权限
* @param imgUrl url
* @param id_card_side 正面反面标识
* @return
*/
public static JSONObject idcard(String access_token,String imgUrl,String id_card_side){
try{
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("access_token", access_token));
list.add(new BasicNameValuePair("url", imgUrl));
list.add(new BasicNameValuePair("id_card_side", id_card_side));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpClientBuilder httpClientBuilder = HttpClients.custom();
CloseableHttpClient httpClient = httpClientBuilder.build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpResponseEntity = httpResponse.getEntity();
String responseEntityStr = EntityUtils.toString(httpResponseEntity);
httpResponse.close();
JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
return jsonObject;
}catch (Exception e){
log.error("识别身份证失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"+e.getMessage());
}
return null;
}
/**
* 识别营业执照
* @param access_token
* @param imgUrl
* @return
*/
public static JSONObject business_license(String access_token,String imgUrl){
try{
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license";
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("access_token", access_token));
list.add(new BasicNameValuePair("url", imgUrl));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpClientBuilder httpClientBuilder = HttpClients.custom();
CloseableHttpClient httpClient = httpClientBuilder.build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpResponseEntity = httpResponse.getEntity();
String responseEntityStr = EntityUtils.toString(httpResponseEntity);
httpResponse.close();
JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
return jsonObject;
}catch (Exception e){
log.error("识别营业执照失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"+e.getMessage());
}
return null;
}