-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_output.go
281 lines (253 loc) · 7.93 KB
/
image_output.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package output
import (
"bytes"
"errors"
"github.com/KangSpace/gqrcode/core/cons"
"github.com/KangSpace/gqrcode/core/model"
"github.com/KangSpace/gqrcode/util"
"github.com/KangSpace/gqrcode/util/imaging"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
)
// Define PNG output Here
type ImageOutput struct {
image *image.NRGBA
*BaseOutput
}
func NewOutput(output *BaseOutput) *ImageOutput {
out := &ImageOutput{BaseOutput: output}
out.initImage(output.Size)
return out
}
func NewPNGOutput(size int) *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: PNG, Size: size}}
out.initImage(size)
return out
}
// NewPNGOutput0 :Output a new PNG image by auto size.
func NewPNGOutput0() *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: PNG, Size: AUTO_SIZE}}
return out
}
func NewJPGOutput(size int) *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: JPG, Size: size}}
out.initImage(size)
return out
}
// NewJPGOutput0 :Output a new JPG image by auto size.
func NewJPGOutput0() *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: JPG, Size: AUTO_SIZE}}
return out
}
func NewGIFOutput(size int) *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: GIF, Size: size}}
out.initImage(size)
return out
}
// NewGIFOutput0 :Output a new GIF image by auto size.
func NewGIFOutput0() *ImageOutput {
out := &ImageOutput{BaseOutput: &BaseOutput{Type: GIF, Size: AUTO_SIZE}}
return out
}
func (out *ImageOutput) initImage(size int) {
// Point range : (0,0),(size-1,size-1)
out.image = image.NewNRGBA(image.Rect(0, 0, size, size))
out.modules = make([][]*bool, size+1)
for i := range out.modules {
out.modules[i] = make([]*bool, size+1)
}
}
// Init :init for output when size is AUTO_SIZE
func (out *ImageOutput) Init(version *model.Version, qz *model.QuietZone) {
if out.Size == AUTO_SIZE {
out.Size = version.GetDefaultPixelSize() + qz.GetDefaultPixelSize()
out.initImage(out.Size)
}
}
// Write : write data
func (out *ImageOutput) Write(x int, y int, black bool) {
setColor := out.getWriteColor(black, cons.DataPart)
out.image.Set(x, y, setColor)
out.modules[x][y] = &black
}
// WriteModule : write data
func (out *ImageOutput) WriteModule(x int, y int, black bool, pixelSize int, part cons.QRCodeStructPart) {
setColor := out.getWriteColor(black, part)
out.WriteModuleColor(x, y, black, setColor, pixelSize)
}
// getWriteColor: get color by BaseOutput.CodeColor
func (out *ImageOutput) getWriteColor(black bool, part cons.QRCodeStructPart) color.Color {
if black {
if out.BaseOutput.CodeColor.DataColor != nil {
var color = image.Black.C
switch part {
case cons.DataPart:
color = out.BaseOutput.CodeColor.DataColor
case cons.FinderPatternPart:
color = out.BaseOutput.CodeColor.FinderPatternColor
case cons.AlignmentPart:
color = out.BaseOutput.CodeColor.AlignmentPatternColor
case cons.QuietZonePart:
color = out.BaseOutput.CodeColor.QuietZoneColor
case cons.TimingPatternPart:
color = out.BaseOutput.CodeColor.TimingPatternColor
case cons.FormatPart:
color = out.BaseOutput.CodeColor.FormatColor
case cons.VersionPart:
color = out.BaseOutput.CodeColor.VersionColor
}
return color
}
return image.Black.C
}
return image.White.C
}
func (out *ImageOutput) WriteModuleColor(x int, y int, dark bool, setColor color.Color, pixelSize int) {
out.modules[x][y] = &dark
x = x * pixelSize
y = y * pixelSize
for i := 0; i < pixelSize; i++ {
for j := 0; j < pixelSize; j++ {
out.image.Set(x+i, y+j, setColor)
}
}
}
func (out *ImageOutput) IsModuleSet(x int, y int) bool {
return out.BaseOutput.IsModuleSet(x, y)
}
func (out *ImageOutput) GetModule(x int, y int) bool {
return out.BaseOutput.GetModule(x, y)
}
// Save : save file
func (out *ImageOutput) Save(fileName string) error {
if file, err := os.Create(fileName); err == nil {
defer file.Close()
return out.SaveToWriter(file)
} else {
return err
}
}
// SaveToWriter : save to any io.Writer
func (out *ImageOutput) SaveToWriter(writer io.Writer) error {
switch out.BaseOutput.Type {
case JPG:
return jpeg.Encode(writer, out.image, nil)
case PNG:
return png.Encode(writer, out.image)
case GIF:
return gif.Encode(writer, out.image, nil)
}
return errors.New("not supported \"" + string(out.BaseOutput.Type) + "\"")
}
// SaveToBase64 : save image to base64 string
func (out *ImageOutput) SaveToBase64() (base64Str string, err error) {
imageBytes := bytes.NewBuffer(nil)
var base64UrlImageType util.Base64URLImageType
switch out.BaseOutput.Type {
case JPG:
base64UrlImageType = util.JpegType
case PNG:
base64UrlImageType = util.PngType
case GIF:
base64UrlImageType = util.GifType
default:
return "", errors.New("not supported \"" + string(out.BaseOutput.Type) + "\"")
}
err = out.SaveToWriter(imageBytes)
if err != nil {
return "", err
}
return util.ImageToBase64Url(base64UrlImageType, imageBytes.Bytes()), nil
}
func (out *ImageOutput) GetBaseOutput() *BaseOutput {
return out.BaseOutput
}
func (out *ImageOutput) GetImage() *image.NRGBA {
return out.image
}
func (out *ImageOutput) drawNewImage(minPoint image.Point, maxPoint image.Point, imageSize int) {
newImg := image.NewNRGBA(image.Rect(0, 0, imageSize, imageSize))
draw.Draw(newImg, newImg.Bounds(), image.White, image.Pt(0, 0), draw.Src)
r := image.Rectangle{Min: minPoint, Max: maxPoint}
draw.Draw(newImg, r, out.image, image.Pt(0, 0), draw.Over)
//logo image handle
newImg = out.drawLogoImage(newImg)
out.image = newImg
}
func (out *ImageOutput) drawLogoImage(srcImage *image.NRGBA) *image.NRGBA {
logoOption := out.BaseOutput.containLogoOption()
if logoOption != nil {
logoFilePath := logoOption.Value
var logoFile *os.File
var err error
if logoFile, err = os.Open(logoFilePath); err != nil {
panic(err)
}
defer logoFile.Close()
var logoImg image.Image
if logoImg, err = png.Decode(logoFile); err != nil {
if logoImg, err = jpeg.Decode(logoFile); err != nil {
if logoImg, err = gif.Decode(logoFile); err != nil {
panic(err)
}
}
}
width := logoImg.Bounds().Dx()
height := logoImg.Bounds().Dy()
logoSize := width * height
remainSize := int(0.1 * float32(out.Size*out.Size))
// remain 10%
if remainSize < logoSize {
remainRate := float32(remainSize) / float32(logoSize)
width = int(float32(width) * remainRate)
height = int(float32(height) * remainRate)
}
x := (out.Size-width)/2 - 1
y := (out.Size-height)/2 - 1
logoImg = imaging.Resize0(logoImg, width, height)
r := image.Rectangle{Min: image.Pt(x, y), Max: image.Pt(x+width, y+height)}
draw.Draw(srcImage, r, logoImg, image.Pt(0, 0), draw.Over)
}
return srcImage
}
func (out *ImageOutput) drawIntoNewImage(minPoint image.Point, maxPoint image.Point) {
out.drawNewImage(minPoint, maxPoint, out.Size)
}
func (out *ImageOutput) ResizeToFit(moduleSize int, quietZoneSize int, pixelSize int) {
modulePixels := moduleSize * pixelSize
quietZonePixels := quietZoneSize * pixelSize
if out.Size == modulePixels {
return
} else if out.Size == modulePixels+quietZonePixels {
imgX := quietZonePixels / 2
imgY := quietZonePixels / 2
out.drawIntoNewImage(image.Pt(imgX, imgY), image.Point{X: imgX + moduleSize*pixelSize, Y: imgY + moduleSize*pixelSize})
return
} else {
maxImageSize := modulePixels + quietZonePixels
imgX := 0
imgY := 0
if quietZoneSize > 0 {
imgX = quietZonePixels / 2
imgY = quietZonePixels / 2
}
out.drawNewImage(image.Pt(imgX, imgY), image.Point{X: imgX + moduleSize*pixelSize, Y: imgY + moduleSize*pixelSize}, maxImageSize)
out.image = imaging.Resize(out.image, out.Size)
}
return
}
func (out *ImageOutput) GetColor() CodeColor {
return DefaultCodeColor
}
// Clone : Shallow copy BaseOutput and modules from output, init new image instance
func (out *ImageOutput) Clone() Output {
clone := &ImageOutput{BaseOutput: &BaseOutput{Type: out.Type, Size: out.Size, Options: out.Options, CodeColor: out.CodeColor}}
clone.initImage(out.Size)
return clone
}