java 证件识别_证件识别接口JAVA调用示例
此篇java文章是基本聚合數(shù)據(jù)(http://www.juhe.cn)-----證件識別接口來演示,基本HTTP?POST請求上傳圖片并接收J(rèn)SON數(shù)據(jù)來處理,其他與上傳圖片相關(guān)的接口請求可參考此篇文章。
使用前你需要:
①:通過?http://www.juhe.cn/docs/api/id/153申請一個名片識別的appkey
1.支持的證件類型清單
此接口可通過GET請求得到結(jié)果,java網(wǎng)絡(luò)請求有HttpClient相關(guān)工具包及HttpURLConnection相關(guān)的包等,這里用的是HttpClient,需要先導(dǎo)包,如果用maven話會更方便直接把:
org.apache.httpcomponents
httpmime
4.3.6
org.apache.httpcomponents
httpclient
4.4.1
復(fù)制到配置文件?pom.xml
請求代碼如下:
public?static?String?get()?throws?IOException?{
//?創(chuàng)建HttpClient對象
CloseableHttpClient?httpClient?=?HttpClients.createDefault();
CloseableHttpResponse?response?=?null;
String?result?=?null;
try?{
HttpGet?httpGet?=?new?HttpGet("http://api2.juheapi.com/cardrecon/supportlist?key="+?appKey);
//?執(zhí)行網(wǎng)絡(luò)請求
response?=?httpClient.execute(httpGet);
//?獲取請求實(shí)體
HttpEntity?resEntity?=?response.getEntity();
if?(resEntity?!=?null)?{
//?ConverStreamToString是下面寫的一個方法是把網(wǎng)絡(luò)請求的字節(jié)流轉(zhuǎn)換為utf8的字符串
result?=?ConvertStreamToString(resEntity.getContent(),?"UTF-8");
}
EntityUtils.consume(resEntity);
}?catch?(Exception?e)?{
}?finally?{
//?關(guān)閉請求
response.close();
httpClient.close();
}
//?得到的是JSON類型的數(shù)據(jù)需要第三方解析JSON的jar包來解析
return?result;
}
2.證件圖片識別
//?此方法是POST請求上傳的參數(shù)中包含本地圖片信息File類型
public?static?String?post(String?type,?File?file)?throws?Exception?{
CloseableHttpClient?httpClient?=?HttpClients.createDefault();
CloseableHttpResponse?response?=?null;
String?result?=?null;
//?HttpClient請求的相關(guān)設(shè)置,可以不用配置,用默認(rèn)的參數(shù),這里設(shè)置連接和超時時長(毫秒)
RequestConfig?config?=?RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
try?{
HttpPost?httppost?=?new?HttpPost("http://api2.juheapi.com/cardrecon/upload");
//?FileBody封裝File類型的參數(shù)
FileBody?bin?=?new?FileBody(file);
//?StringBody封裝String類型的參數(shù)
StringBody?keyBody?=?new?StringBody(key,?ContentType.TEXT_PLAIN);
StringBody?typeBody?=?new?StringBody(type,?ContentType.TEXT_PLAIN);
//?addPart將參數(shù)傳入,并指定參數(shù)名稱
HttpEntity?reqEntity?=?MultipartEntityBuilder.create()
.addPart("pic",?bin).addPart("key",?keyBody)
.addPart("cardType",?typeBody).build();
httppost.setEntity(reqEntity);
httppost.setConfig(config);
//?執(zhí)行網(wǎng)絡(luò)請求并返回結(jié)果
response?=?httpClient.execute(httppost);
HttpEntity?resEntity?=?response.getEntity();
if?(resEntity?!=?null)?{
result?=?ConvertStreamToString(resEntity.getContent(),?"UTF-8");
}
EntityUtils.consume(resEntity);
}?finally?{
response.close();
httpClient.close();
}
//?得到的是JSON類型的數(shù)據(jù)需要第三方解析JSON的jar包來解析
return?result;
}
//?此方法是把傳進(jìn)的字節(jié)流轉(zhuǎn)化為相應(yīng)的字符串并返回,此方法一般在網(wǎng)絡(luò)請求中用到
public?static?String?ConvertStreamToString(InputStream?is,?String?charset)
throws?Exception?{
StringBuilder?sb?=?new?StringBuilder();
try?(InputStreamReader?inputStreamReader?=?new?InputStreamReader(is,charset))?{
try?(BufferedReader?reader?=?new?BufferedReader(inputStreamReader))?{
String?line?=?null;
while?((line?=?reader.readLine())?!=?null)?{
sb.append(line).append("\r\n");
}
}
}
return?sb.toString();
}
總結(jié)
以上是生活随笔為你收集整理的java 证件识别_证件识别接口JAVA调用示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java添加窗体_添加的窗体
- 下一篇: jdbc mysql查询显示图片_JDB