0

I read passport information through a scanner When the scanner reads the passport, the data comes out like this

#PGRPMUSAAAA<<BBB<CCCC<<<<<<<<<<<<<<<<<<<<<<<<<\nM987654321USA7303010M20071519876543V12345678\n

I want to get FULL NAME, LAST NAME ,FIRST NAME, NATION CODE,PASSPORTNUM,SEX,BIRTH respectively

I extracted the name and other data by expressing it like this in the code

var MrzArraySplit =  mrz.Substring(0).Split(new[] { "<" }, StringSplitOptions.RemoveEmptyEntries);

 Data.FullName = OcrArraySplit[0] + OcrArraySplit[1] + OcrArraySplit[2]; //AAABBBCCCC
 Data.LastName = OcrArraySplit[0];     // AAA
 Data.FirstName1 = OcrArraySplit[1];      // BBB
 Data.FirstName2 = OcrArraySplit[2];      // CCCC
 Data.PassportNum = OcrArraySplit[3].Replace("\n",""); // \nM987654321USA7303010M20071519876543V12345678\n
 Data.Birth = "";
 Data.Sex = "";
 Data.NationCode = "";

How should I code to extract the data I want to get?

codeman
  • 17
  • 3

2 Answers2

0

Second line of MRZ (between \n and \n) contains not only password number, but all information you need (see https://en.wikipedia.org/wiki/Machine-readable_passport for example). Extract parts of string from fixed positions:

var line2 = OcrArraySplit[3].Replace("\n","");
d.PasspornNum = line2.Substring(0, 9);
d.Nationality = line2.Substring(10, 3);

etc

pakeha_by
  • 2,081
  • 1
  • 15
  • 7
0

Here is my code to parse the strings (https://github.com/yushulx/dotnet-mrz-sdk/blob/main/MrzScanner.cs):

public static JsonNode? Parse(string[] lines)
    {
        JsonNode mrzInfo = new JsonObject();

        if (lines.Length == 0)
        {
            return null;
        }

        if (lines.Length == 2)
        {
            string line1 = lines[0];
            string line2 = lines[1];

            var type = line1.Substring(0, 1);
            if (!new Regex(@"[I|P|V]").IsMatch(type)) return null;
            if (type == "P")
            {
                mrzInfo["type"] = "PASSPORT (TD-3)";
            }
            else if (type == "V")
            {
                if (line1.Length == 44)
                {
                    mrzInfo["type"] = "VISA (MRV-A)";
                }
                else if (line1.Length == 36)
                {
                    mrzInfo["type"] = "VISA (MRV-B)";
                }
            }
            else if (type == "I")
            {
                mrzInfo["type"] = "ID CARD (TD-2)";
            }

            // Get issuing State infomation
            var nation = line1.Substring(2, 5);
            if (new Regex(@"[0-9]").IsMatch(nation)) return null;
            if (nation[nation.Length - 1] == '<') {
                nation = nation.Substring(0, 2);
            }
            mrzInfo["nationality"] = nation;
            // Get surname information
            line1 = line1.Substring(5);
            var pos = line1.IndexOf("<<");
            var surName = line1.Substring(0, pos);
            if (new Regex(@"[0-9]").IsMatch(surName)) return null;
            surName = surName.Replace("<", " ");
            mrzInfo["surname"] = surName;
            // Get givenname information
            var givenName = line1.Substring(surName.Length + 2);
            if (new Regex(@"[0-9]").IsMatch(givenName)) return null;
            givenName = givenName.Replace("<", " ");
            givenName = givenName.Trim();
            mrzInfo["givenname"] = givenName;
            // Get passport number information
            var passportNumber = "";
            passportNumber = line2.Substring(0, 9);
            passportNumber = passportNumber.Replace("<", " ");
            mrzInfo["passportnumber"] = passportNumber;
            // Get Nationality information
            var issueCountry = line2.Substring(10, 3);
            if (new Regex(@"[0-9]").IsMatch(issueCountry)) return null;
            if (issueCountry[issueCountry.Length - 1] == '<') {
                issueCountry = issueCountry.Substring(0, 2);
            }
            mrzInfo["issuecountry"] = issueCountry;
            // Get date of birth information
            var birth = line2.Substring(13, 6);
            var date = new DateTime();
            var currentYear = date.Year;
            if (Int32.Parse(birth.Substring(0, 2)) > (currentYear % 100)) {
                birth = "19" + birth;
            } else {
                birth = "20" + birth;
            }
            birth = birth.Substring(0, 4) + "-" + birth.Substring(4, 2) + "-" + birth.Substring(6, 2);
            if (new Regex(@"[A-Za-z]").IsMatch(birth)) return null;
            mrzInfo["birth"] = birth;
            // Get gender information
            var gender = line2[20] + "";
            if (!(new Regex(@"[M|F|x|<]").IsMatch(gender))) return null;
            mrzInfo["gender"] = gender;
            // Get date of expiry information
            var expiry = line2.Substring(21, 6);
            if (new Regex(@"[A-Za-z]").IsMatch(expiry)) return null;
            if (Int32.Parse(expiry.Substring(0, 2)) >= 60) {
                expiry = "19" + expiry;
            } else {
                expiry = "20" + expiry;
            }
            expiry = expiry.Substring(0, 4) + "-" + expiry.Substring(4, 2) + "-" + expiry.Substring(6);
            mrzInfo["expiry"] = expiry;
        }
        else if (lines.Length == 3)
        {
            string line1 = lines[0];
            string line2 = lines[1];
            string line3 = lines[2];
            var type = line1.Substring(0, 1);
            if (!new Regex(@"[I|P|V]").IsMatch(type)) return null;
            mrzInfo["type"] = "ID CARD (TD-1)";
            // Get nationality infomation
            var nation = line2.Substring(15, 3);
            if (new Regex(@"[0-9]").IsMatch(nation)) return null;
            nation = nation.Replace("<", "");
            mrzInfo["nationality"] = nation;
            // Get surname information
            var pos = line3.IndexOf("<<");
            var surName = line3.Substring(0, pos);
            if (new Regex(@"[0-9]").IsMatch(surName)) return null;
            surName = surName.Replace("<", " ");
            surName.Trim();
            mrzInfo["surname"] = surName;
            // Get givenname information
            var givenName = line3.Substring(surName.Length + 2);
            if (new Regex(@"[0-9]").IsMatch(givenName)) return null;
            givenName = givenName.Replace("<", " ");
            givenName = givenName.Trim();
            mrzInfo["givenname"] = givenName;
            // Get passport number information
            var passportNumber = "";
            passportNumber = line1.Substring(5, 9);
            passportNumber = passportNumber.Replace("<", " ");
            mrzInfo["passportnumber"] = passportNumber;
            // Get issuing country or organization information
            var issueCountry = line1.Substring(2, 3);
            if (new Regex(@"[0-9]").IsMatch(issueCountry)) return null;
            issueCountry = issueCountry.Replace("<", "");
            mrzInfo["issuecountry"] = issueCountry;
            // Get date of birth information
            var birth = line2.Substring(0, 6);
            if (new Regex(@"[A-Za-z]").IsMatch(birth)) return null;
            var date = new DateTime();
            var currentYear = date.Year;
            if (Int32.Parse(birth.Substring(0, 2)) > (currentYear % 100)) {
                birth = "19" + birth;
            } else {
                birth = "20" + birth;
            }
            birth = birth.Substring(0, 4) + "-" + birth.Substring(4, 2) + "-" + birth.Substring(6);
            mrzInfo["birth"] = birth;
            // Get gender information
            var gender = line2[7] + "";
            if (!(new Regex(@"[M|F|X|<]").IsMatch(gender))) return null;
            gender = gender.Replace("<", "X");
            mrzInfo["gender"] = gender;
            // Get date of expiry information
            var expiry = "20" + line2.Substring(8, 6);
            if (new Regex(@"[A-Za-z]").IsMatch(expiry)) return null;
            expiry = expiry.Substring(0, 4) + "-" + expiry.Substring(4, 2) + "-" + expiry.Substring(6);
            mrzInfo["expiry"] = expiry;
        }

        return mrzInfo;
    }
yushulx
  • 11,695
  • 8
  • 37
  • 64