package com.dpkj.modules.cardReader.utils; import com.dpkj.common.exception.RRException; import lombok.Data; import java.io.UnsupportedEncodingException; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Consumer; /** * 有关身份证的基本信息 */ @Data public class IdCardInfo { /** * 名字 */ private String name; /** * 性别 */ private String sex; /** * 民族 */ private String nation; /** * 出生日期 */ private LocalDateTime brith; /** * 住址 */ private String address; /** * 身份证号码 */ private String certNo; /** * 签发机关 */ private String department; /** * 有效日期 */ private LocalDateTime effectDate; /** * 截至日期 */ private LocalDateTime expireDate; /** * 外国人姓名 */ private String enName; /** * 外国人国籍 */ private String nationalityCode; /** * 注册过的sdk实例 */ private CardReaderSdk cardReaderSdk; /** * 是否有指纹 */ private Boolean fingerExist; /** * 指纹数据 */ private byte[] fingerData; /** * 港澳台通行证号码 */ private String TXZHM; /** * 港澳台通行证签发次数 */ private Integer TXZQFCS; /** * 外国人换证次数 */ private Integer HZCS; public IdCardInfo(CardReaderSdk cardReaderSdk, Long handle){ if ( cardReaderSdk == null ){ throw new RRException("sdk初始化失败"); } this.cardReaderSdk = cardReaderSdk; byte[] errMsg = new byte[999]; cardReaderSdk.PICC_Reader_ReadIDCard(handle, errMsg); try { String gbk = new String(errMsg, "GBK").trim(); if (!gbk.equals("")) { throw new RRException(); } } catch (Exception e) { throw new RRException("身份证信息读取失败"); } this.name = this.convert2Str(this.cardReaderSdk::GetName, "姓名转换失败"); this.sex = this.convert2Str(this.cardReaderSdk::GetSex, "性别转换失败"); this.nation = this.convert2Str(this.cardReaderSdk::GetNation, "民族转换失败"); String br = this.convert2Str(this.cardReaderSdk::GetBirth, "出生日期转换失败"); if ( br != null && br != "" ){ this.brith = this.str2LocalDateTime(br); } this.address = this.convert2Str(this.cardReaderSdk::GetAddress, "住址转换失败"); this.certNo = this.convert2Str(this.cardReaderSdk::GetCertNo, "身份证转换失败"); this.department = this.convert2Str(this.cardReaderSdk::GetDepartemt, "签发机关转换失败"); String eff = this.convert2Str(this.cardReaderSdk::GetEffectDate, "过期时间转换失败"); if ( eff != null && eff != "" ){ this.effectDate = this.str2LocalDateTime(eff); } String exp = this.convert2Str(this.cardReaderSdk::GetExpireDate, "截至日期转换失败"); if ( exp != null && exp != "" ){ this.expireDate = this.str2LocalDateTime(exp); } this.enName = this.convert2Str(this.cardReaderSdk::GetEnName, "外国人姓名转换失败"); this.nationalityCode = this.convert2Str(this.cardReaderSdk::GetNationalityCode, "外国人国籍转换失败"); // this.fingerExist = this.cardReaderSdk.IsFingerExist() != 0; // if ( this.fingerExist ) { // this.fingerData = this.convert2Str(this.cardReaderSdk::GetFingerprint, "指纹数据转换失败").getBytes(StandardCharsets.UTF_8); // } } private String convert2Str(Consumer function, String tip) { byte[] param = new byte[256]; function.accept(param); try { // 检查字节数组是否为空或者只包含值为 0 的字节 boolean isAllZero = true; for (byte b : param) { if (b != 0) { isAllZero = false; break; } } if (isAllZero) { return null; } return new String(param, "GBK").trim(); }catch (Exception e){ throw new RRException(tip == null ? "参数转换失败" : tip); } } private LocalDateTime str2LocalDateTime(String str){ // 定义日期格式化器 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); // 将字符串解析为 LocalDate LocalDate localDate = LocalDate.parse(str, formatter); // 将 LocalDate 转换为 LocalDateTime,设置时间为 00:00:00 return localDate.atStartOfDay(); } }