|
@@ -0,0 +1,183 @@
|
|
|
+package com.nuliji.tools.third.baidu;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.nuliji.tools.Tools;
|
|
|
+import com.nuliji.tools.Transform;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class BaiduAi {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(BaiduAi.class);
|
|
|
+
|
|
|
+ final static String api = "https://aip.baidubce.com/rest/2.0/";
|
|
|
+
|
|
|
+ private String appKey = "";
|
|
|
+ private String appSecret = "";
|
|
|
+
|
|
|
+ private static Map<String, String> tokenMap = new HashMap<>();
|
|
|
+ private static Map<String, Date> tokenExpiresIn = new HashMap<>();
|
|
|
+
|
|
|
+ public BaiduAi(String appKey, String appSecret){
|
|
|
+ this.appKey = appKey;
|
|
|
+ this.appSecret = appSecret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum IdcardSide {
|
|
|
+ FRONT("front"), BACK("back");
|
|
|
+ public String key;
|
|
|
+ private IdcardSide(String key){
|
|
|
+ this.key = key;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static IdcardSide ValueOf(String name){
|
|
|
+ if (name == null) return null;
|
|
|
+ return IdcardSide.valueOf(name.toUpperCase());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public enum IdcardResult {
|
|
|
+ ADDRESS("住址"), ID("公民身份号码"), BIRTH("出生"), NAME("姓名"), GENDER("性别"), NATION("民族");
|
|
|
+ public String key;
|
|
|
+ private IdcardResult(String key){
|
|
|
+ this.key = key;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static IdcardResult ValueOf(String name){
|
|
|
+ if (name == null) return null;
|
|
|
+ return IdcardResult.valueOf(name.toUpperCase());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public class IdcardResponse{
|
|
|
+ private Integer direction;
|
|
|
+ private String image_status;
|
|
|
+ private String risk_type;
|
|
|
+ private String edit_tool;
|
|
|
+ private JSONObject words_result;
|
|
|
+ private Integer words_result_num;
|
|
|
+
|
|
|
+ public Integer getDirection() {
|
|
|
+ return direction;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setDirection(Integer direction) {
|
|
|
+ this.direction = direction;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getImage_status() {
|
|
|
+ return image_status;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setImage_status(String image_status) {
|
|
|
+ this.image_status = image_status;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getRisk_type() {
|
|
|
+ return risk_type;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRisk_type(String risk_type) {
|
|
|
+ this.risk_type = risk_type;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getEdit_tool() {
|
|
|
+ return edit_tool;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setEdit_tool(String edit_tool) {
|
|
|
+ this.edit_tool = edit_tool;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject getWords_result() {
|
|
|
+ return words_result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setWords_result(JSONObject words_result) {
|
|
|
+ this.words_result = words_result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getWords_result_num() {
|
|
|
+ return words_result_num;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setWords_result_num(Integer words_result_num) {
|
|
|
+ this.words_result_num = words_result_num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 身份证识别
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public IdcardResponse idcard(IdcardSide idCardSide, boolean detectDirection, boolean detectRisk, byte[] image) {
|
|
|
+ MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
|
|
+ params.add("image", Tools.encodeBase64(image));
|
|
|
+ params.add("id_card_side", idCardSide.key);
|
|
|
+ params.add("detect_direction", detectDirection ? "true" : "false");
|
|
|
+ params.add("detect_risk", detectRisk ? "true" : "false");
|
|
|
+
|
|
|
+ return Transform.convert(execute(api+"ocr/v1/idcard?access_token"+getAccessToken(appKey, appSecret), params), IdcardResponse.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取微信公众平台accesstoken
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private synchronized String getAccessToken(String appKey, String appSecret) {
|
|
|
+ Date date = new Date();
|
|
|
+ // 系统刚启动时,或者离上次取accessToken有7180秒以上时,重新取它
|
|
|
+ if (!tokenMap.containsKey(appKey) || date.getTime() / 1000 - tokenExpiresIn.get(appKey).getTime() / 1000 >= 2592000) {
|
|
|
+ try {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("grant_type", "client_credential");
|
|
|
+ params.put("client_id", appKey);
|
|
|
+ params.put("client_secret", appSecret);
|
|
|
+ String paramStr = "";
|
|
|
+ for(String key : params.keySet()){
|
|
|
+ String value = params.get(key);
|
|
|
+ value = URLEncoder.encode(value, "UTF-8");
|
|
|
+ paramStr = paramStr + key + "=" + value + "&";
|
|
|
+ }
|
|
|
+ JSONObject object = execute("https://aip.baidubce.com/oauth/2.0/token?"+paramStr, null);
|
|
|
+ tokenMap.put(appKey, object.getString("accessToken"));
|
|
|
+ tokenExpiresIn.put(appKey, date);
|
|
|
+ logger.info("getAccessToken:" + tokenMap.get(appKey));
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return tokenMap.get(appKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject execute(String api, MultiValueMap<String, Object> params) throws RuntimeException {
|
|
|
+ try {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(params, headers);
|
|
|
+ String result = restTemplate.postForObject(api, formEntity, String.class);
|
|
|
+ logger.info("BaiduAi返回结果:" + result);
|
|
|
+ JSONObject a = JSONObject.parseObject(result);
|
|
|
+
|
|
|
+ if (a.getInteger("status") == 0) {
|
|
|
+ throw new Exception("BaiduAi接口错误: " + result);
|
|
|
+ }
|
|
|
+ return a;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|