From b3286a55d1f53bda18653923fff2d013802ff1b9 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 02 六月 2020 19:39:00 +0800
Subject: [PATCH] add image.go
---
extend/util/image.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 111 insertions(+), 0 deletions(-)
diff --git a/extend/util/image.go b/extend/util/image.go
new file mode 100644
index 0000000..585d0e1
--- /dev/null
+++ b/extend/util/image.go
@@ -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
+}
\ No newline at end of file
--
Gitblit v1.8.0