panlei
2019-06-29 2ef897766c8222b7d907ebed8a951e9a72f7877b
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
package util
 
import (
    "basic.com/pubsub/protomsg.git"
    "bytes"
    "fmt"
    "github.com/gogo/protobuf/proto"
    "image"
    "image/jpeg"
    "reflect"
)
// 按尺寸去切图
func Subimg(dbyte []byte,x0,y0,x1,y1 int,) protomsg.Image{
 
    bbb := bytes.NewBuffer(dbyte)                           // 必须加一个buffer 不然没有read方法就会报错
    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)                         //写入文件
}