liuxiaolong
2020-06-09 783ea0c3f6aa5a74c1f858514939696ba623adc7
fix DrawRect,use drawLine
3个文件已修改
210 ■■■■■ 已修改文件
controllers/fileController.go 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/util/image.go 165 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/util/util.go 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/fileController.go
@@ -181,14 +181,8 @@
    }
    red := color.RGBA{255, 0, 0, 0}
    for _,faceResult := range *faceArr {
        rect := image.Rect(int(faceResult.Pos.RcFace.Left),
            int(faceResult.Pos.RcFace.Top),
            int(faceResult.Pos.RcFace.Right),
            int(faceResult.Pos.RcFace.Bottom))
        err = godraw.DrawRectangle(img, rect, red, 3)
        if err != nil {
            logger.Debug("godraw.DrawRectangle err:", err)
        }
        util.DrawRect(img.(*image.RGBA),int(faceResult.Pos.RcFace.Left),int(faceResult.Pos.RcFace.Top),int(faceResult.Pos.RcFace.Right),
            int(faceResult.Pos.RcFace.Bottom), red)
    }
    quantity := 100
extend/util/image.go
@@ -61,51 +61,142 @@
    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
        }
// 长宽变为一比一,每边各扩百分之20
func EnlargeSizeForCar(x0, y0, x1, y1 int, i protomsg.Image) (x0_new, y0_new, x1_new, y1_new int) {
    // 先把长宽变为一比一
    chazhi := (y1 - y0) - (x1 - x0)
    x0 = x0 - chazhi/2
    if x0 < 0 {
        x0 = 0
    }
    x1 = x1 + chazhi/2
    if x1 > int(i.Width) {
        x1 = int(i.Width)
    }
    return nil, err
    // 再把每边各扩大百分之20
    enlarge := float32(0.2)
    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 > int(i.Width) {
        x1_new = int(i.Width)
    }
    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 > int(i.Height) {
        y1_new = int(i.Height)
    }
    return
}
func DrawPolygon(img *protomsg.Image, points []protomsg.Point) ([]byte, error) {
// 不扩边
func EnlargeSize(x0, y0, x1, y1 int, i protomsg.Image) (int, int, int, int) {
    var pts []image.Point
    for _, pt := range points {
        pts = append(pts, image.Point{
            X: int(pt.X),
            Y: int(pt.Y),
        })
    // 先把长宽变为一比一
    chazhi := (y1 - y0) - (x1 - x0)
    x0 = x0 - chazhi/2
    if x0 < 0 {
        x0 = 0
    }
    yellow := color.RGBA{255, 255, 0, 0}
    x1 = x1 + chazhi/2
    if x1 > int(i.Width) {
        x1 = int(i.Width)
    }
    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
    if y0 < 0 {
        y0 = 0
    }
    if y1 > int(i.Height) {
        y1 = int(i.Height)
    }
    return x0, y0, x1, y1
}
// 长宽变为一比一,每边各扩百分之50
func EnlargeSizeForTem(x0, y0, x1, y1 int, i protomsg.Image) (x0_new, y0_new, x1_new, y1_new int) {
    // 先把长宽变为一比一
    chazhi := (y1 - y0) - (x1 - x0)
    x0 = x0 - chazhi/2
    if x0 < 0 {
        x0 = 0
    }
    x1 = x1 + chazhi/2
    if x1 > int(i.Width) {
        x1 = int(i.Width)
    }
    // 再把每边各扩大百分之50
    enlarge := float32(0.5)
    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 > int(i.Width) {
        x1_new = int(i.Width)
    }
    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 > int(i.Height) {
        y1_new = int(i.Height)
    }
    return
}
func drawLine(img *image.RGBA, x0, y0, x1, y1 int, c color.Color) {
    var dx int
    if x1 > x0 {
        dx = x1 - x0
    } else {
        dx = x0 - x1
    }
    var dy int
    if y1 > y0 {
        dy = y1 - y0
    } else {
        dy = y0 - y1
    }
    sx, sy := 1, 1
    if x0 >= x1 {
        sx = -1
    }
    if y0 >= y1 {
        sy = -1
    }
    err := dx - dy
    for {
        img.Set(x0, y0, c)
        if x0 == x1 && y0 == y1 {
            return
        }
        e2 := err * 2
        if e2 > -dy {
            err -= dy
            x0 += sx
        }
        if e2 < dx {
            err += dx
            y0 += sy
        }
    }
}
    return nil, err
func DrawRect(img *image.RGBA, x0, y0, x1, y1 int, c color.Color) {
    drawLine(img, x0, y0, x1, y0, c)
    drawLine(img, x1, y0, x1, y1, c)
    drawLine(img, x1, y1, x0, y1, c)
    drawLine(img, x0, y1, x0, y0, c)
}
extend/util/util.go
@@ -18,7 +18,6 @@
    "time"
    "webserver/extend/code"
    "basic.com/pubsub/protomsg.git"
    "crypto/rand"
    "fmt"
    "github.com/gin-gonic/gin"
@@ -93,40 +92,6 @@
        return nil, err
    }
    return pix, nil
}
// 长宽变为一比一,每边各扩百分之20
func EnlargeSize(x0, y0, x1, y1 int, i protomsg.Image) (x0_new, y0_new, x1_new, y1_new int) {
    // 先把长宽变为一比一
    chazhi := (y1 - y0) - (x1 - x0)
    x0 = x0 - chazhi/2
    if x0 < 0 {
        x0 = 0
    }
    x1 = x1 + chazhi/2
    if x1 > int(i.Width) {
        x1 = int(i.Width)
    }
    // 再把每边各扩大百分之20
    enlarge := float32(0.2)
    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 > int(i.Width) {
        x1_new = int(i.Width)
    }
    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 > int(i.Height) {
        y1_new = int(i.Height)
    }
    return
}
func StringIpToInt(ipstring string) int {