shikeying
2024-04-08 8c3f7605718d7b5b3f3fa6de8922c5ee132b0890
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
package com.walker.openocr;
 
import com.baidu.aip.ocr.AipOcr;
import com.fasterxml.jackson.databind.JsonNode;
import com.walker.infrastructure.utils.FileUtils;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.openocr.oil.OilPrintResolver;
import com.walker.openocr.table.TableConfig;
import com.walker.openocr.table.TableObject;
import com.walker.openocr.table.TableTextResolver;
import com.walker.openocr.table.config.FileConfigLoader;
import com.walker.openocr.util.BaiduOcrUtils;
import com.walker.openocr.util.FileReaderUtils;
import org.json.JSONObject;
import org.junit.Test;
 
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
 
public class BaiduOcrTest {
 
    //设置APPID/AK/SK
    public static final String APP_ID = "18547554";
    public static final String API_KEY = "epNhm4LRAhSpDTCB6Wfc8VQO";
    public static final String SECRET_KEY = "ICwmmmVQB7A89dhtiPfu0q8OiwDh1Qpb";
 
    private AipOcr client = null;
 
    private String folder = "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources";
 
//    @Test
    public void resolveOilSinopecWx() throws Exception{
        String filePath = folder + "/oil_json_dqsh_print.txt";
        byte[] content = FileUtils.getFileBytes(new File(filePath));
        JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(new String(content));
        int textCount = jsonNode.get("words_result_num").asInt();
        System.out.println("共有文本数量:" + textCount);
 
        List<TextBlock> textBlocks = new ArrayList<>();
 
        JsonNode wordNode = null;
        for(Iterator<JsonNode> it = jsonNode.get("words_result").elements(); it.hasNext();){
            wordNode = it.next();
            textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode));
        }
 
        FileConfigLoader oilSinopecConfig = new FileConfigLoader();
        TableConfig tableConfig = oilSinopecConfig.load(folder + "/oil_config_zsh_wx.txt");
 
        List<TableConfig> tableConfigs = new ArrayList<>(4);
        tableConfigs.add(tableConfig);
 
        TextResolver<TableObject, TableConfig> textResolver = new OilPrintResolver();
        TableObject tableObject = textResolver.resolve(textBlocks, tableConfigs);
        System.out.println("金额:" + tableObject.getTableDataMap().get("jin_e"));
        System.out.println("支付时间:" + tableObject.getTableDataMap().get("zhi_fu_shi_jian"));
    }
 
    @Test
    public void textOcr(){
        client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
 
        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
 
        // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
 
        // 传入可选参数调用接口
        HashMap<String, String> 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");
 
        // 调用接口
//        String path = "F:/app_ocr_demo/ocr_oil_002.jpg";
//        String path = "F:/app_ocr_demo/ocr_oil_010.jpg";
//        String path = "F:/app_ocr_demo/ocr_oil_014.jpg";
//        String path = "F:/app_ocr_demo/ocr_e_002.jpg";
        String path = "F:/app_ocr_demo/ocr_e_022.jpg";
//        String path = "F:/app_ocr_demo/ocr_oil_017.jpg";
//        String path = "F:/app_ocr_demo/ocr_oil_011.jpg";
//        JSONObject res = client.basicGeneral(path, options);
        JSONObject res = client.general(path, options); // 包含位置
        if(res.has("error_code")){
            System.out.println("调用返回错误:" + res.getString("error_msg"));
            return;
        }
        String data = res.toString(2);
//        FileReaderUtils.saveJson2File(data, "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources/oil_json_default_print.txt");
        System.out.println(data);
    }
 
//    @Test
    public void readJson2BlockText() throws Exception{
        String filePath = "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources/oil_json.txt";
        byte[] content = FileUtils.getFileBytes(new File(filePath));
        JsonNode jsonNode = JsonUtils.jsonStringToObjectNode(new String(content));
        int textCount = jsonNode.get("words_result_num").asInt();
        System.out.println("共有文本数量:" + textCount);
 
        List<TextBlock> textBlocks = new ArrayList<>();
 
        JsonNode wordNode = null;
        for(Iterator<JsonNode> it = jsonNode.get("words_result").elements(); it.hasNext();){
            wordNode = it.next();
            textBlocks.add(BaiduOcrUtils.acquireTextBlock(wordNode));
        }
 
        for(TextBlock block : textBlocks){
            System.out.println(block);
        }
    }
}