-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.go
421 lines (317 loc) · 8.07 KB
/
canvas.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package captcha
import (
"embed"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"image"
"image/color"
"image/draw"
"math"
"math/rand"
"time"
"unicode/utf8"
)
//go:embed fonts/*
var fontFS embed.FS
var defaultFont *truetype.Font
func init() {
fontdata, _ := fontFS.ReadFile("fonts/SourceHanSerifCN-Light.ttf")
defaultFont, _ = freetype.ParseFont(fontdata)
rand.Seed(time.Now().UnixNano())
// fmt.Printf("defaultFont %+v :err:%+v", defaultFont, err)
}
type Canvas struct {
*image.NRGBA
Width int
Height int
Config Config
}
//DrawBackgroud 设置画板背景
func (c *Canvas) DrawBackgroud() {
backgroupColor := image.NewUniform(c.Config.BackgroupColor)
draw.Draw(c, c.Bounds(), backgroupColor, image.ZP, draw.Over)
}
func (c *Canvas) DrawLines(lineCount int) {
for lineCount > 0 {
p1 := Point{X: rand.Intn(c.Width), Y: rand.Intn(c.Height)}
p2 := Point{X: rand.Intn(c.Width), Y: rand.Intn(c.Height)}
c.DrawLine(p1, p2, randomColor())
lineCount--
}
}
//DrawLine 画干扰线
func (c *Canvas) DrawLine(p1, p2 Point, color color.Color) {
var k float64 = 0
var b float64 = 0
if (p2.X - p1.X) != 0 {
k = float64(p2.Y-p1.Y) / float64(p2.X-p1.X)
}
b = float64(p1.Y) - k*float64(p1.X)
sx := 1
offsetX := p1.X
if p2.X < p1.X {
sx = -1
}
if k == 0 && p2.X == p1.X {
tmpY := p1.Y
if p2.Y < p1.Y {
sx = -1
}
for {
c.Set(offsetX, tmpY, color)
if tmpY == p2.Y {
break
}
tmpY = tmpY + sx
}
return
}
for {
offsetY := int(k*float64(offsetX) + b)
c.Set(offsetX, offsetY, color)
if offsetX == p2.X {
break
}
offsetX = offsetX + sx
}
}
//DrawBezierLines 画贝塞尔干扰线
func (c *Canvas) DrawBezierLines(lineCount int) {
for lineCount > 0 {
lineColor := randomColor()
x1 := rand.Intn(c.Width / 4)
y1 := randomInt(c.Height/4, int(float64(c.Height)*0.9))
x2 := randomInt(c.Width/2, int(float64(c.Width)*0.9))
y2 := randomInt(c.Height/4, int(float64(c.Height)*0.9))
cx := randomInt(c.Width/4, int(float64(c.Width)*0.7))
cy := randomInt(c.Height/4, int(float64(c.Height)*0.6))
// B(t) = (1-t)P0 + 2t(1-t)P1 + tP2, t∈[0,1]
// x = Math.pow(1-t, 2) * x1 + 2 * t * (1-t) * cx + Math.pow(t, 2) * x2
// y = Math.pow(1-t, 2) * y1 + 2 * t * (1-t) * cy + Math.pow(t, 2) * y2
var t float64
for t < 1 {
x := math.Pow(1-t, 2.0)*float64(x1) + 2*t*(1-t)*float64(cx) + math.Pow(t, 2.0)*float64(x2)
y := math.Pow(1-t, 2)*float64(y1) + 2*t*(1-t)*float64(cy) + math.Pow(t, 2)*float64(y2)
c.Set(int(x), int(y), lineColor)
t = t + 0.001
}
lineCount--
}
}
//getFontPostions 获取所有字体随机位置
func (c *Canvas) getFontPostions(text string) []Point {
chCount := utf8.RuneCountInString(text)
var points []Point
if c.Config.Style == CaptchaStyle_Normal {
padding := c.Config.FontSize / 2
span := (c.Width - padding*2) / chCount
y := (c.Height + c.Config.FontSize) / 2
for i := 0; i < chCount; i++ {
x := padding + span*i
p := Point{X: x, Y: y}
points = append(points, p)
}
} else {
for i := 0; i < chCount; i++ {
p := c.randomFontPosition(56)
points = append(points, p)
}
}
return points
}
//DrawString 绘制文字验证码
func (c *Canvas) DrawString(text string) []DrawRect {
var drawRects []DrawRect
textPos := c.getFontPostions(text)
var index = 0
for _, ch := range text {
pos := textPos[index]
fontImg, areaPoint := c.DrawFont(string(ch), c.Config.FontColors)
// if c.Config.Style == CaptchaStyle_Normal {
// //居中显示
// pos.Y = (c.Height + areaPoint.MaxY) / 2
// fmt.Printf("pos %+v - %+v;", pos, fontImg.Bounds().Size())
// }
minX := areaPoint.MinX
maxX := areaPoint.MaxX
minY := areaPoint.MinY
maxY := areaPoint.MaxY
width := maxX - minX
height := maxY - minY
nW := fontImg.Bounds().Max.X
nH := fontImg.Bounds().Max.Y
for x := 0; x < nW; x++ {
for y := 0; y < nH; y++ {
co := fontImg.At(x, y)
if _, _, _, a := co.RGBA(); a > 0 {
if x >= minX && x <= maxX && y >= minY && y <= maxY {
c.Set(pos.X+(x-minX), pos.Y-height+(y-minY), fontImg.At(x, y))
}
}
}
}
var dp DrawRect
dp.X = minX + pos.X
dp.Y = pos.Y - height
dp.Width = width
dp.Height = height
drawRects = append(drawRects, dp)
index = index + 1
}
return drawRects
// lineColor := randomColor()
// for _, linePos := range drawPos {
// p1 := Point{X: linePos.X, Y: linePos.Y}
// p2 := Point{X: linePos.X + linePos.Width, Y: linePos.Y}
// p3 := Point{X: linePos.X, Y: linePos.Y + linePos.Height}
// p4 := Point{X: linePos.X + linePos.Width, Y: linePos.Y + linePos.Height}
// c.DrawLine(p1, p2, lineColor)
// c.DrawLine(p1, p3, lineColor)
// c.DrawLine(p3, p4, lineColor)
// c.DrawLine(p2, p4, lineColor)
// }
// fmt.Printf("drawpos %+v %s", drawPos, text)
}
//DrawFont 绘制验证码字
func (c *Canvas) DrawFont(fontText string, fontColors []color.Color) (*Palette, *AreaPoint) {
fontSize := c.Config.FontSize
rand.Seed(time.Now().UnixNano())
fntColorIndex := rand.Intn(len(fontColors))
rColor := fontColors[fntColorIndex]
p := []color.Color{
color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0x00},
rColor,
}
canvas := NewPalette(image.Rect(0, 0, fontSize, fontSize), p)
font := defaultFont
if c.Config.Font != nil {
font = c.Config.Font
}
dc := freetype.NewContext()
dc.SetDPI(float64(72))
dc.SetFont(font)
dc.SetClip(canvas.Bounds())
dc.SetDst(canvas)
// 文字大小
dc.SetFontSize(float64(fontSize))
// 文字颜色
fontColor := image.NewUniform(rColor)
dc.SetSrc(fontColor)
// 画文本
pt := freetype.Pt(0, fontSize) // 字出现的位置
_, err := dc.DrawString(string(fontText), pt)
if err != nil {
panic(err)
}
//旋转角度
canvas.Rotate(randomInt(-c.Config.MaxRotate, c.Config.MaxRotate))
ap := c.calcImageSpace(canvas)
return canvas, ap
}
func (c *Canvas) calcImageSpace(pa *Palette) *AreaPoint {
nW := pa.Bounds().Max.X
nH := pa.Bounds().Max.Y
// 计算裁剪的最小及最大的坐标
minX := nW
maxX := 0
minY := nH
maxY := 0
for x := 0; x < nW; x++ {
for y := 0; y < nH; y++ {
co := pa.At(x, y)
if _, _, _, a := co.RGBA(); a > 0 {
if x < minX {
minX = x
}
if x > maxX {
maxX = x
}
if y < minY {
minY = y
}
if y > maxY {
maxY = y
}
}
}
}
minX = int(math.Max(0, float64(minX-2)))
maxX = int(math.Min(float64(nW), float64(maxX+2)))
minY = int(math.Max(0, float64(minY-2)))
maxY = int(math.Min(float64(nH), float64(maxY+2)))
return &AreaPoint{
minX,
maxX,
minY,
maxY,
}
}
func (c *Canvas) randomFontPosition(fontSize int) Point {
minX := 0
minY := fontSize
maxX := c.Width - fontSize
maxY := c.Height
x := randomInt(minX, maxX)
y := randomInt(minY, maxY)
return Point{x, y}
}
//randomInt 返回随机数 [min,max)
func randomInt(min, max int) int {
// rand.Seed(time.Now().UnixNano())
if min >= max || max == 0 {
return max
}
return rand.Intn(max-min) + min
}
func randomColor() color.RGBA {
red := rand.Intn(256)
green := rand.Intn(256)
blue := rand.Intn(256)
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
}
//DrawCircles 随机产生circleCount干扰点
func (c *Canvas) DrawCircles(circleCount int) {
for circleCount > 0 {
cc := randomColor()
x := rand.Intn(c.Width)
y := rand.Intn(c.Height)
r := rand.Intn(3)
fillCircle := rand.Intn(5) > 1
c.DrawCircle(x, y, r, fillCircle, cc)
circleCount = circleCount - 1
}
}
func (c *Canvas) drawCircle8(xc, yc, x, y int, cc color.Color) {
c.Set(xc+x, yc+y, cc)
c.Set(xc-x, yc+y, cc)
c.Set(xc+x, yc-y, cc)
c.Set(xc-x, yc-y, cc)
c.Set(xc+y, yc+x, cc)
c.Set(xc-y, yc+x, cc)
c.Set(xc+y, yc-x, cc)
c.Set(xc-y, yc-x, cc)
}
// DrawCircle 画圆
func (c *Canvas) DrawCircle(xc, yc, r int, fill bool, cc color.Color) {
size := c.Bounds().Size()
if xc+r < 0 || xc-r >= size.X || yc+r < 0 || yc-r >= size.Y {
return
}
x, y, d := 0, r, 3-2*r
for x <= y {
if fill {
for yi := x; yi <= y; yi++ {
c.drawCircle8(xc, yc, x, yi, cc)
}
} else {
c.drawCircle8(xc, yc, x, y, cc)
}
if d < 0 {
d = d + 4*x + 6
} else {
d = d + 4*(x-y) + 10
y--
}
x++
}
}