I. Introduction to the number structure of 15-digit ID card and 18-digit ID card
In order to verify the id number, we need to understand the coding rules of the ID number in China. China’s id card number mostly consists of a number of digits or a mixture of digits and letters. Early id cards consisted of 15 digits, mainly issued before 1980, but the 18-digit numbering rule was added in consideration of the Y2K bug, as the 15-digit id number could only be used for people born between January 1, 1900 and December 31, 1999.
12 3 4 5 6 7 8 9 10 11 12 13 14 15 AAAAA Y Y M M D N N S The first six digits of AAAAAA are the code of the location (birthplace) of the id card coding object, which can be obtained from the relevant standards published by the National Bureau of Statistics. YY refers to the last two digits of the year of birth, MM and DD refers to the month and day of birth, and 0 is added in the high order if less than two digits. NNS is the serial number and cannot be determined. S is the gender identification code, which is odd for males and even for females. With that in mind, it becomes much easier to write code.
Second, algorithm implementation
Knowing the rules of the ID card number, we can infer that the 15-digit transformation of the ID card takes two steps. First complete the 15-digit ID number to 17, and then complete the last one. But is the last digit a number or a letter X? There’s a problem here again. We know that the last digit of the ID card is the check digit, so how do we get the last digit? Originally, the last digit was made up of the numbers 1 through 9, and anything over 9, such as 11, was denoted by the letter X, otherwise the number became 19 digits. After understanding these, the realization idea of id card completion algorithm is as follows:
Step1 add the 15-digit id number to the year of birth and change it to 17 digits
Step2. Multiply the 17 digits of id obtained by Step1 by different coefficients. From 1 to 17 coefficient respectively: 7-9-10-5-8-4-5-8-4-3-7-9-7-9-10-4-2.
Step3. Add the 17 digits multiplied by the coefficients
Step4. Divide the result of step3 by 11 to get the remainder
Due to the particularity of numbers, the remainder can only be the 11 digits 0-10, the corresponding number of the last digit of the ID card is 1-0-X-9-8-7-6-5-4-3-2. If the remainder is 3, the last digit of the corresponding ID card number is 9. If it is 10, the last digit of the ID card number is 2.
Three, code,
E =qiege(d) [m,n]=size(d); % Set image size top=1; bottom=m; left=1; right=n; Top bottom left right % starting from the first line, top down, if the sum of this line is 0 and top<=m, While sum(d(top,:))==0 && top<=m top=top+1; While sum(d(bottom,:))==0 && bottom>=1 bottom=bottom-1; sum(d(bottom,:))==0 && bottom>=1 bottom=bottom-1; While sum(d(:,left))==0 &&left <=n left=left+1; While sum(d(:,right))==0 && left<=n right=right-1; End % dd=right-left; hh=bottom-top; e=imcrop(d,[left top dd hh]);Copy the code
Iv. Demo results
Complete code or simulation consultation QQ1575304183