import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.json.JSONObject;
public class BaiduOCRHttpUtil {
private final static String BAIDU_APP_CLIENT_ID = "IDSPpc10YHtORRV9T5ia9grN";
private final static String BAIDU_APP_CLIENT_SECRET = "IDSPpc10YHtORRV9T5ia9grN";
private final static String PASSPORT_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/passport";
private final static String IMG_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage";
private final static String ACCESS_TOKE_URL = "https://aip.baidubce.com/oauth/2.0/token";
private final static String ID_CARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
private final static Logger log = Logger.getLogger(BaiduOCRHttpUtil.class);
public static String getAccessToken() {
try {
String getAccessTokenUrl = String
.format("%s?grant_type=client_credentials&client_id=%s&client_secret=%s",
ACCESS_TOKE_URL, BAIDU_APP_CLIENT_ID,
BAIDU_APP_CLIENT_SECRET);
String accessTokenInfo = getUrlResultData(getAccessTokenUrl);
JSONObject accessTokenJson = new JSONObject(accessTokenInfo);
if (accessTokenJson.has("error")) {
throw new RuntimeException(
accessTokenJson.getString("error_description"));
}
return accessTokenJson.getString("access_token");
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
public static String inputStreamToString(InputStream in)
throws UnsupportedEncodingException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
String imgStr = Base64Util.encode(baos.toByteArray());
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
return imgParam;
}
public static String passport(String accessToken, String imgParam)
throws RuntimeException {
try {
if (accessToken == null || accessToken.length() == 0) {
throw new RuntimeException("accessToken为空");
}
if (imgParam == null || imgParam.length() == 0) {
throw new RuntimeException("护照图片为空");
}
if (imgParam.getBytes().length > 4 * 1024 * 1024) {
throw new RuntimeException("图片超出4M");
}
String passportInfo = post(PASSPORT_URL, accessToken, "image="
+ imgParam);
if (passportInfo == null || passportInfo.length() == 0) {
throw new RuntimeException("护照识别失败");
}
JSONObject passportJson = new JSONObject(passportInfo);
if (passportJson.getInt("words_result_num") < 1) {
throw new RuntimeException("护照未扫描出信息");
}
return passportInfo;
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
public static String idcard(String accessToken, String imgParam) {
try {
if (accessToken == null || accessToken.length() == 0) {
throw new RuntimeException("accessToken为空");
}
if (imgParam == null || imgParam.length() == 0) {
throw new RuntimeException("护照图片为空");
}
if (imgParam.getBytes().length > 4 * 1024 * 1024) {
throw new RuntimeException("图片超出4M");
}
String param = "id_card_side=" + "front" + "&image=" + imgParam;
String idcardInfo = post(ID_CARD_URL, accessToken, param);
if (idcardInfo == null || idcardInfo.length() == 0) {
throw new RuntimeException("身份证识别失败");
}
JSONObject jsonObject = new JSONObject(idcardInfo);
if (jsonObject.getInt("words_result_num") < 1) {
throw new RuntimeException("身份证未扫描出信息");
}
return idcardInfo;
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
public static String img(String accessToken, String imgParam) {
try {
if (accessToken == null || accessToken.length() == 0) {
throw new RuntimeException("accessToken为空");
}
if (imgParam == null || imgParam.length() == 0) {
throw new RuntimeException("护照图片为空");
}
if (imgParam.getBytes().length > 4 * 1024 * 1024) {
throw new RuntimeException("图片超出4M");
}
String param = "image=" + imgParam;
String result = post(IMG_URL, accessToken, param);
if (result == null || result.length() == 0) {
throw new RuntimeException("图片识别失败");
}
JSONObject jsonObject = new JSONObject(result);
if (jsonObject.getInt("words_result_num") < 1) {
throw new RuntimeException("图片未扫描出信息");
}
return result;
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
private static String post(String requestUrl, String accessToken,
String params) throws Exception {
String contentType = "application/x-www-form-urlencoded";
return post(requestUrl, accessToken, contentType, params);
}
private static String post(String requestUrl, String accessToken,
String contentType, String params) throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return post(requestUrl, accessToken, contentType, params, encoding);
}
private static String post(String requestUrl, String accessToken,
String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return postGeneralUrl(url, contentType, params, encoding);
}
private static String postGeneralUrl(String generalUrl, String contentType,
String params, String encoding) throws Exception {
URL url = new URL(generalUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
connection.connect();
Map<String, List<String>> headers = connection.getHeaderFields();
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
return result;
}
private static String getUrlResultData(String url) {
HttpURLConnection http = null;
try {
URL ur = new URL(url);
http = (HttpURLConnection) ur.openConnection();
http.setConnectTimeout(10000);
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.connect();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
http.getOutputStream(), "UTF-8"));
out.flush();
InputStream is = http.getInputStream();
byte[] b = new byte[1024];
int c = -1;
StringBuilder ret = new StringBuilder();
while ((c = is.read(b)) != -1) {
ret.append(new String(b, 0, c, "UTF-8"));
}
String decode = URLDecoder.decode(ret.toString(), "UTF-8");
return decode;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
class Base64Util {
private static final char last2byte = (char) Integer
.parseInt("00000011", 2);
private static final char last4byte = (char) Integer
.parseInt("00001111", 2);
private static final char last6byte = (char) Integer
.parseInt("00111111", 2);
private static final char lead6byte = (char) Integer
.parseInt("11111100", 2);
private static final char lead4byte = (char) Integer
.parseInt("11110000", 2);
private static final char lead2byte = (char) Integer
.parseInt("11000000", 2);
private static final char[] encodeTable = new char[] { 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };
public Base64Util() {
}
public static String encode(byte[] from) {
StringBuilder to = new StringBuilder(
(int) ((double) from.length * 1.34D) + 3);
int num = 0;
char currentByte = 0;
int i;
for (i = 0; i < from.length; ++i) {
for (num %= 8; num < 8; num += 6) {
switch (num) {
case 0:
currentByte = (char) (from[i] & lead6byte);
currentByte = (char) (currentByte >>> 2);
case 1:
case 3:
case 5:
default:
break;
case 2:
currentByte = (char) (from[i] & last6byte);
break;
case 4:
currentByte = (char) (from[i] & last4byte);
currentByte = (char) (currentByte << 2);
if (i + 1 < from.length) {
currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
}
break;
case 6:
currentByte = (char) (from[i] & last2byte);
currentByte = (char) (currentByte << 4);
if (i + 1 < from.length) {
currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
}
}
to.append(encodeTable[currentByte]);
}
}
if (to.length() % 4 != 0) {
for (i = 4 - to.length() % 4; i > 0; --i) {
to.append("=");
}
}
return to.toString();
}
}