日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java 证件识别_证件识别接口JAVA调用示例

發(fā)布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 证件识别_证件识别接口JAVA调用示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

此篇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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。