Commit 0d046be0 authored by yanzg's avatar yanzg

身份证识别

parent 7c784fb4
...@@ -24,22 +24,34 @@ public class SfzHandle18 implements SfzHandle { ...@@ -24,22 +24,34 @@ public class SfzHandle18 implements SfzHandle {
@Override @Override
public boolean validate(String idCard) { public boolean validate(String idCard) {
if (idCard.length() == CHINA_ID_MAX_LENGTH && !idCard.startsWith(SfzHandleEn2023.START)) {
return validate18(idCard);
}
return false;
}
/**
* 验证身份证校验是否合法
*
* @param idCard 身份证
* @return 校验合法
*/
public static boolean validate18(String idCard) {
boolean bTrue = false; boolean bTrue = false;
if (idCard.length() == CHINA_ID_MAX_LENGTH) { int endIndex = idCard.length() - 1;
// 前17位 // 前17位
String code17 = idCard.substring(0, 17); String code17 = idCard.substring(0, endIndex);
// 第18位 // 第18位
String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH); String code18 = idCard.substring(endIndex);
if (SfzHandleUtil.isNum(code17)) { if (SfzHandleUtil.isNum(code17)) {
char[] cArr = code17.toCharArray(); char[] cArr = code17.toCharArray();
if (cArr != null) { if (cArr != null) {
int[] iCard = converCharToInt(cArr); int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard); int iSum17 = getPowerSum(iCard);
// 获取校验位 // 获取校验位
String val = getCheckCode18(iSum17); String val = getCheckCode18(iSum17);
if (val.length() > 0 && val.equalsIgnoreCase(code18)) { if (val.length() > 0 && val.equalsIgnoreCase(code18)) {
bTrue = true; bTrue = true;
}
} }
} }
} }
......
package com.yanzuoguang.util.sfz; package com.yanzuoguang.util.sfz;
import static com.yanzuoguang.util.sfz.SfzHandle15.CHINA_ID_MIN_LENGTH;
import static com.yanzuoguang.util.sfz.SfzHandle18.CHINA_ID_MAX_LENGTH;
import static com.yanzuoguang.util.sfz.SfzHandleUtil.cityCodes;
/** /**
* 2023年国外永居证处理 * 2023年国外永居证处理
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public class SfzHandleEn2023 implements SfzHandle { public class SfzHandleEn2023 implements SfzHandle {
public static final String START = "9";
@Override @Override
public boolean validate(String idCard) { public boolean validate(String idCard) {
if (idCard.length() == CHINA_ID_MAX_LENGTH && idCard.startsWith(SfzHandleEn2023.START)) {
return SfzHandle18.validate18(idCard);
}
return false; return false;
} }
@Override @Override
public String repair(String idCard) { public String repair(String idCard) {
return null; return idCard;
} }
@Override @Override
public String getGender(String idCard) { public String getGender(String idCard) {
return null; return SfzHandleUtil.SFZ_HANDLE_18.getGender(idCard);
} }
@Override @Override
public String getBirthday(String idCard) { public String getBirthday(String idCard) {
return null; return SfzHandleUtil.SFZ_HANDLE_18.getGender(idCard);
} }
@Override @Override
public String getCountry(String idCard) { public String getCountry(String idCard) {
return null; return idCard.substring(3, 6);
} }
@Override @Override
public String getAreaId(String idCard) { public String getAreaId(String idCard) {
return null; return idCard.substring(1, 3);
} }
@Override @Override
public String getProvinceName(String idCard) { public String getProvinceName(String idCard) {
return null; int len = idCard.length();
String sProvince = null;
String sProvinNum = "";
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
sProvinNum = idCard.substring(1, 3);
}
sProvince = cityCodes.get(sProvinNum);
return sProvince;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment