liuxiaolong
2020-06-02 b3286a55d1f53bda18653923fff2d013802ff1b9
add image.go
1个文件已添加
111 ■■■■■ 已修改文件
extend/util/image.go 111 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extend/util/image.go
New file
@@ -0,0 +1,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
}