-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage.go
73 lines (62 loc) · 1.31 KB
/
image.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package sdk_camera
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"io"
)
import (
"bytes"
"golang.org/x/image/bmp"
)
// BGRToJpeg BGR转jpeg
func BGRToJpeg(o io.Writer, bgr []byte, w int, h int, opt *jpeg.Options) error {
if len(bgr) != w*h*3 {
return fmt.Errorf("bgr input error")
}
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
x, y := 0, 0
for i := 0; i < len(bgr); i++ {
if i > 0 && i%3 == 0 {
b, g, r := bgr[i-3], bgr[i-2], bgr[i-1]
rgba.Set(x, y, color.RGBA{R: r, G: g, B: b, A: 0})
if x > 0 && x%(w-1) == 0 {
x = 0
y++
} else {
x++
}
}
}
return jpeg.Encode(o, rgba, opt)
}
// GrayToJpeg 灰度图转jpeg
func GrayToJpeg(o io.Writer, gray []byte, w int, h int, opt *jpeg.Options) error {
if len(gray) != w*h {
return fmt.Errorf("gray input error")
}
grayImage := image.NewGray(image.Rect(0, 0, w, h))
for i, x, y := 0, 0, 0; i < len(gray); i++ {
grayImage.Set(x, y, color.Gray{Y: gray[i]})
switch {
case x == 0:
x++
case x%(w-1) != 0:
x++
case x%(w-1) == 0:
x = 0
y++
}
}
return bmp.Encode(o, grayImage)
}
// ImageToJpeg image对象转jpeg
func ImageToJpeg(img image.Image) ([]byte, error) {
buf := &bytes.Buffer{}
err := jpeg.Encode(buf, img, &jpeg.Options{Quality: 75})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}