前言
百度識(shí)別api是一個(gè)非常實(shí)用的工具,他可以非常有效的進(jìn)行身份信息讀取,我們可以通過他來獲取到身份證上的所有信息,百度識(shí)別不管只具有身份證識(shí)別,還有很多證件的識(shí)別api。
接下來我們說具體實(shí)現(xiàn)流程:首先要引用SDK包,然后獲取百度地圖提供的accessToken碼,然后存入圖片路徑進(jìn)行數(shù)據(jù)返回。
1.引入百度地圖SDK包
(1)在http://ai.baidu.com/sdk下載SDK
(2)將下載的aip-java-sdk-version.zip解壓后,復(fù)制到工程文件夾中。
(3)在Eclipse右鍵“工程 -> Properties -> Java Build Path -> Add JARs”。
(4)添加SDK工具包aip-java-sdk-version.jar和第三方依賴工具包json-20160810.jar log4j-1.2.17.jar。
2.獲取accessToken
package com.telematics.wx.util;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
獲取token類
/
public class AuthService {
public static void main(String[] args) {
String str= getAuth();
System.out.println(str);
}
/*
/**
}
3.進(jìn)行識(shí)別
@RequestMapping(“test”)
@ResponseBody
public String test(HttpServletRequest request,HttpServletResponse response, ModelMap model,HttpSession session){
System.out.println(“進(jìn)入身份識(shí)別”);
// 身份證識(shí)別url
String idcardIdentificate = “https://aip.baidubce.com/rest/2.0/ocr/v1/idcard”;
// 本地圖片路徑
String path=session.getServletContext().getRealPath("/upload");
System.out.println(“根目錄路徑” path);
String filePath = path "\" “shenfenzheng.jpg”;
System.out.println(“圖片路徑” filePath);
try {
byte[] imgData = FileUtil.readFileByBytes(filePath);//使用工具類進(jìn)行轉(zhuǎn)碼
String imgStr = Base64Util.encode(imgData);//百度地圖的所有圖片均需要base64編碼、去掉編碼頭后再進(jìn)行urlencode。//import com.baidu.aip.util.Base64Util;
// 識(shí)別身份證正面id_card_side=front;識(shí)別身份證背面id_card_side=back;
String params = “id_card_side=front&” URLEncoder.encode(“image”, “UTF-8”) “=”
URLEncoder.encode(imgStr, “UTF-8”);
String token=AuthService.getAuth();//調(diào)用上面的accessToken碼代碼
String accessToken = token;
String result = HttpUtil.post(idcardIdentificate, accessToken, params);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return “0”;
}
4.工具類
package com.goocom.util;
import java.io.*;
/**
文件讀取工具類
*/
public class FileUtil {
/**
讀取文件內(nèi)容,作為字符串返回
*/
public static String readFileAsString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
if (file.length() > 1024 * 1024 * 1024) {
throw new IOException(“File is too large”);
}
StringBuilder sb = new StringBuilder((int) (file.length()));
// 創(chuàng)建字節(jié)輸入流
FileInputStream fis = new FileInputStream(filePath);
// 創(chuàng)建一個(gè)長(zhǎng)度為10240的Buffer
byte[] bbuf = new byte[10240];
// 用于保存實(shí)際讀取的字節(jié)數(shù)
int hasRead = 0;
while ( (hasRead = fis.read(bbuf)) > 0 ) {
sb.append(new String(bbuf, 0, hasRead));
}
fis.close();
return sb.toString();
}
/**
根據(jù)文件路徑讀取byte[] 數(shù)組
*/
public static byte[] readFileByBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try { in = new BufferedInputStream(new FileInputStream(file)); short bufSize = 1024; byte[] buffer = new byte[bufSize]; int len1; while (-1 != (len1 = in.read(buffer, 0, bufSize))) { bos.write(buffer, 0, len1); } byte[] var7 = bos.toByteArray(); return var7; } finally { try { if (in != null) { in.close(); } } catch (IOException var14) { var14.printStackTrace(); } bos.close(); }
}
}
}
到此結(jié)束,第一次寫博客有些不太熟練希望大家諒解,有什么要求和評(píng)論希望大家一起學(xué)習(xí)。