java 證件識別_證件識別接口JAVA調(diào)用示例

此篇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);

//?獲取請求實體

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();

}

————————————————

版權(quán)聲明:本文為CSDN博主「南燕Jo」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/weixin_29009669/article/details/115044452

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容