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 } // 长宽变为一比一,每边各扩百分之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) } // 再把每边各扩大百分之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 EnlargeSize(x0, y0, x1, y1 int, i protomsg.Image) (int, int, int, 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) } 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 } } } 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) }