liuxiaolong
2020-06-06 aa26c2c62693dcf7e8484fbc831e67b040db2fa0
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
package util
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/godraw.git"
    "image"
    "image/color"
)
 
// 按尺寸去切图
func SubCutImg(src *protomsg.Image, rect *protomsg.Rect, enlarge int) ([]byte, error){
    bkImg, _ := godraw.ToImage(src.Data, int(src.Width), int(src.Height))
 
    rectNew := image.Rect(EnlargeCut(rect, int(src.Width), int(src.Height), enlarge))
    subImg := bkImg.(*image.RGBA).SubImage(rectNew)
 
    bytes,err := godraw.ImageToJpeg(subImg, nil)
    if err != nil {
        return nil, err
    }
    return bytes, nil
}
 
// 长宽变为一比一,每边各扩百分之...
func EnlargeCut(rect *protomsg.Rect, maxW,maxH int, enlargePer int) (x0_new, y0_new, x1_new, y1_new int) {
    // 先把长宽变为一比一
    x0 := int(rect.Left)
    y0 := int(rect.Top)
    x1 := int(rect.Right)
    y1 := int(rect.Bottom)
 
    chazhi := (y1 - y0) - (x1 - x0)
    x0 = x0 - chazhi/2
    if x0 < 0 {
        x0 = 0
    }
    x1 = x1 + chazhi/2
    if x1 > maxW {
        x1 = maxW
    }
 
    // 再把每边各扩大百分之...
    enlarge := float32(enlargePer / 100)
    x0_new = int((1+enlarge)*float32(x0) - enlarge*float32(x1))
    if x0_new < 0 {
        x0_new = 0
    }
    x1_new = int((1+enlarge)*float32(x1) - enlarge*float32(x0))
    if x1_new > maxW {
        x1_new = maxW
    }
    y0_new = int((1+enlarge)*float32(y0) - enlarge*float32(y1))
    if y0_new < 0 {
        y0_new = 0
    }
    y1_new = int((1+enlarge)*float32(y1) - enlarge*float32(y0))
    if y1_new > maxH {
        y1_new = maxH
    }
 
    return x0_new, y0_new, x1_new, y1_new
}
 
func DrawRect(img *protomsg.Image, rect *protomsg.Rect) ([]byte, error) {
    red := color.RGBA{255, 0, 0, 0}
 
    targetRect := image.Rect(int(rect.Left), int(rect.Top), int(rect.Right), int(rect.Bottom))
    bgImg, err := godraw.ToImage(img.Data, int(img.Width), int(img.Height))
    if err == nil {
        err1 := godraw.DrawRectangle(bgImg, targetRect, red, 2)
        if err1 != nil {
            return nil, err1
        } else {
            bytes,err := godraw.ImageToJpeg(bgImg, nil)
            if err != nil {
                return nil, err
            }
            return bytes, nil
        }
    }
 
    return nil, err
}
 
func DrawPolygon(img *protomsg.Image, points []protomsg.Point) ([]byte, error) {
 
    var pts []image.Point
    for _, pt := range points {
        pts = append(pts, image.Point{
            X: int(pt.X),
            Y: int(pt.Y),
        })
    }
    yellow := color.RGBA{255, 255, 0, 0}
 
    bgImg, err := godraw.ToImage(img.Data, int(img.Width), int(img.Height))
    if err == nil {
        err1 := godraw.DrawPolygon(bgImg, pts, yellow,2)
        if err1 != nil {
            return nil, err1
        } else {
            bytes,err := godraw.ImageToJpeg(bgImg, nil)
            if err != nil {
                return nil, err
            }
            return bytes, nil
        }
    }
 
    return nil, err
}