You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					233 lines
				
				8.0 KiB
			
		
		
			
		
	
	
					233 lines
				
				8.0 KiB
			| 
											3 years ago
										 | package com.ynxbd.common.helper.common;
 | ||
|  | 
 | ||
|  | import com.baidu.aip.ocr.AipOcr;
 | ||
|  | import com.ynxbd.common.bean.Patient;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | import org.apache.commons.lang3.StringUtils;
 | ||
|  | import java.text.ParseException;
 | ||
|  | import java.text.SimpleDateFormat;
 | ||
|  | import java.util.Date;
 | ||
|  | import java.util.HashMap;
 | ||
|  | import java.util.Iterator;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @Author wsq
 | ||
|  |  * @Date 2020/10/12 11:56
 | ||
|  |  * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved.
 | ||
|  |  */
 | ||
|  | @Slf4j
 | ||
|  | public class IDNumberHelper {
 | ||
|  | 
 | ||
|  |     private static String APP_ID;
 | ||
|  |     private static String API_KEY;
 | ||
|  |     private static String SECRET_KEY;
 | ||
|  |     // front:身份证含照片的一面,back:身份证带国徽的一面,必须正确声明,否则"error_msg": "recognize id card error"
 | ||
|  |     private final static String ID_CARD_SIDE = "front";
 | ||
|  |     // 图片存储文件夹名
 | ||
|  |     private final static String FOLDER_NAME = "idCard";
 | ||
|  | 
 | ||
|  |     static {
 | ||
|  | //        Properties properties = FileHelper.readProperties("idcard.properties");
 | ||
|  | //        if (properties != null) {
 | ||
|  | //            APP_ID = FileHelper.propertiesGetString(properties, "baidu.appId");
 | ||
|  | //            API_KEY = FileHelper.propertiesGetString(properties, "baidu.apiKey");
 | ||
|  | //            SECRET_KEY = FileHelper.propertiesGetString(properties, "baidu.secretKey");
 | ||
|  | //        }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 根据身份证号获取性别
 | ||
|  |      *
 | ||
|  |      * @param idNumber 身份证号码
 | ||
|  |      * @return 性别
 | ||
|  |      */
 | ||
|  |     public static String getSex(String idNumber) {
 | ||
|  |         if (isValid(idNumber)) return null;
 | ||
|  | 
 | ||
|  |         String sex = null;
 | ||
|  |         if (StringUtils.isNotBlank(idNumber)) {
 | ||
|  |             if (idNumber.length() == 18) {
 | ||
|  |                 sex = Integer.parseInt(idNumber.substring(16, 17)) % 2 == 1 ? "男" : "女";
 | ||
|  |             } else {
 | ||
|  |                 sex = Integer.parseInt(idNumber.substring(14, 15)) % 2 == 1 ? "男" : "女";
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  |         return sex;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 根据身份证号获取年龄
 | ||
|  |      *
 | ||
|  |      * @param idNumber 身份证号码
 | ||
|  |      * @return 年龄
 | ||
|  |      */
 | ||
|  |     public static Integer getAge(String idNumber) {
 | ||
|  |         if (isValid(idNumber)) return null;
 | ||
|  | 
 | ||
|  |         String year, month, day;
 | ||
|  |         if (idNumber.length() == 18) {
 | ||
|  |             year = idNumber.substring(6, 10);// 得到年份
 | ||
|  |             month = idNumber.substring(10, 12);// 得到月份
 | ||
|  |             day = idNumber.substring(12, 14);//得到日
 | ||
|  |         } else {
 | ||
|  |             year = "19" + idNumber.substring(6, 8);// 年份
 | ||
|  |             month = idNumber.substring(8, 10);// 月份
 | ||
|  |             day = idNumber.substring(10, 12);//日
 | ||
|  |         }
 | ||
|  |         Date date = new Date();// 得到当前的系统时间
 | ||
|  |         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 | ||
|  |         String curYear = format.format(date).substring(0, 4);// 当前年份
 | ||
|  |         String curMonth = format.format(date).substring(5, 7);// 月份
 | ||
|  |         String curDay = format.format(date).substring(8, 10);//
 | ||
|  | 
 | ||
|  |         int age = Integer.parseInt(curYear) - Integer.parseInt(year);
 | ||
|  | 
 | ||
|  |         if (Integer.parseInt(month) > Integer.parseInt(curMonth)
 | ||
|  |                 //如果当前月份小于出生月份,说明生日还没过
 | ||
|  |                 || (Integer.parseInt(month) == Integer.parseInt(curMonth) && Integer.parseInt(day) > Integer.parseInt(curDay))) {
 | ||
|  |             age--;
 | ||
|  |         }
 | ||
|  |         return age;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 获取出生日期
 | ||
|  |      *
 | ||
|  |      * @param idNumber 身份证号码
 | ||
|  |      * @return 出生日期 yyyy-mm-dd
 | ||
|  |      */
 | ||
|  |     public static String getBirthday(String idNumber) {
 | ||
|  |         if (isValid(idNumber)) return null;
 | ||
|  | 
 | ||
|  |         String y, m, d;
 | ||
|  |         if (idNumber.length() == 18) { //18位身份证号
 | ||
|  |             y = idNumber.substring(6, 10);
 | ||
|  |             m = idNumber.substring(10, 12);
 | ||
|  |             d = idNumber.substring(12, 14);
 | ||
|  |         } else {
 | ||
|  |             // 身份证上的年份(15位身份证为1980年前的)
 | ||
|  |             y = "19" + idNumber.substring(6, 8);
 | ||
|  |             m = idNumber.substring(8, 10);
 | ||
|  |             d = idNumber.substring(10, 12);
 | ||
|  |         }
 | ||
|  |         return y + "-" + m + "-" + d;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 身份证验证
 | ||
|  |      *
 | ||
|  |      * @param idNumber 身份证号码
 | ||
|  |      * @return 是否有效
 | ||
|  |      */
 | ||
|  |     public static boolean isValid(String idNumber) {
 | ||
|  |         if (idNumber == null || "".equals(idNumber)) {
 | ||
|  |             return false;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         int len = idNumber.length();
 | ||
|  |         if (len != 15 && len != 18) {  // 校验长度只能为15或18
 | ||
|  |             log.info("身份证位数异常");
 | ||
|  |             return true;
 | ||
|  |         }
 | ||
|  |         // 校验生日
 | ||
|  |         if (!validDate(idNumber)) {
 | ||
|  |             log.info("生日异常");
 | ||
|  |             return true;
 | ||
|  |         }
 | ||
|  |         return false;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 校验生日
 | ||
|  |      *
 | ||
|  |      * @param idNumber 身份证号码
 | ||
|  |      * @return 是否有效
 | ||
|  |      */
 | ||
|  |     private static boolean validDate(String idNumber) {
 | ||
|  |         try {
 | ||
|  |             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
 | ||
|  | 
 | ||
|  |             String birth = idNumber.length() == 15 ? "19" + idNumber.substring(6, 12) : idNumber.substring(6, 14);
 | ||
|  |             if (new Date().compareTo(format.parse(birth)) < 0) {
 | ||
|  |                 return false;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             Date birthDate = format.parse(birth);
 | ||
|  |             if (!birth.equals(format.format(birthDate))) {
 | ||
|  |                 return false;
 | ||
|  |             }
 | ||
|  |         } catch (ParseException e) {
 | ||
|  |             return false;
 | ||
|  |         }
 | ||
|  |         return true;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public static void main(String[] args) {
 | ||
|  |         System.out.println(getAge("53233119851015381X"));
 | ||
|  |         System.out.println(getSex("410527200002175438"));
 | ||
|  |         System.out.println(getBirthday("53233119861025381X"));
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     // 百度身份证识别
 | ||
|  |     public static Patient getCardInfo(String image) {
 | ||
|  |         String imagePath = FileHelper.saveBase64Image(image, FOLDER_NAME, "time-" + System.currentTimeMillis() + ".png", true, false);
 | ||
|  | 
 | ||
|  |         if (imagePath == null) return null;
 | ||
|  | 
 | ||
|  |         AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);// 初始化一个AipOcr
 | ||
|  |         client.setConnectionTimeoutInMillis(3000);
 | ||
|  |         client.setSocketTimeoutInMillis(10000);
 | ||
|  |         HashMap<String, String> options = new HashMap<>();
 | ||
|  |         options.put("detect_direction", "true");
 | ||
|  |         // 是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,
 | ||
|  |         options.put("detect_risk", "false");
 | ||
|  |         // client.idcard() ==>识别图片的方法是==>身份证识别
 | ||
|  |         org.json.JSONObject resJson = client.idcard(imagePath, ID_CARD_SIDE, options);
 | ||
|  |         // 获取JSON中的==>words_result对象
 | ||
|  |         org.json.JSONObject words_result = resJson.getJSONObject("words_result");
 | ||
|  | 
 | ||
|  |         Patient patient = new Patient();
 | ||
|  | 
 | ||
|  |         Iterator<String> it = words_result.keys();
 | ||
|  |         while (it.hasNext()) {
 | ||
|  |             String key = it.next();
 | ||
|  |             String value = words_result.getJSONObject(key).getString("words"); // 获取JSON中的==>words值
 | ||
|  | 
 | ||
|  |             if (value == null || "".equals(value)) {
 | ||
|  |                 log.info("缺少" + key + "参数");
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             switch (key) {
 | ||
|  |                 case "姓名":
 | ||
|  |                     patient.setName(value);
 | ||
|  |                     break;
 | ||
|  |                 case "民族":
 | ||
|  |                     patient.setNation(value);
 | ||
|  |                     break;
 | ||
|  |                 case "住址":
 | ||
|  |                     patient.setAddress(value);
 | ||
|  |                     break;
 | ||
|  |                 case "公民身份号码":
 | ||
|  |                     patient.setIdCardNo(value);
 | ||
|  |                     break;
 | ||
|  |                 case "出生":
 | ||
|  |                     patient.setBirthday(value);
 | ||
|  |                     break;
 | ||
|  |                 case "性别":
 | ||
|  |                     patient.setSex(value);
 | ||
|  |                     break;
 | ||
|  |                 default:
 | ||
|  |                     System.out.println("param is not find");
 | ||
|  |                     break;
 | ||
|  |             }
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         FileHelper.updateFileName(imagePath, patient.getIdCardNo());
 | ||
|  |         return patient;
 | ||
|  |     }
 | ||
|  | }
 |