forked from yeqown/go-qrcode
/
image.go
205 lines (182 loc) · 5.44 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package qrcode
import (
"fmt"
"image"
"image/color"
"io"
"log"
"os"
"github.com/fogleman/gg"
"github.com/lampalink/go-qrcode/matrix"
)
// Draw image with matrix
var (
_defaultFilename = "default.jpeg"
_defaultPadding = 40
)
// drawAndSaveToFile image with matrix
func drawAndSaveToFile(name string, m matrix.Matrix, opt *outputImageOptions) error {
f, err := os.Create(name)
if err != nil {
return fmt.Errorf("could not create file: %v", err)
}
defer f.Close()
return drawAndSave(f, m, opt)
}
// drawAndSave save image into io.Writer
func drawAndSave(w io.Writer, m matrix.Matrix, opt *outputImageOptions) (err error) {
if opt == nil {
opt = _defaultOutputOption
}
img := draw(m, opt)
// DONE(@yeqown): support file format specified config option
if err = opt.imageEncoder.Encode(w, img); err != nil {
err = fmt.Errorf("jpeg.Encode got err: %v", err)
}
return
}
//
//// draw deal QRCode's matrix to be a image.Image
//func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
// // w as image width, h as image height
// w := mat.Width()*opt.qrBlockWidth() + 2*_defaultPadding
// h := w
// rgba := image.NewRGBA(image.Rect(0, 0, w, h))
//
// // top-bottom _defaultPadding
// for posX := 0; posX < w; posX++ {
// for posY := 0; posY < _defaultPadding; posY++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
//
// for posY := h - _defaultPadding; posY < h; posY++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
// }
//
// // left-right _defaultPadding
// for posY := _defaultPadding; posY < h-_defaultPadding; posY++ {
// for posX := 0; posX < _defaultPadding; posX++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
//
// for posX := w - _defaultPadding; posX < w; posX++ {
// rgba.Set(posX, posY, opt.backgroundColor())
// }
// }
//
// ctx := &DrawContext{
// UpperLeft: image.Point{}, // useless
// lowerRight: image.Point{}, // useless
// img: rgba,
// color: color.Black, // useless
// }
// shape := opt.getShape()
//
// // iterate the matrix to Draw each pixel
// mat.Iterate(matrix.ROW, func(x int, y int, v matrix.State) {
// // Draw the block
// ctx.UpperLeft = image.Point{
// X: x*opt.qrBlockWidth() + _defaultPadding,
// Y: y*opt.qrBlockWidth() + _defaultPadding,
// }
// ctx.lowerRight = image.Point{
// X: (x+1)*opt.qrBlockWidth() + _defaultPadding,
// Y: (y+1)*opt.qrBlockWidth() + _defaultPadding,
// }
// ctx.color = opt.stateRGBA(v)
// // DONE(@yeqown): make this abstract to Shapes
// shape.Draw(ctx)
// })
//
// // DONE(@yeqown): add logo image
// if opt.logoImage() != nil {
// // Draw logo image into rgba
// bound := opt.logo.Bounds()
// UpperLeft, lowerRight := bound.Min, bound.Max
// logoWidth, logoHeight := lowerRight.X-UpperLeft.X, lowerRight.Y-UpperLeft.Y
//
// if !validLogoImage(w, h, logoWidth, logoHeight) {
// log.Printf("w=%d, h=%d, logoW=%d, logoH=%d, logo is over than 1/5 of QRCode \n",
// w, h, logoWidth, logoHeight)
// goto done
// }
//
// // DONE(@yeqown): calculate the xOffset and yOffset
// // which point(xOffset, yOffset) should icon upper-left to start
// xOffset, yOffset := (w-logoWidth)/2, (h-logoHeight)/2
//
// for posX := UpperLeft.X; posX < lowerRight.X; posX++ {
// for posY := UpperLeft.Y; posY < lowerRight.Y; posY++ {
// rgba.Set(posX+xOffset, posY+yOffset, opt.logo.At(posX, posY))
// }
// }
// }
//done:
// return rgba
//}
// draw deal QRCode's matrix to be a image.Image
func draw(mat matrix.Matrix, opt *outputImageOptions) image.Image {
if _debug {
fmt.Printf("matrix.Width()=%d, matrix.Height()=%d\n", mat.Width(), mat.Height())
}
// w as image width, h as image height
w := mat.Width()*opt.qrBlockWidth() + 2*_defaultPadding
h := w
// rgba := image.NewRGBA(image.Rect(0, 0, w, h))
dc := gg.NewContext(w, h)
// draw background
dc.SetColor(opt.backgroundColor())
dc.DrawRectangle(0, 0, float64(w), float64(h))
dc.Fill()
// qrcode block draw context
ctx := &DrawContext{
Context: dc,
UpperLeft: image.Point{},
W: opt.qrBlockWidth(),
H: opt.qrBlockWidth(),
Color: color.Black,
MatrixW: mat.Width(),
MatrixH: mat.Height(),
}
shape := opt.getShape()
// iterate the matrix to Draw each pixel
mat.Iterate(matrix.ROW, func(x int, y int, v matrix.State) {
// Draw the block
ctx.CursorX = x
ctx.CursorY = y
ctx.UpperLeft = image.Point{
X: x*opt.qrBlockWidth() + _defaultPadding,
Y: y*opt.qrBlockWidth() + _defaultPadding,
}
ctx.Color = opt.stateRGBA(v)
// DONE(@yeqown): make this abstract to Shapes
shape.Draw(ctx)
//if x == y && _debug {
// _ = dc.SavePNG(fmt.Sprintf("./.debug/%d.png", x))
//}
})
//if _debug {
// fmt.Printf("save as tmp.png, err=%v\n", dc.SavePNG("./tmp.png"))
//}
// DONE(@yeqown): add logo image
if opt.logoImage() != nil {
// Draw logo image into rgba
bound := opt.logo.Bounds()
upperLeft, lowerRight := bound.Min, bound.Max
logoWidth, logoHeight := lowerRight.X-upperLeft.X, lowerRight.Y-upperLeft.Y
if !validLogoImage(w, h, logoWidth, logoHeight) {
log.Printf("w=%d, h=%d, logoW=%d, logoH=%d, logo is over than 1/5 of QRCode \n",
w, h, logoWidth, logoHeight)
goto done
}
// DONE(@yeqown): calculate the xOffset and yOffset
// which point(xOffset, yOffset) should icon upper-left to start
dc.DrawImage(opt.logoImage(), (w-logoWidth)/2, (h-logoHeight)/2)
}
done:
return dc.Image()
}
func validLogoImage(qrWidth, qrHeight, logoWidth, logoHeight int) bool {
return qrWidth >= 5*logoWidth && qrHeight >= 5*logoHeight
}