---
panlei
2019-07-02 ef4901d4f39a8976cc66c7c73fbd519d7ae100ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package util
 
import (
    "gocv.io/x/gocv"
    "image"
    "log"
)
// 按尺寸去切图
func Subimg(dbyte []byte,x0,y0,x1,y1 int,) []byte{
 
    //bbb := bytes.NewBuffer(dbyte)                           // 必须加一个buffer 不然没有read方法就会报错
    ////log.Println("==================看看这个buffer",bbb)
    //m, _, _ := image.Decode(bbb)                          // 图片文件解码
    //rgbImg := m.(*image.YCbCr)
    //subImg := rgbImg.SubImage(image.Rect(x0, y0, x1, y1)) //图片裁剪x0 y0 x1 y1
    //fmt.Println(reflect.TypeOf(subImg))
    ////f, _ := os.Create("./test.jpg")                   //创建文件
    ////defer f.Close()                                         //关闭文件
    //emptyBuff := bytes.NewBuffer(nil)                 //开辟一个新的空buff
    //jpeg.Encode(emptyBuff, subImg, nil)                //img写入到buff
    //bytes := emptyBuff.Bytes()
    //i := protomsg.Image{}
    //proto.Unmarshal(bytes,&i)
    //return i
    //f, _ := os.Create("./test.jpg")                   //创建文件
    //defer f.Close()                                         //关闭文件
    //jpeg.Encode(f, subImg, nil)                         //写入文件
    log.Println("--------------------------四大金刚:",x0,y0,x1,y1)
    img,_ := gocv.NewMatFromBytes(720,1280,gocv.MatTypeCV8UC3,dbyte)
    rect := image.Rect(x0,y0,x1,y1)
    region := img.Region(rect)
    bytes, _ := gocv.IMEncode(".jpg", region)
    return bytes
}