summaryrefslogtreecommitdiff
path: root/src/main/java/info/selflearner/ocr_telegram/model/Passport.java
blob: 4e8a0a72fca1703f0209ec763e01c671b0f73c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package info.selflearner.ocr_telegram.model;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Passport {
    public char type;
    public Character subtype;
    public String issuer;
    public String surname;
    public String givenNames;
    public String number;
    public String nationality;
    public LocalDate dateOfBirth;
    public Character sex;
    public LocalDate expirationDate;
    public String personalNumber;
    public int[] mrzLinesConfidence;

    public Passport(char type, Character subtype, String issuer, String surname, String givenNames, String number, String nationality, LocalDate dateOfBirth, Character sex, LocalDate expirationDate, String personalNumber, int[] mrzLinesConfidence) {
        this.type = type;
        this.subtype = subtype;
        this.issuer = issuer;
        this.surname = surname;
        this.givenNames = givenNames;
        this.number = number;
        this.nationality = nationality;
        this.dateOfBirth = dateOfBirth;
        this.sex = sex;
        this.expirationDate = expirationDate;
        this.personalNumber = personalNumber;
        this.mrzLinesConfidence = mrzLinesConfidence;
    }

    public Passport(String[] mrzLines, int[] mrzLinesConfidence) {
        this.type = mrzLines[0].charAt(0);
        this.subtype = mrzLines[0].charAt(1) == '<' ? null : mrzLines[0].charAt(0);
        this.issuer = mrzLines[0].substring(2, 5);
        this.surname = mrzLines[0].substring(5, mrzLines[0].indexOf('<', 6));
        this.givenNames = mrzLines[0].substring(surname.length() + 7, mrzLines[0].indexOf('<', surname.length() + 8));
        this.number = mrzLines[1].substring(0, 9);
        this.nationality = mrzLines[1].substring(10, 13);
        if (Integer.parseInt("20" + mrzLines[1].substring(13, 15)) < LocalDate.now().getYear()) {
            this.dateOfBirth = LocalDate.of(Integer.parseInt("20" + mrzLines[1].substring(13, 15)), Integer.parseInt(mrzLines[1].substring(15, 17)), Integer.parseInt(mrzLines[1].substring(17, 19)));
        } else {
            this.dateOfBirth = LocalDate.of(Integer.parseInt("19" + mrzLines[1].substring(13, 15)), Integer.parseInt(mrzLines[1].substring(15, 17)), Integer.parseInt(mrzLines[1].substring(17, 19)));
        }
        this.sex = mrzLines[1].charAt(20) != '<' ? mrzLines[1].charAt(20) : null;
        this.expirationDate = LocalDate.of(Integer.parseInt("20" + mrzLines[1].substring(21, 23)), Integer.parseInt(mrzLines[1].substring(23, 25)), Integer.parseInt(mrzLines[1].substring(25, 27)));
        this.personalNumber = mrzLines[1].substring(28, 42);
        this.mrzLinesConfidence = mrzLinesConfidence;
    }

    @Override
    public String toString() {
        return "*Passport*\n" +
                "Type: `" + type + "`\n" +
                "Subtype: `" + subtype + "`\n" +
                "Issuer: `" + issuer + "`\n" +
                "Surname: `" + surname + "`\n" +
                "Given names: `" + givenNames + "`\n" +
                "Number: `" + number + "`\n" +
                "Nationality: `" + nationality + "`\n" +
                "Date of birth: `" + dateOfBirth.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) + "`\n" +
                "Gender: `" + sex + "`\n" +
                "Expiration date: `" + expirationDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) + "`\n" +
                "Personal number: `" + personalNumber + "`\n";
    }
}