Commit f55f4ae6 authored by yanzg's avatar yanzg

身份证识别

parent 0d046be0
......@@ -60,20 +60,8 @@ public class SfzHandle15 implements SfzHandle {
return null;
}
if (SfzHandleUtil.isNum(idCard)) {
// 获取出生年月日
String birthday = idCard.substring(6, 12);
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null) {
cal.setTime(birthDate);
}
// 获取出生年(完全表现形式,如:2010)
String sYear = String.valueOf(cal.get(Calendar.YEAR));
String sYear = getBirthDateYear(birthday);
idCard18 = idCard.substring(0, 6) + sYear + idCard.substring(8);
// 转换字符数组
char[] cArr = idCard18.toCharArray();
......@@ -94,6 +82,28 @@ public class SfzHandle15 implements SfzHandle {
return idCard18;
}
/**
* 获取生日年份
*
* @param birthday 生日
* @return 年份
*/
public static String getBirthDateYear(String birthday) {
// 获取出生年月日
Date birthDate = null;
try {
birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (birthDate != null) {
cal.setTime(birthDate);
}
// 获取出生年(完全表现形式,如:2010)
return String.valueOf(cal.get(Calendar.YEAR));
}
@Override
public String getGender(String idCard) {
return SfzHandleUtil.SFZ_HANDLE_18.getGender(this.repair(idCard));
......
......@@ -43,7 +43,6 @@ public class SfzHandle18 implements SfzHandle {
String code17 = idCard.substring(0, endIndex);
// 第18位
String code18 = idCard.substring(endIndex);
if (SfzHandleUtil.isNum(code17)) {
char[] cArr = code17.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
......@@ -54,8 +53,13 @@ public class SfzHandle18 implements SfzHandle {
bTrue = true;
}
}
}
if (SfzHandleUtil.isNum(code17) && idCard.length() == CHINA_ID_MAX_LENGTH) {
return bTrue;
} else if (idCard.length() != CHINA_ID_MIN_LENGTH) {
return bTrue;
} else {
return false;
}
}
@Override
......@@ -92,14 +96,7 @@ public class SfzHandle18 implements SfzHandle {
@Override
public String getProvinceName(String idCard) {
int len = idCard.length();
String sProvince = null;
String sProvinNum = "";
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
sProvinNum = idCard.substring(0, 2);
}
sProvince = cityCodes.get(sProvinNum);
return sProvince;
return cityCodes.get(this.getAreaId(idCard).substring(0, 2));
}
/**
......
package com.yanzuoguang.util.sfz;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.regex.Pattern;
import static com.yanzuoguang.util.sfz.SfzHandle15.CHINA_ID_MIN_LENGTH;
import static com.yanzuoguang.util.sfz.SfzHandle15.getBirthDateYear;
import static com.yanzuoguang.util.sfz.SfzHandleUtil.cityCodes;
/**
* 2017年国外永居证处理
*
* @author 颜佐光
*/
public class SfzHandleEn2017 implements SfzHandle {
public static final Pattern EN2017 = Pattern.compile("^[a-zA-Z]{3}\\d{12}$");
@Override
public boolean validate(String idCard) {
if (idCard.length() == CHINA_ID_MIN_LENGTH && EN2017.matcher(idCard).matches()) {
return true;
}
return false;
}
@Override
public String repair(String idCard) {
return null;
return idCard;
}
@Override
public String getGender(String idCard) {
return null;
return StringHelper.EMPTY;
}
@Override
public String getBirthday(String idCard) {
return null;
String birthday = idCard.substring(7, 13);
return getBirthDateYear(birthday) + birthday.substring(2,6);
}
@Override
public String getCountry(String idCard) {
return null;
return StringHelper.EMPTY;
}
@Override
public String getAreaId(String idCard) {
return null;
return idCard.substring(3, 5);
}
@Override
public String getProvinceName(String idCard) {
return null;
return cityCodes.get(this.getAreaId(idCard).substring(0, 2));
}
}
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;
......@@ -32,7 +31,7 @@ public class SfzHandleEn2023 implements SfzHandle {
@Override
public String getBirthday(String idCard) {
return SfzHandleUtil.SFZ_HANDLE_18.getGender(idCard);
return SfzHandleUtil.SFZ_HANDLE_18.getBirthday(idCard);
}
@Override
......@@ -47,13 +46,6 @@ public class SfzHandleEn2023 implements SfzHandle {
@Override
public String getProvinceName(String idCard) {
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;
return cityCodes.get(this.getAreaId(idCard).substring(0, 2));
}
}
......@@ -11,11 +11,11 @@ import java.util.regex.Pattern;
*/
public class SfzHandleHuZhao implements SfzHandle {
private static final String HU_ZHAO_REGEX = "^[a-zA-Z]{0,4}\\d{7,8}$";
private static final Pattern HU_ZHAO_REGEX = Pattern.compile("^[a-zA-Z]{0,4}\\d{7,8}$");
@Override
public boolean validate(String idCard) {
return Pattern.matches(HU_ZHAO_REGEX, idCard);
return HU_ZHAO_REGEX.matcher(idCard).matches();
}
@Override
......
......@@ -5,14 +5,15 @@ import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.SfzhUtil;
import com.yanzuoguang.util.sfz.SfzHandleUtil;
import helper.vo.ResponseDataMainResult;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static com.yanzuoguang.util.sfz.SfzHandleUtil.SFZ_HANDLE_15;
import static com.yanzuoguang.util.sfz.SfzHandleUtil.SFZ_HANDLE_18;
public class TestSfzUtil {
......@@ -55,6 +56,7 @@ public class TestSfzUtil {
validateIdCard18("44142419920313185X");
}
@Test
public void testId18() throws NoSuchMethodException {
// 获取泛型的返回值类型
......@@ -68,6 +70,7 @@ public class TestSfzUtil {
}
}
}
@Test
public void testId18AND15() throws NoSuchMethodException {
// 获取泛型的返回值类型
......@@ -82,6 +85,7 @@ public class TestSfzUtil {
}
}
}
@Test
public void testId18AND15GetAge() throws NoSuchMethodException {
// 获取泛型的返回值类型
......@@ -96,6 +100,62 @@ public class TestSfzUtil {
}
}
@Test
public void testEn2017() throws NoSuchMethodException {
String idCard = "PAK310080010103";
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_15.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_18.validate(idCard));
Assert.assertTrue(SfzHandleUtil.SFZ_HANDLE_EN2017.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2023.validate(idCard));
Assert.assertEquals("", SfzHandleUtil.SFZ_HANDLE_EN2017.getGender(idCard));
Assert.assertEquals("19800101", SfzHandleUtil.SFZ_HANDLE_EN2017.getBirthday(idCard));
Assert.assertEquals("", SfzHandleUtil.SFZ_HANDLE_EN2017.getCountry(idCard));
Assert.assertEquals("31", SfzHandleUtil.SFZ_HANDLE_EN2017.getAreaId(idCard));
Assert.assertEquals("上海", SfzHandleUtil.SFZ_HANDLE_EN2017.getProvinceName(idCard));
}
@Test
public void testEn2023() throws NoSuchMethodException {
String idCard = "931586198001010028";
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_15.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_18.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2017.validate(idCard));
Assert.assertTrue(SfzHandleUtil.SFZ_HANDLE_EN2023.validate(idCard));
Assert.assertEquals("女", SfzHandleUtil.SFZ_HANDLE_EN2023.getGender(idCard));
Assert.assertEquals("19800101", SfzHandleUtil.SFZ_HANDLE_EN2023.getBirthday(idCard));
Assert.assertEquals("586", SfzHandleUtil.SFZ_HANDLE_EN2023.getCountry(idCard));
Assert.assertEquals("31", SfzHandleUtil.SFZ_HANDLE_EN2023.getAreaId(idCard));
Assert.assertEquals("上海", SfzHandleUtil.SFZ_HANDLE_EN2023.getProvinceName(idCard));
}
@Test
public void test18() throws NoSuchMethodException {
String idCard = "43052419871104817X";
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_15.validate(idCard));
Assert.assertTrue(SfzHandleUtil.SFZ_HANDLE_18.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2017.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2023.validate(idCard));
Assert.assertEquals("男", SfzHandleUtil.SFZ_HANDLE_18.getGender(idCard));
Assert.assertEquals("19871104", SfzHandleUtil.SFZ_HANDLE_18.getBirthday(idCard));
Assert.assertEquals("100", SfzHandleUtil.SFZ_HANDLE_18.getCountry(idCard));
Assert.assertEquals("430524", SfzHandleUtil.SFZ_HANDLE_18.getAreaId(idCard));
Assert.assertEquals("湖南", SfzHandleUtil.SFZ_HANDLE_18.getProvinceName(idCard));
}
@Test
public void test15() throws NoSuchMethodException {
String idCard = "430524871104817";
Assert.assertTrue(SfzHandleUtil.SFZ_HANDLE_15.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_18.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2017.validate(idCard));
Assert.assertFalse(SfzHandleUtil.SFZ_HANDLE_EN2023.validate(idCard));
Assert.assertEquals("男", SfzHandleUtil.SFZ_HANDLE_15.getGender(idCard));
Assert.assertEquals("19871104", SfzHandleUtil.SFZ_HANDLE_15.getBirthday(idCard));
Assert.assertEquals("100", SfzHandleUtil.SFZ_HANDLE_15.getCountry(idCard));
Assert.assertEquals("430524", SfzHandleUtil.SFZ_HANDLE_15.getAreaId(idCard));
Assert.assertEquals("湖南", SfzHandleUtil.SFZ_HANDLE_15.getProvinceName(idCard));
}
/**
* 验证18位身份编码是否合法
......
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