刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package jnpf.base.model.ocr.model;
 
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.media.Schema;
import jnpf.base.model.ocr.OcrModel;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * ocr解析行驶证模型
 *
 * @author JNPF开发平台组
 * @version v6.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2025/7/29 16:58:47
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "ocr解析行驶证模型")
@NoArgsConstructor
public class VehicleLicModel extends OcrModel {
    @Schema(description = "号牌号码")
    private String plateNo;
    @Schema(description = "车辆类型")
    private String vehicleType;
    @Schema(description = "所有人")
    private String owner;
    @Schema(description = "住址")
    private String address;
    @Schema(description = "使用性质")
    private String useCharacter;
    @Schema(description = "品牌型号")
    private String model;
    @Schema(description = CLSBDH)
    private String vin;
    @Schema(description = FDJHM)
    private String engineNo;
    @Schema(description = "注册日期")
    private Long registerDate;
    @Schema(description = "发证日期")
    private Long issueDate;
 
    private static final String CLSBDH = "车辆识别代号";
    private static final String FDJHM = "发动机号码";
    private static final String DATE_DD = "yyyyMMdd";
    private static final String DATE_DD2 = "yyyy-MM-dd";
 
    @Override
    public void extract(String url, JSONObject body) {
        super.extract(url, body);
        if (StringUtil.isEmpty(this.getContent()) || !this.getContent().contains("行驶证")) {
            return;
        }
        this.setInfo();
        this.setContent(null);
        this.setStrList(null);
    }
 
    private void setInfo() {
        // 使用正则表达式提取关键信息
        String ocrText = this.getContent();
        int size = this.getStrList().size();
        //for循环取单行数据,避免整体取到其他行合并信息
        for (int i = 0; i < size; i++) {
            if (StringUtil.isEmpty(this.plateNo) && this.getStrList().get(i).contains("号牌号码")) {
                this.plateNo = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.vehicleType) && this.getStrList().get(i).contains("车辆类型")) {
                this.vehicleType = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.owner) && this.getStrList().get(i).contains("所有人")) {
                this.owner = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.address) && this.getStrList().get(i).contains("住址")) {
                this.address = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.useCharacter) && this.getStrList().get(i).contains("使用性质")) {
                this.useCharacter = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.model) && this.getStrList().get(i).contains("品牌型号")) {
                this.model = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.vin) && this.getStrList().get(i).contains(CLSBDH)) {
                this.vin = this.getStrList().get(i + 1);
            }
            if (StringUtil.isEmpty(this.engineNo) && this.getStrList().get(i).contains(FDJHM)) {
                this.engineNo = this.getStrList().get(i + 1);
            }
            if (this.registerDate == null && this.getStrList().get(i).contains("注册日期")) {
                Date date = DateUtil.checkDate(this.getStrList().get(i + 1), DATE_DD2);
                if (date == null) {
                    date = DateUtil.checkDate(this.getStrList().get(i + 1), DATE_DD);
                }
                this.registerDate = date != null ? date.getTime() : null;
            }
            if (this.issueDate == null && this.getStrList().get(i).contains("发证日期")) {
                Date date = DateUtil.checkDate(this.getStrList().get(i + 1), DATE_DD2);
                if (date == null) {
                    date = DateUtil.checkDate(this.getStrList().get(i + 1), DATE_DD);
                }
                this.issueDate = date != null ? date.getTime() : null;
            }
        }
        //车牌号识别
        extractPlateNo();
 
        otherProp(ocrText);
    }
 
    private void otherProp(String ocrText) {
        String vehicleTypeA = betweenText(ocrText, "车辆类型", "所有");
        if (StringUtil.isEmpty(this.vehicleType) || (StringUtil.isNotEmpty(vehicleTypeA) && !vehicleTypeA.startsWith(this.vehicleType))) {
            this.vehicleType = vehicleTypeA;
        }
        String ownerA = betweenText(ocrText, "所有", "住址");
        if (StringUtil.isEmpty(this.owner) || (StringUtil.isNotEmpty(ownerA) && !ownerA.startsWith(this.owner))) {
            this.owner = ownerA;
        }
        String addressA = betweenText(ocrText, "住址", "使用性质");
        if (StringUtil.isEmpty(this.address) || (StringUtil.isNotEmpty(addressA) && !addressA.startsWith(this.address))) {
            this.address = addressA;
        }
        String useCharacterA = betweenText(ocrText, "使用性质", "品牌型号");
        if (StringUtil.isEmpty(this.useCharacter) || (StringUtil.isNotEmpty(useCharacterA) && !useCharacterA.startsWith(this.useCharacter))) {
            this.useCharacter = useCharacterA;
        }
        String modelA = betweenText(ocrText, "品牌型号", CLSBDH);
        if (StringUtil.isEmpty(this.model) || (StringUtil.isNotEmpty(modelA) && !modelA.startsWith(this.model))) {
            this.model = modelA;
        }
        String vinA = betweenText(ocrText, CLSBDH, FDJHM);
        if (StringUtil.isEmpty(this.vin) || (StringUtil.isNotEmpty(vinA) && !vinA.startsWith(this.vin))) {
            this.vin = vinA;
        }
        String engineNoA = betweenText(ocrText, FDJHM, "注册日期");
        if (StringUtil.isEmpty(this.engineNo) || (StringUtil.isNotEmpty(engineNoA) && !engineNoA.startsWith(this.engineNo))) {
            this.engineNo = engineNoA;
        }
        Long registerDateA = extractDateField("注册日期[::]\\s*(\\d{4}-\\d{1,2}-\\d{1,2})", ocrText);
        if (this.registerDate != null && registerDateA != null) {
            this.registerDate = registerDateA;
        }
        Long issueDateA = extractDateField("发证日期[::]\\s*(\\d{4}-\\d{1,2}-\\d{1,2})", ocrText);
        if (this.issueDate != null && issueDateA != null) {
            this.issueDate = issueDateA;
        }
    }
 
    private Long extractDateField(String regex, String text) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            String dateStr = matcher.group(1);
            Date date = DateUtil.checkDate(dateStr, DATE_DD2);
            if (date == null) {
                date = DateUtil.checkDate(dateStr, DATE_DD);
            }
            return date != null ? date.getTime() : null;
        }
        return null;
    }
 
    private String betweenText(String content, String text1, String text2) {
        List<String> list = new ArrayList<>(Arrays.asList("号牌号码", "PlateNo", "Plate", "No", "车辆类型", "vehicleType", "Vehicle", "Type", "所有人", "Owner", "住址",
                "Address", "使用性质", "UseCharacter", "Use", "Character", "品牌型号", "Model", CLSBDH, "Vin", FDJHM, "EngineNo", "Engine",
                "注册日期", "RegisterDate", "Register", "Data", "发证日期", "IssueDate", "Issue"));
        if (StringUtil.isNotEmpty(this.plateNo)) {
            list.add(this.plateNo);
        }
        int m = content.indexOf(text1);
        int n = content.indexOf(text2);
        if (m > 0 && n > m) {
            String substring = content.substring(m + text1.length(), n);
            if (StringUtil.isNotEmpty(substring)) {
                for (String s : list) {
                    if (substring.contains(s)) {
                        substring = substring.replace(s, "");
                    }
                }
            }
            return substring;
        }
        return "";
    }
 
    private void extractPlateNo() {
        String regex = "[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新台港澳][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[警学领]?";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(this.getContent());
        if ((StringUtil.isEmpty(this.plateNo) || !pattern.matcher(this.plateNo).matches()) && matcher.find()) {
            this.plateNo = matcher.group();
        }
    }
}