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
package com.walker.openocr.util;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.openocr.TextBlock;
 
public class BaiduOcrUtils {
 
    /**
     * 从json结果(OCR识别返回的JSON字符串文本)中,把每个字快生成一个对象。
     * @param wordNode
     * @return
     * @author 时克英
     * @date 2023-10-10
     */
    public static final TextBlock acquireTextBlock(JsonNode wordNode){
        JsonNode locationNode = wordNode.get("location");
        int top = locationNode.get("top").asInt();
        int left = locationNode.get("left").asInt();
        int width = locationNode.get("width").asInt();
        int height = locationNode.get("height").asInt();
        float[] start = {left, top};
        float[] end   = {left+width, top};
 
        TextBlock textBlock = new TextBlock();
        String text = wordNode.get("words").toString().replaceAll("\"", StringUtils.EMPTY_STRING);
        textBlock.setText(text);
        textBlock.setStartPosition(start);
        textBlock.setEndPosition(end);
        textBlock.setScore(0);
        return textBlock;
    }
}