封装好的调用百度OCR的工具类
package com.zx.test.util;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class BaiduOcrUtils {
public static String CLIENT_ID;
public static String CLIENT_SECRET;
public static String TOKEN_URL;
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
static {
try {
CLIENT_ID = PropertiesReaderUtil.getProperties("baidu.clientId");
CLIENT_SECRET = PropertiesReaderUtil.getProperties("baidu.clientSecret");
TOKEN_URL = PropertiesReaderUtil.getProperties("baidu.token.url");
} catch (Exception e) {
e.printStackTrace();
}
}
public static JSONObject upload(String url, String body) {
Map<String, Object> paramMap = new HashMap(3);
paramMap.put("grant_type", "client_credentials");
paramMap.put("client_id", CLIENT_ID);
paramMap.put("client_secret", CLIENT_SECRET);
String result = null;
try {
result = HttpUtil.post(TOKEN_URL, paramMap);
} catch (Exception e) {
throw new BaseException(ResponseCode.EXTERNAL_INTERFACE_ERROR.getCode(), CommonConstant.RESPONSE_ERROR_READ, null);
}
String accessToken = JSONObject.parseObject(result).getString("access_token");
if (accessToken.isEmpty()) {
throw new ErrorSignatureException();
}
url = url + "?access_token=" + accessToken;
String responseBody = null;
try {
responseBody = HttpUtil.createPost(url).contentType("application/x-www-form-urlencoded")
.body(body).execute().body();
} catch (Exception e) {
throw new BaseException(ResponseCode.EXTERNAL_INTERFACE_ERROR.getCode(), "请求识别接口异常,请稍后再试~", null);
}
JSONObject response = JSONObject.parseObject(responseBody);
if (response.containsKey("words_result")) {
return response.getJSONObject("words_result");
}
return response;
}
public static String getAccessToken() {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + CLIENT_ID
+ "&client_secret=" + CLIENT_SECRET);
Request request = new Request.Builder()
.url(TOKEN_URL)
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = null;
try {
response = HTTP_CLIENT.newCall(request).execute();
return new org.json.JSONObject(response.body().string()).getString("access_token");
} catch (IOException e) {
throw new BaseException(ResponseCode.EXTERNAL_INTERFACE_ERROR.getCode(), CommonConstant.RESPONSE_ERROR_READ, null);
}
}
public static JSONObject ocrUpload(String url, String base64) {
try {
String result = com.wonders.eshiyun.common.utils.ocr.HttpUtil.post(
url,
getAccessToken(),
"image=" + base64);
System.out.println(result);
return JSONObject.parseObject(result);
} catch (Exception e) {
throw new BaseException(ResponseCode.EXTERNAL_INTERFACE_ERROR.getCode(), CommonConstant.RESPONSE_ERROR_READ, null);
}
}
public static void main(String[] args) throws Exception {
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/passport";
String filePath = "C:\\Users\\zx\\Desktop\\护照.png";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
JSONObject jsonObject = ocrUpload(url,imgParam);
System.out.println(jsonObject.toString());
}
}