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
package com.walker.openocr.support;
 
import com.baidu.aip.ocr.AipOcr;
import com.fasterxml.jackson.databind.JsonNode;
import com.walker.infrastructure.ApplicationRuntimeException;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.openocr.AbstractOcrLoader;
import com.walker.openocr.TextBlock;
import com.walker.openocr.util.BaiduOcrUtils;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
 
public class BaiduOcrLoader extends AbstractOcrLoader {
 
    private HashMap<String, String> options;
 
    public BaiduOcrLoader(){
 
    }
 
    @Override
    public List<TextBlock> loadText(byte[] image, HashMap<String, String> options) {
        this.checkOcrClient();
        JSONObject res = client.general(image, options); // 包含位置
        if(res.has("error_code")){
            logger.error("调用返回错误:" + res.getString("error_msg"));
            return null;
        }
 
//        List<TextBlock> textBlocks = new ArrayList<>();
//        try {
//            JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(res.toString(2));
//            JsonNode wordNode = null;
//            for(Iterator<JsonNode> it = jsonNode.get("words_result").elements(); it.hasNext();){
//                wordNode = it.next();
//                textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode));
//            }
//        } catch (Exception e) {
//            logger.error("res = {}", res);
//            throw new ApplicationRuntimeException("百度返回json数据,系统解析错误:" + e.getMessage(), e);
//        }
        return this.doLoad(res);
    }
 
    @Override
    public List<TextBlock> loadText(String imagePath, HashMap<String, String> options) {
        this.checkOcrClient();
        JSONObject res = client.general(imagePath, options); // 包含位置
        if(res.has("error_code")){
            logger.error("调用返回错误:" + res.getString("error_msg"));
            return null;
        }
        return this.doLoad(res);
    }
 
    private List<TextBlock> doLoad(JSONObject res){
        List<TextBlock> textBlocks = new ArrayList<>();
        try {
            JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(res.toString(2));
            JsonNode wordNode = null;
            for(Iterator<JsonNode> it = jsonNode.get("words_result").elements(); it.hasNext();){
                wordNode = it.next();
                textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode));
            }
        } catch (Exception e) {
            logger.error("res = {}", res);
            throw new ApplicationRuntimeException("百度返回json数据,系统解析错误:" + e.getMessage(), e);
        }
        return textBlocks;
    }
 
    private void checkOcrClient(){
        if(this.client == null){
            if(StringUtils.isEmpty(this.appId) || StringUtils.isEmpty(this.apiKey) || StringUtils.isEmpty(this.secretKey)){
                throw new IllegalArgumentException("appId, apiKey, secretKey为空,无法初始化百度OCR client!");
           }
            client = new AipOcr(this.appId, this.apiKey, this.secretKey);
            // 可选:设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
 
            // 传入可选参数调用接口
            options = new HashMap<String, String>();
            options.put("language_type", "CHN_ENG");
            options.put("detect_direction", "true");
            options.put("detect_language", "true");
            options.put("probability", "true");
            // 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false
//        options.put("vertexes_location", "true");
            logger.info("百度: AipOcr初始化成功");
        }
    }
 
    public String getAppId() {
        return appId;
    }
 
    public void setAppId(String appId) {
        this.appId = appId;
    }
 
    public String getApiKey() {
        return apiKey;
    }
 
    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }
 
    public String getSecretKey() {
        return secretKey;
    }
 
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
 
    private String appId;
    private String apiKey;
    private String secretKey;
    private AipOcr client = null;
}