刘光辉
12 小时以前 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
package jnpf.base.model.ocr;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jnpf.base.model.ocr.model.*;
import jnpf.exception.DataException;
import jnpf.util.wxutil.HttpUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * ocr解析模型父类
 *
 * @author JNPF开发平台组
 * @version v6.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2025/7/29 16:58:47
 */
@Data
@Schema(description = "ocr解析模型父类")
@NoArgsConstructor
public class OcrModel {
    @JsonIgnore
    private String type;
    @JsonIgnore
    private List<String> strList;
    private String content;
 
    public static OcrModel get(String type) {
        OcrModel ocrModel = new OcrModel();
        if (OcrConstant.TEXT_OCR.equals(type)) {
            return ocrModel;
        }
        if (OcrConstant.BANK_CARD.equals(type)) {
            ocrModel = new BankCardModel();
        }
        if (OcrConstant.IDCARD_FRONT.equals(type) || OcrConstant.IDCARD_BACK.equals(type)) {
            ocrModel = new IdCardModel();
            ocrModel.setType(type);
            return ocrModel;
        }
        if (OcrConstant.BUSINESS_LICENSE.equals(type)) {
            ocrModel = new BusinessLicModel();
        }
        if (OcrConstant.INVOICE.equals(type)) {
            ocrModel = new InvoiceLicModel();
        }
        if (OcrConstant.DRIVING_LICENSE.equals(type)) {
            ocrModel = new DrivingLicModel();
        }
        if (OcrConstant.VEHICLE_LICENSE.equals(type)) {
            ocrModel = new VehicleLicModel();
        }
        if (OcrConstant.TRAIN_TICKET.equals(type)) {
            ocrModel = new TrainTicketModel();
        }
        return ocrModel;
    }
 
    public void getStrList(JSONObject obj) {
        List<String> list = new ArrayList<>();
        if (obj != null && obj.containsKey("status")) {
            JSONArray results = obj.getJSONArray("results");
            for (Object result : results) {
                JSONArray m = (JSONArray) result;
                for (Object n : m) {
                    JSONObject o = (JSONObject) n;
                    list.add(o.getString("text"));
                }
            }
        }
        strList = list;
    }
 
    public void extract(String url, JSONObject body) {
        JSONObject systemJson = this.getSystemJson(url, body);
        getStrList(systemJson);
        this.content = String.join("", strList);
    }
 
    public JSONObject getSystemJson(String url, JSONObject body) {
        JSONObject jsonObject = HttpUtil.httpRequest(url + "/predict/ocr_system", "POST", body.toJSONString());
        if (jsonObject == null) {
            throw new DataException("OCR服务器连接失败!");
        }
        return jsonObject;
    }
 
    public JSONObject getTableJson(String url, JSONObject body) {
        //structure_layout
        return HttpUtil.httpRequest(url + "/predict/structure_table", "POST", body.toJSONString());
    }
}