From 783ea0c3f6aa5a74c1f858514939696ba623adc7 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期二, 09 六月 2020 14:18:37 +0800 Subject: [PATCH] fix DrawRect,use drawLine --- extend/util/image.go | 165 ++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 128 insertions(+), 37 deletions(-) diff --git a/extend/util/image.go b/extend/util/image.go index 585d0e1..509e338 100644 --- a/extend/util/image.go +++ b/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) } \ No newline at end of file -- Gitblit v1.8.0