zouyu
2025-11-27 eed98e551c817ead7965e08820d4b7adbc4a47f0
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
package com.xindao.ocr.smartjavaai.entity;
 
import cn.smartjavaai.common.entity.DetectionRectangle;
import cn.smartjavaai.common.entity.Point;
import lombok.Data;
 
/**
 * OCR 检测框
 * @author dwj
 * @date 2025/5/20
 */
@Data
public class OcrBox {
 
    /**
     * 左上角
     */
    private Point topLeft;
 
    /**
     * 右上角
     */
    private Point topRight;
 
    /**
     * 右下角
     */
    private Point bottomRight;
 
    /**
     * 左下角
     */
    private Point bottomLeft;
 
    public OcrBox(Point topLeft, Point topRight, Point bottomRight, Point bottomLeft) {
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomRight = bottomRight;
        this.bottomLeft = bottomLeft;
    }
 
    public OcrBox() {
    }
 
    public float[] toFloatArray() {
        return new float[]{
                (float)topLeft.getX(), (float)topLeft.getY(),
                (float)topRight.getX(), (float)topRight.getY(),
                (float)bottomRight.getX(), (float)bottomRight.getY(),
                (float)bottomLeft.getX(), (float)bottomLeft.getY()
        };
    }
 
    /**
     * 转换为 DetectionRectangle,使用最小外包矩形
     */
    public DetectionRectangle toDetectionRectangle() {
        float[] pts = toFloatArray();
        float minX = Math.min(Math.min(pts[0], pts[2]), Math.min(pts[4], pts[6]));
        float minY = Math.min(Math.min(pts[1], pts[3]), Math.min(pts[5], pts[7]));
        float maxX = Math.max(Math.max(pts[0], pts[2]), Math.max(pts[4], pts[6]));
        float maxY = Math.max(Math.max(pts[1], pts[3]), Math.max(pts[5], pts[7]));
        DetectionRectangle rect = new DetectionRectangle();
        rect.setX((int) minX);
        rect.setY((int) minY);
        rect.setWidth((int) (maxX - minX));
        rect.setHeight((int) (maxY - minY));
        return rect;
    }
 
 
}