此java文章是基本聚合數據證件識別接口來演示,基本HTTP POST請求上傳圖片並接收JSON數據來處理。
使用前你需要通過
https://www.juhe.cn/docs/api/id/153
申請一個名片識別的appkey
1.支持的證件類型清單
請求地址:http://api2.juheapi.com/cardrecon/supportlist?key=您申請的appkey
此接口可通過GET請求得到結果,java網絡請求有HttpClient相關工具包及HttpURLConnection相關的包等,這里用的是HttpClient,需要先導包,如果用maven話會更方便直接把:
<dependency><groupid>org.apache.httpcomponents</groupid> <artifactid>httpmime</artifactid> <version>4.3.6</version> </dependency> <dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.4.1</version> </dependency>
復制到配置文件 pom.xml
請求代碼如下:
public static String get() throws IOException {
// 創建HttpClient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
try {
HttpGet httpGet = new HttpGet("http://api2.juheapi.com/cardrecon/supportlist?key="+ appKey);
// 執行網絡請求
response = httpClient.execute(httpGet);
// 獲取請求實體
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// ConverStreamToString是下面寫的一個方法是把網絡請求的字節流轉換為utf8的字符串
result = ConvertStreamToString(resEntity.getContent(), "UTF-8");
}
EntityUtils.consume(resEntity);
} catch (Exception e) {
} finally {
// 關閉請求
response.close();
httpClient.close();
}
// 得到的是JSON類型的數據需要第三方解析JSON的jar包來解析
return result;
}
2.證件圖片識別
請求地址:http://api2.juheapi.com/cardrecon/upload
// 此方法是POST請求上傳的參數中包含本地圖片信息File類型
public static String post(String type, File file) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
// HttpClient請求的相關設置,可以不用配置,用默認的參數,這里設置連接和超時時長(毫秒)
RequestConfig config = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
try {
HttpPost httppost = new HttpPost("http://api2.juheapi.com/cardrecon/upload");
// FileBody封裝File類型的參數
FileBody bin = new FileBody(file);
// StringBody封裝String類型的參數
StringBody keyBody = new StringBody(key, ContentType.TEXT_PLAIN);
StringBody typeBody = new StringBody(type, ContentType.TEXT_PLAIN);
// addPart將參數傳入,並指定參數名稱
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("pic", bin).addPart("key", keyBody)
.addPart("cardType", typeBody).build();
httppost.setEntity(reqEntity);
httppost.setConfig(config);
// 執行網絡請求並返回結果
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類型的數據需要第三方解析JSON的jar包來解析
return result;
}
// 此方法是把傳進的字節流轉化為相應的字符串並返回,此方法一般在網絡請求中用到
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();
}