package com.zhgd.xmgl.util; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.*; /** * @program: wisdomSite * @description: 身份证工具类 * @author: Mr.Peng * @create: 2021-03-10 10:04 **/ @Component @Slf4j public class IdCardUtils { private static String tokenurl = "https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens"; private static String username; private static String domainname; private static String password; private static String projectid; private static String endpoint; /** * 是否启用华为云账号功能 */ private static Boolean enable; @Value("${hw-ocr-enable:false}") public void setEnable(Boolean enable) { IdCardUtils.enable = enable; } @Value("${hw-ocr-username}") public void setUsername(String hwusername) { username = hwusername; } @Value("${hw-ocr-domainname}") public void setDomainname(String hwdomainname) { domainname = hwdomainname; } @Value("${hw-ocr-password}") public void setPassword(String hwpassword) { password = hwpassword; } @Value("${hw-ocr-projectid}") public void setProjectid(String hwprojectid) { projectid = hwprojectid; } @Value("${hw-ocr-endpoint}") public void setEndpoint(String hwendpoint) { endpoint = hwendpoint; } /** * 识别身份证信息 * * @param imageurl 图片地址 * @param side true 正面(头像面) false 反面(国徽面) *

* 注意事项:此处申请华为云必须在华为云上海一区开通身份证识别功能,然后拷贝项目ID到此处进行识别 */ public static String getIdCardInfo(String imageurl, boolean side) { if (enable) { // String JSONSTR = "{ \n \"auth\": { \n \"identity\": { \n \"methods\": [ \n \"password\" \n ], \n \"password\": { \n \"user\": { \n\"name\": \"" + username + "\", \n \"password\": \"" + password + "\", \n \"domain\": { \n \"name\": \"" + domainname + "\" \n } \n } \n} \n}, \n\"scope\": { \n \"project\": { \n\"name\": \"cn-east-3\" \n} \n} \n} \n}"; // String token = HttpRequest.post(tokenurl).body(String.valueOf(JSONUtil.parse(JSONSTR))).execute().header("X-Subject-Token"); // String url = "https://ocr.cn-east-3.myhuaweicloud.com/v2/" + projectid + "/ocr/id-card"; // Map param = new HashMap<>(); // param.put("url", imageurl); // param.put("side", side ? "front" : "back"); // String result = HttpRequest.post(url).header("X-Auth-Token", token).body(String.valueOf(JSONUtil.parse(param))).execute().body(); // log.info(result); // return result; // String tokenurl = "https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens"; // String JSONSTR = "{ \n \"auth\": { \n \"identity\": { \n \"methods\": [ \n \"password\" \n ], \n \"password\": { \n \"user\": { \n\"name\": \"" + username + "\", \n \"password\": \"" + password + "\", \n \"domain\": { \n \"name\": \"" + username + "\" \n } \n } \n} \n}, \n\"scope\": { \n \"project\": { \n\"name\": \"cn-east-3\" \n} \n} \n} \n}"; //获取token String tokenUrl = "https://iam." + endpoint + ".myhuaweicloud.com/v3/auth/tokens"; String JSONSTR = getTokenStr(domainname, username, password, endpoint); String tokenBody = JSONUtil.toJsonStr(JSONSTR); log.info("token_url:{},body:{}", tokenUrl, tokenBody); String token = HttpRequest.post(tokenUrl).body(tokenBody).execute().header("x-subject-token"); log.info("response-token:{}", token); //获取身份证信息 String cardUrl = "https://ocr." + endpoint + ".myhuaweicloud.com/v2/" + projectid + "/ocr/id-card"; Map param = new HashMap<>(); param.put("url", imageurl); param.put("side", side ? "front" : "back"); String cardBoday = JSONUtil.toJsonStr(param); log.info("token_url:{},body:{}", cardUrl, cardBoday); String result = HttpRequest.post(cardUrl).header("x-auth-token", token).body(cardBoday).execute().body(); log.info(result); return result; } return null; } public static String getTokenStr(String domainname, String username, String passwrd, String endpoint) { JSONObject domain = new JSONObject(); domain.put("name", domainname); JSONObject user = new JSONObject(); user.put("name", username); user.put("password", passwrd); user.put("domain", domain); JSONObject password = new JSONObject(); password.put("user", user); List methods = new ArrayList(); methods.add("password"); JSONObject identity = new JSONObject(); identity.put("methods", methods); identity.put("password", password); JSONObject project = new JSONObject(); project.put("name", endpoint); JSONObject scope = new JSONObject(); scope.put("project", project); JSONObject json = new JSONObject(); json.put("identity", identity); json.put("scope", scope); JSONObject data = new JSONObject(); data.put("auth", json); return data.toJSONString(); } /** * 通过身份证号码获取出生日期(birthday)、年龄(age)、性别(sex) * * @param idCardNo 身份证号码 * @return 返回的出生日期格式:1993-05-07 性别格式:1男,2女 */ public static Map getBirthdayAgeSex(String idCardNo) { String birthday = ""; String age = ""; String sexCode = ""; int year = Calendar.getInstance().get(Calendar.YEAR); char[] number = idCardNo.toCharArray(); boolean flag = true; if (number.length == 15) { for (int x = 0; x < number.length; x++) { if (!flag) { return new HashMap(); } flag = Character.isDigit(number[x]); } } else if (number.length == 18) { for (int x = 0; x < number.length - 1; x++) { if (!flag) { return new HashMap(); } flag = Character.isDigit(number[x]); } } if (flag && idCardNo.length() == 15) { birthday = "19" + idCardNo.substring(6, 8) + "-" + idCardNo.substring(8, 10) + "-" + idCardNo.substring(10, 12); sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 3, idCardNo.length())) % 2 == 0 ? "2" : "1"; age = (year - Integer.parseInt("19" + idCardNo.substring(6, 8))) + ""; } else if (flag && idCardNo.length() == 18) { birthday = idCardNo.substring(6, 10) + "-" + idCardNo.substring(10, 12) + "-" + idCardNo.substring(12, 14); sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 4, idCardNo.length() - 1)) % 2 == 0 ? "2" : "1"; age = (year - Integer.parseInt(idCardNo.substring(6, 10))) + ""; } Map map = new HashMap(); map.put("birthday", birthday); map.put("age", age); map.put("sex", sexCode); return map; } public static void main(String[] args) { String projectid = "0c9c4261fa80f43b2fdbc0103360d790"; String domainname = "yunhaiyun"; String username = "hqocr"; String password = "hq123456"; String endpoint = "cn-north-4"; String imageurl = "http://119.29.110.58:9000/itbgp/file/3b097a48-7e46-407c-9e32-fc725204c485.jpg"; String JSONSTR = getTokenStr(domainname, username, password, endpoint); String token = HttpRequest.post("https://iam." + endpoint + ".myhuaweicloud.com/v3/auth/tokens").body(JSONUtil.toJsonStr(JSONSTR)).execute().header("x-subject-token"); log.info(token); String url = "https://ocr." + endpoint + ".myhuaweicloud.com/v2/" + projectid + "/ocr/id-card"; Map param = new HashMap<>(); param.put("url", imageurl); param.put("side", "front"); String result = HttpRequest.post(url).header("x-auth-token", token).body(JSONUtil.toJsonStr(param)).execute().body(); log.info(result); } /*public static void main(String[] args) { String username="hqocr"; String domain="yunhaiyun"; String password="hq123456"; String projectid="0c9c4261fa80f43b2fdbc0103360d790"; String imageurl="http://119.29.110.58:9000/itbgp/file/3b097a48-7e46-407c-9e32-fc725204c485.jpg"; String JSONSTR=getTokenStr(domain,username,password,"cn-north-4"); String token = HttpRequest.post("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens").body(JSONUtil.toJsonStr(JSONSTR)).execute().header("x-subject-token"); log.info(token); }*/ }