shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.openocr.vehicle;
 
import com.walker.openocr.ValueParser;
import com.walker.openocr.table.CellObject;
import com.walker.openocr.table.TableConfig;
import com.walker.openocr.table.TableObject;
import com.walker.openocr.util.TextUtils;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 行驶证识别结果对象定义。
 * @author 时克英
 * @date 2022-09-27
 */
public class VehicleLicenseTable extends TableObject {
 
    // 属性id 与 解析器关联
    private Map<String, ValueParser> idParserMap = new HashMap<>(8);
 
    private Map<String, CellObject> tableData = new HashMap<>();
 
    public VehicleLicenseTable(TableConfig tableConfig, List<ValueParser> valueParserList) {
        super(tableConfig);
        if(valueParserList == null || valueParserList.size() == 0){
            logger.warn("valueParserList 没有设置,无法更好解析行驶证属性参数");
        } else {
            for(ValueParser valueParser : valueParserList){
                idParserMap.put(valueParser.getName(), valueParser);
            }
        }
    }
 
    @Override
    public void printRowCache(){
        super.printRowCache();
        logger.debug("~~~~~~~~~~~~~~~~~ table data ~~~~~~~~~~~~~~~~~");
        for(CellObject other : this.tableData.values()){
            logger.debug(other.toString());
        }
    }
 
    @Override
    public Map<String, CellObject> getTableDataMap(){
        return this.tableData;
    }
 
    @Override
    public void calculateValue(){
        super.calculateValue();
 
        //
//        Map<String, CellObject> map = new HashMap<>();
        if(this.rowCache.size() > 0){
            for (List<CellObject> list : this.rowCache.values()){
                for(CellObject co : list){
                    if(co.isConfigurable()){
                        tableData.put(co.getId(), co);
                    }
                }
            }
        }
 
        // 把没有解析到的属性值,通过简单数据推断后设置到属性中。
        this.predictValue("hao_pai_hao_ma");
        this.predictValue("che_liang_lei_xing");
        this.predictValue("zhu_zhi");
        this.predictValue("che_liang_shi_bie_dai_ma");
    }
 
    private void predictValue(String id){
        ValueParser valueParser = this.idParserMap.get(id);
        CellObject one = tableData.get(id);
        if(one != null && !TextUtils.isEmpty(one.getValue())){
            if(valueParser != null){
                if(valueParser.isTypeValue(one.getValue())){
                    return;
                }
            }
        } else if(one != null && one.getValue() == null){
            if(valueParser != null){
                if(valueParser.isTypeValue(one.getValue())){
                    return;
                }
            }
        } else if(one == null){
            // 没有解析到该属性
            one = new CellObject();
            one.setId(id);
            tableData.put(id, one);
        }
 
        String predictValue = this.parseValue(id, valueParser);
        if(predictValue != null){
            if(!one.isConfigurable()){
                logger.debug(predictValue + " 没有配置项,创建一个: " + id);
                one.setCellConfigItem(this.getRecognizeConfig().getCellConfigItem(id));
            }
            one.setValue(predictValue);
            one.setId(id);
        }
    }
 
    private String parseValue(String id, ValueParser valueParser){
//        ValueParser valueParser = this.idParserMap.get(id);
        if(valueParser == null){
            return null;
        }
        Object result = null;
        for (List<CellObject> list : this.rowCache.values()){
            for(CellObject co : list){
                result = valueParser.getValue(co.getSource().getText());
                if(result != null){
                    logger.debug("找到成功解析的属性值:" + co.getId() + ", " + co.getSource().getText());
                    return result.toString();
                }
            }
        }
        // 从其他属性搜索
        if(result == null){
            for(CellObject co : this.otherCellObjectList){
                result = valueParser.getValue(co.getSource().getText());
                if(result != null){
                    logger.debug("找到'otherCellObjectList'解析的属性值:" + co.getId() + ", " + co.getSource().getText());
                    return result.toString();
                }
            }
        }
        return null;
    }
}