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;
/**
* http 工具类
*
* @since 1.6
*/
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);
/**
* 获取百度的AccessToken 参考 <a href='http://ai.baidu.com/docs#/Auth/top'>百度Access
* Token获取</a>
*
* @return 百度的AccessToken
* @exception RuntimeException
* 1.unknown client id(API Key不正确)<br/>
* 2.Client authentication failed(Secret Key不正确)<br/>
* 3 other(访问不通等等)<br/>
*/
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());
}
}
/**
* InputStream会取一次会关闭,使用下面这些InputStream要注意文件是否要保存<br/>
* MultipartFile的InputStream <br/>
* req.getInputStream<br/>
* httpconn.getInputStream<br/>
*
* @return
* @throws IOException
* @throws UnsupportedEncodingException
* <br/>
* <code>URLEncoder.encode(imgStr, "UTF-8")</code>
*/
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;
}
/**
* 通过护照图片获取护照信息 <br/>
* 支持对中国大陆居民护照的资料页进行结构化识别,包含国家码、姓名、姓名拼音、性别、护照号、出生日期、出生地点、签发日期、有效期至、签发地点。<br/>
* 参考 <a
* href='http://ai.baidu.com/docs#/OCR-API-Passport/top'>百度文字识别-护照识别</a>
* <a
* href='http://ai.baidu.com/docs#/OCR-API-Passport/top'>百度文字识别-通用文字识别</a>
*
* @param imgParam
* 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,
* 最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
* @param accessToken
* 百度的AccessToken,通过{@link #getAccessToken()}获取
* @exception RuntimeException
* 参数校验 accessToken为空,护照图片为空,图片超出4M,护照未扫描出信息,访问不通...<br/>
*
* @return 1
*/
public static String passport(String accessToken, String imgParam)
throws RuntimeException {
try {
if (accessToken == null || accessToken.length() == 0) {
百度开发者文字识别(身份证+图片+护照)
最新推荐文章于 2020-08-11 15:56:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)