/
qrcode.go
1832 lines (1703 loc) · 46.3 KB
/
qrcode.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package processor
import (
"errors"
"fmt"
"math"
"sort"
img "github.com/echosoar/imgpro/core"
method "github.com/echosoar/imgpro/method"
)
// QRCode device
func QRCodeProcessor(imgCore *img.Core) {
imgCore.Bind(&img.Processor{
Keys: []string{"qrcode"},
PreConditions: []string{"rgba", "frame", "width", "height"},
Runner: qrCodeRunner,
})
}
func qrCodeRunner(core *img.Core) map[string]img.Value {
rgba := core.Result["rgba"].Frames
width := core.Result["width"].Int
height := core.Result["height"].Int
frame := core.Result["frame"].Int
results := []img.Value{}
for frameIndex := 0; frameIndex < frame; frameIndex++ {
qr := QRCode{
Width: width,
Height: height,
Pixels: rgba[frameIndex].Rgba,
}
qr.Run()
result := qr.GetResult()
results = append(results, result)
}
return map[string]img.Value{
"qrcode": {
Type: img.ValueTypeFrames,
Frames: results,
},
}
}
type QRCode struct {
Width int
Height int
Pixels []img.RGBA
greyPixels []int
regions []*QRCodeRegion
corners []*QRCodeCorner
codeItems []*QRCodeItem
}
type QRCodeRegion struct {
posi img.ValuePosition
size int
cornersIndex int
}
type QRCodeCorner struct {
corners []img.ValuePosition
matrix []float64
center img.ValuePosition
attachToCode int
}
type QRCodeItem struct {
corners []*QRCodeCorner
matrix []float64
version int
size int
Pixels []int
errorCorrectionLevel int
mask int
blocksData []int
result img.Value
success bool
currentReadIndex int
}
type QRDataInfo struct {
alignmentPatterns []int
}
// refs: https://www.thonky.com/qr-code-tutorial/error-correction-table
type QRDataECC struct {
tdc int // Total Number of Data Codewords for this Version and EC Level
ecc int // EC Codewords Per Block
blocks1 int // Number of Blocks in Group 1
dc1 int // Number of Data Codewords in Each of Group 1's Blocks
blocks2 int // Number of Blocks in Group 2
dc2 int // Number of Data Codewords in Each of Group 2's Blocks
}
const (
qrThresholdSDen = 8.0
qrThresholdT = 5.0
)
const (
qrPixelWhite = 0
qrPixelBlack = 1
qrPixelRegion = 2
qrRegionIndexDiff = 2
qrMaxRegion = 256
qrCornerSize = 7
qrMustMatch = true
qrIsBlack = true
qrIsWhite = false
)
const (
qrDataTypeEnd = 0
qrDataTypeNumric = 1
qrDataType8BitByte = 4
qrDataTypeECI = 7
)
var QRDataInfoList = []QRDataInfo{
{},
{alignmentPatterns: []int{}},
{alignmentPatterns: []int{6, 18}},
{alignmentPatterns: []int{6, 22}},
{alignmentPatterns: []int{6, 26}},
{alignmentPatterns: []int{6, 30}},
{alignmentPatterns: []int{6, 34}},
{alignmentPatterns: []int{6, 22, 38}},
{alignmentPatterns: []int{6, 24, 42}},
{alignmentPatterns: []int{6, 26, 46}},
{alignmentPatterns: []int{6, 28, 50}},
{alignmentPatterns: []int{6, 30, 54}},
{alignmentPatterns: []int{6, 32, 58}},
{alignmentPatterns: []int{6, 34, 62}},
{alignmentPatterns: []int{6, 26, 46, 66}},
{alignmentPatterns: []int{6, 26, 48, 70}},
{alignmentPatterns: []int{6, 26, 50, 74}},
{alignmentPatterns: []int{6, 30, 54, 78}},
{alignmentPatterns: []int{6, 30, 56, 82}},
{alignmentPatterns: []int{6, 30, 58, 86}},
{alignmentPatterns: []int{6, 34, 62, 90}},
{alignmentPatterns: []int{6, 28, 50, 72, 94}},
{alignmentPatterns: []int{6, 26, 50, 74, 98}},
{alignmentPatterns: []int{6, 30, 54, 78, 102}},
{alignmentPatterns: []int{6, 28, 54, 80, 106}},
{alignmentPatterns: []int{6, 32, 58, 84, 110}},
{alignmentPatterns: []int{6, 30, 58, 86, 114}},
{alignmentPatterns: []int{6, 34, 62, 90, 118}},
{alignmentPatterns: []int{6, 26, 50, 74, 98, 122}},
{alignmentPatterns: []int{6, 30, 54, 78, 102, 126}},
{alignmentPatterns: []int{6, 26, 52, 78, 104, 130}},
{alignmentPatterns: []int{6, 30, 56, 82, 108, 134}},
{alignmentPatterns: []int{6, 34, 60, 86, 112, 138}},
{alignmentPatterns: []int{6, 30, 58, 86, 114, 142}},
{alignmentPatterns: []int{6, 34, 62, 90, 118, 146}},
{alignmentPatterns: []int{6, 30, 54, 78, 102, 126, 150}},
{alignmentPatterns: []int{6, 24, 50, 76, 102, 128, 154}},
{alignmentPatterns: []int{6, 28, 54, 80, 106, 132, 158}},
{alignmentPatterns: []int{6, 32, 58, 84, 110, 136, 162}},
{alignmentPatterns: []int{6, 26, 54, 82, 110, 138, 166}},
{alignmentPatterns: []int{6, 30, 58, 86, 114, 142, 170}},
}
var qrEccList = [][]QRDataECC{
{},
{ // version 1: L/M/Q/H 四个纠错级别,1/0/3/2
{19, 7, 1, 19, 0, 0},
{16, 10, 1, 16, 0, 0},
{13, 13, 1, 13, 0, 0},
{9, 17, 1, 9, 0, 0},
},
{ // version 2
{34, 10, 1, 34, 0, 0},
{28, 16, 1, 28, 0, 0},
{22, 22, 1, 22, 0, 0},
{16, 28, 1, 16, 0, 0},
},
{ // version 3
{55, 15, 1, 55, 0, 0},
{44, 26, 1, 44, 0, 0},
{34, 18, 2, 17, 0, 0},
{26, 22, 2, 13, 0, 0},
},
{ // version 4
{80, 20, 1, 80, 0, 0},
{64, 18, 2, 32, 0, 0},
{48, 26, 2, 24, 0, 0},
{36, 16, 4, 9, 0, 0},
},
{ // version 5
{108, 26, 1, 108, 0, 0},
{86, 24, 2, 43, 0, 0},
{62, 18, 2, 15, 2, 16},
{46, 22, 2, 11, 2, 12},
},
{ // version 6
{136, 18, 2, 68, 0, 0},
{108, 16, 4, 27, 0, 0},
{76, 24, 4, 19, 0, 0},
{60, 28, 4, 15, 0, 0},
},
{ // version 7
{156, 20, 2, 78, 0, 0},
{124, 18, 4, 31, 0, 0},
{88, 18, 2, 14, 4, 15},
{66, 26, 4, 13, 1, 14},
},
{ // version 8
{194, 24, 2, 97, 0, 0},
{154, 22, 2, 38, 2, 39},
{110, 22, 4, 18, 2, 19},
{86, 26, 4, 14, 2, 15},
},
{ // version 9
{232, 30, 2, 116, 0, 0},
{182, 22, 3, 36, 2, 37},
{132, 20, 4, 16, 4, 17},
{100, 24, 4, 12, 4, 13},
},
{ // version 10
{274, 18, 2, 68, 2, 69},
{216, 26, 4, 43, 1, 44},
{154, 24, 6, 19, 2, 20},
{122, 28, 6, 15, 2, 16},
},
{ // version 11
{324, 20, 4, 81, 0, 0},
{254, 30, 1, 50, 4, 51},
{180, 28, 4, 22, 4, 23},
{140, 24, 3, 12, 8, 13},
},
{ // version 12
{370, 24, 2, 92, 2, 93},
{290, 22, 6, 36, 2, 37},
{206, 26, 4, 20, 6, 21},
{158, 28, 7, 14, 4, 15},
},
{ // version 13
{428, 26, 4, 107, 0, 0},
{334, 22, 8, 37, 1, 38},
{244, 24, 8, 20, 4, 21},
{180, 22, 12, 11, 4, 12},
},
{ // version 14
{461, 30, 3, 115, 1, 116},
{365, 24, 4, 40, 5, 41},
{261, 20, 11, 16, 5, 17},
{197, 24, 11, 12, 5, 13},
},
{ // version 15
{523, 22, 5, 87, 1, 88},
{415, 24, 5, 41, 5, 42},
{295, 30, 5, 24, 7, 25},
{223, 24, 11, 12, 7, 13},
},
{ // version 16
{589, 24, 5, 98, 1, 99},
{453, 28, 7, 45, 3, 46},
{325, 24, 15, 19, 2, 20},
{253, 30, 3, 15, 13, 16},
},
{ // version 17
{647, 28, 1, 107, 5, 108},
{507, 28, 10, 46, 1, 47},
{367, 28, 1, 22, 15, 23},
{283, 28, 2, 14, 17, 15},
},
{ // version 18
{721, 30, 5, 120, 1, 121},
{563, 26, 9, 43, 4, 44},
{397, 28, 17, 22, 1, 23},
{313, 28, 2, 14, 19, 15},
},
{ // version 19
{795, 28, 3, 113, 4, 114},
{627, 26, 3, 44, 11, 45},
{445, 26, 17, 21, 4, 22},
{341, 26, 9, 13, 16, 14},
},
{ // version 20
{861, 28, 3, 107, 5, 108},
{669, 26, 3, 41, 13, 42},
{485, 30, 15, 24, 5, 25},
{385, 28, 15, 15, 10, 16},
},
{ // version 21
{932, 28, 4, 116, 4, 117},
{714, 26, 17, 42, 0, 0},
{512, 28, 17, 22, 6, 23},
{406, 30, 19, 16, 6, 17},
},
{ // version 22
{1006, 28, 2, 111, 7, 112},
{782, 28, 17, 46, 0, 0},
{568, 30, 7, 24, 16, 25},
{442, 24, 34, 13, 0, 0},
},
{ // version 23
{1094, 30, 4, 121, 5, 122},
{860, 28, 4, 47, 14, 48},
{614, 30, 11, 24, 14, 25},
{464, 30, 16, 15, 14, 16},
},
{ // version 24
{1174, 30, 6, 117, 4, 118},
{914, 28, 6, 45, 14, 46},
{664, 30, 11, 24, 16, 25},
{514, 30, 30, 16, 2, 17},
},
{ // version 25
{1276, 26, 8, 106, 4, 107},
{1000, 28, 8, 47, 13, 48},
{718, 30, 7, 24, 22, 25},
{538, 30, 22, 15, 13, 16},
},
{ // version 26
{1370, 28, 10, 114, 2, 115},
{1062, 28, 19, 46, 4, 47},
{754, 28, 28, 22, 6, 23},
{596, 30, 33, 16, 4, 17},
},
{ // version 27
{1468, 30, 8, 122, 4, 123},
{1128, 28, 22, 45, 3, 46},
{808, 30, 8, 23, 26, 24},
{628, 30, 12, 15, 28, 16},
},
{ // version 28
{1531, 30, 3, 117, 10, 118},
{1193, 28, 3, 45, 23, 46},
{871, 30, 4, 24, 31, 25},
{661, 30, 11, 15, 31, 16},
},
{ // version 29
{1631, 30, 7, 116, 7, 117},
{1267, 28, 21, 45, 7, 46},
{911, 30, 1, 23, 37, 24},
{701, 30, 19, 15, 26, 16},
},
{ // version 30
{1735, 30, 5, 115, 10, 116},
{1373, 28, 19, 47, 10, 48},
{985, 30, 15, 24, 25, 25},
{745, 30, 23, 15, 25, 16},
},
{ // version 31
{1843, 30, 13, 115, 3, 116},
{1455, 28, 2, 46, 29, 47},
{1033, 30, 42, 24, 1, 25},
{793, 30, 23, 15, 28, 16},
},
{ // version 32
{1955, 30, 17, 115, 0, 0},
{1541, 28, 10, 46, 23, 47},
{1115, 30, 10, 24, 35, 25},
{845, 30, 19, 15, 35, 16},
},
{ // version 33
{2071, 30, 17, 115, 1, 116},
{1631, 28, 14, 46, 21, 47},
{1171, 30, 29, 24, 19, 25},
{901, 30, 11, 15, 46, 16},
},
{ // version 34
{2191, 30, 13, 115, 6, 116},
{1725, 28, 14, 46, 23, 47},
{1231, 30, 44, 24, 7, 25},
{961, 30, 59, 16, 1, 17},
},
{ // version 35
{2306, 30, 12, 121, 7, 122},
{1812, 28, 12, 47, 26, 48},
{1286, 30, 39, 24, 14, 25},
{986, 30, 22, 15, 41, 16},
},
{ // version 36
{2434, 30, 6, 121, 14, 122},
{1914, 28, 6, 47, 34, 48},
{1354, 30, 46, 24, 10, 25},
{1054, 30, 2, 15, 64, 16},
},
{ // version 37
{2566, 30, 17, 122, 4, 123},
{1992, 28, 29, 46, 14, 47},
{1426, 30, 49, 24, 10, 25},
{1096, 30, 24, 15, 46, 16},
},
{ // version 38
{2702, 30, 4, 122, 18, 123},
{2102, 28, 13, 46, 32, 47},
{1502, 30, 48, 24, 14, 25},
{1142, 30, 42, 15, 32, 16},
},
{ // version 39
{2812, 30, 20, 117, 4, 118},
{2216, 28, 40, 47, 7, 48},
{1582, 30, 43, 24, 22, 25},
{1222, 30, 10, 15, 67, 16},
},
{ // version 40
{2956, 30, 19, 118, 6, 119},
{2334, 28, 18, 47, 31, 48},
{1666, 30, 34, 24, 34, 25},
},
}
func (qr *QRCode) Run() {
qr.grayscale()
qr.binarization()
qr.findCorners()
qr.polymerizate()
}
// 灰度化
func (qr *QRCode) grayscale() {
greyPixels := make([]int, len(qr.Pixels))
for index, rgba := range qr.Pixels {
greyPixels[index] = method.RGBAToGrey(rgba)
}
qr.greyPixels = greyPixels
}
// 二值化
func (qr *QRCode) binarization() {
avgW := 0.0
avgU := 0.0
thresholds := float64(qr.Width) / qrThresholdSDen
if thresholds < 1 {
thresholds = 1
}
pi := (thresholds - 1) / thresholds
qrThresholdTDiff100 := 100 - qrThresholdT
thresholds200 := 200 * thresholds
qtPi := qrThresholdTDiff100 / thresholds200
for line := 0; line < qr.Height; line++ {
lineStartPixelIndex := qr.Width * line
// rowGreyPixels := qr.greyPixels[lineStartPixelIndex : lineStartPixelIndex+qr.Width]
rowAverage := make([]int, qr.Width)
for row := 0; row < qr.Width; row++ {
var w, u int
if line&1 == 1 {
w = row
u = qr.Width - 1 - row
} else {
w = qr.Width - 1 - row
u = row
}
avgW = avgW*pi + float64(qr.greyPixels[w+lineStartPixelIndex])
avgU = avgU*pi + float64(qr.greyPixels[u+lineStartPixelIndex])
rowAverage[w] += int(avgW)
rowAverage[u] += int(avgU)
}
for row := 0; row < qr.Width; row++ {
pixelIndex := row + lineStartPixelIndex
if qr.greyPixels[pixelIndex] < int(float64(rowAverage[row])*qtPi) {
qr.greyPixels[pixelIndex] = qrPixelBlack
} else {
qr.greyPixels[pixelIndex] = qrPixelWhite
}
}
}
}
// 寻找用于定位的角
func (qr *QRCode) findCorners() {
for line := 0; line < qr.Height; line++ {
lineStartPixelIndex := qr.Width * line
rowGreyPixels := qr.greyPixels[lineStartPixelIndex : lineStartPixelIndex+qr.Width]
lastColor := 0
runLength := 0
runCount := 0
pb := make([]int, 5)
// 宽度按照 1:1:3:1:1 的规模,哪怕倾斜之后也是如此
check := [5]int{1, 1, 3, 1, 1}
for row := 0; row < qr.Width; row++ {
color := 0
if rowGreyPixels[row] > 0 {
color = 1
}
// 避免定位点在最边上的情况
if row == qr.Width-1 {
color = 0
}
if row > 0 && color != lastColor {
for i := 0; i < 4; i++ {
pb[i] = pb[i+1]
}
pb[4] = runLength
runLength = 0
runCount++
if color == 0 && runCount >= 5 {
var avg, err int
ok := true
avg = (pb[0] + pb[1] + pb[3] + pb[4]) / 4
err = avg * 3 / 4
for i := 0; i < 5; i++ {
if pb[i] < check[i]*avg-err || pb[i] > check[i]*avg+err {
ok = false
break
}
}
if ok {
qr.checkIsCorner(line, row, pb)
}
}
}
runLength++
lastColor = color
}
}
}
func (qr *QRCode) checkIsCorner(line, row int, pb []int) {
regionRightLineIndex := qr.regionCode(line, row-pb[4])
regionCenterIndex := qr.regionCode(line, row-pb[4]-pb[3]-pb[2])
regionLeftLineIndex := qr.regionCode(line, row-pb[4]-pb[3]-pb[2]-pb[1]-pb[0])
if regionRightLineIndex < 0 || regionCenterIndex < 0 || regionLeftLineIndex < 0 || regionLeftLineIndex != regionRightLineIndex || regionLeftLineIndex == regionCenterIndex {
return
}
centerRegion := qr.getRegionByRegionIndex(regionCenterIndex)
lineRegion := qr.getRegionByRegionIndex(regionRightLineIndex)
// 已经放在某一个 corner 之中了
if centerRegion.cornersIndex >= 0 || lineRegion.cornersIndex >= 0 {
return
}
ratio := centerRegion.size * 100 / lineRegion.size
if ratio < 10 || ratio > 70 {
return
}
qr.newCorner(regionRightLineIndex, lineRegion, centerRegion)
}
func (qr *QRCode) regionCode(line, row int) int {
if row < 0 || line < 0 || row >= qr.Width || line >= qr.Height {
return -1
}
index := line*qr.Width + row
pixel := int(qr.greyPixels[index])
if pixel >= 2 {
return pixel
}
if pixel == qrPixelWhite {
return -1
}
if len(qr.regions) >= qrMaxRegion {
return -1
}
region := &QRCodeRegion{}
qr.regions = append(qr.regions, region)
regionIndex := len(qr.regions) - 1 + qrRegionIndexDiff // 避免与像素重合
region.posi.X = row
region.posi.Y = line
region.size = 0
region.cornersIndex = -1
qr.fillRegion(row, line, pixel, regionIndex, region, 0)
return regionIndex
}
func (qr *QRCode) fillRegion(row, line, pixel, regionIndex int, region *QRCodeRegion, depth int) {
left := row
right := row
rowGreyPixels := qr.greyPixels[line*qr.Width : line*qr.Width+qr.Width]
if depth >= 1024 {
return
}
// 如果左边的像素和自己一样,就向左移动
for left > 0 && int(rowGreyPixels[left-1]) == pixel {
left--
}
// 如果右边的像素和自己一样,就向左移动
for right < qr.Width-1 && int(rowGreyPixels[right+1]) == pixel {
right++
}
changedPixel := 0
// 填充
for i := left; i <= right; i++ {
newPixel := regionIndex
if rowGreyPixels[i] != newPixel {
rowGreyPixels[i] = regionIndex
changedPixel++
}
}
if changedPixel <= 0 {
return
}
region.size += changedPixel
// 往上面一行、下面一行进行检测
lineAutoCheck := []int{-1, 1}
for _, lineDiff := range lineAutoCheck {
newLine := line + lineDiff
if newLine <= 0 || newLine >= qr.Height {
continue
}
rowGreyPixels := qr.greyPixels[newLine*qr.Width : newLine*qr.Width+qr.Width]
for i := left; i <= right; i++ {
if int(rowGreyPixels[i]) == pixel {
qr.fillRegion(i, newLine, pixel, regionIndex, region, depth+1)
}
}
}
}
func (qr *QRCode) getRegionByRegionIndex(index int) *QRCodeRegion {
return qr.regions[index-qrRegionIndexDiff]
}
func (qr *QRCode) newCorner(lineRegionIndex int, lineRegion *QRCodeRegion, centerRegion *QRCodeRegion) {
corner := &QRCodeCorner{
attachToCode: -1,
}
qr.corners = append(qr.corners, corner)
cornerIndex := len(qr.corners)
lineRegion.cornersIndex = cornerIndex
centerRegion.cornersIndex = cornerIndex
lineRegionIndexUint := lineRegionIndex
// 寻找四个顶点
allPixelPositions := make([]img.ValuePosition, 0)
firstCornerMaxDistance := 0
firstCornerIndex := 0
for line := 0; line < qr.Height; line++ {
for row := 0; row < qr.Width; row++ {
index := line*qr.Width + row
if qr.greyPixels[index] == lineRegionIndexUint {
allPixelPositions = append(allPixelPositions, img.ValuePosition{
X: row,
Y: line,
})
pixelIndex := len(allPixelPositions) - 1
distance := int(math.Pow(float64(row-lineRegion.posi.X), 2.0) + math.Pow(float64(line-lineRegion.posi.Y), 2.0))
if distance > firstCornerMaxDistance {
firstCornerMaxDistance = distance
firstCornerIndex = pixelIndex
}
}
}
}
firstPixelPosi := allPixelPositions[firstCornerIndex]
secondCornerMaxDistance := 0
secondCornerIndex := 0
for index, posi := range allPixelPositions {
distance := int(math.Pow(float64(posi.X-firstPixelPosi.X), 2.0) + math.Pow(float64(posi.Y-firstPixelPosi.Y), 2.0))
if distance > secondCornerMaxDistance {
secondCornerMaxDistance = distance
secondCornerIndex = index
}
}
secondPixelPosi := allPixelPositions[secondCornerIndex]
thirdCornerMaxDistance := 0
thirdCornerIndex := 0
for index, posi := range allPixelPositions {
distance := int(math.Sqrt(math.Pow(float64(posi.X-firstPixelPosi.X), 2.0)+math.Pow(float64(posi.Y-firstPixelPosi.Y), 2.0)) + math.Sqrt(math.Pow(float64(posi.X-secondPixelPosi.X), 2.0)+math.Pow(float64(posi.Y-secondPixelPosi.Y), 2.0)))
if distance > thirdCornerMaxDistance {
thirdCornerMaxDistance = distance
thirdCornerIndex = index
}
}
thirdPixelPosi := allPixelPositions[thirdCornerIndex]
lineInfo := method.PointsToLine(float64(firstPixelPosi.X), float64(firstPixelPosi.Y), float64(secondPixelPosi.X), float64(secondPixelPosi.Y))
if lineInfo.IsX {
secondPixelPosi, thirdPixelPosi = thirdPixelPosi, secondPixelPosi
}
k, b := lineInfo.K, lineInfo.B
thirdIsUnderLine := (float64(thirdPixelPosi.Y) - k*float64(thirdPixelPosi.X)) > b
fouthCornerMaxDistance := 0
fouthCornerIndex := 0
for index, posi := range allPixelPositions {
posiIsUnderLine := (float64(posi.Y) - k*float64(posi.X)) > b
if thirdIsUnderLine == posiIsUnderLine {
continue
}
// distance := math.Abs(k*float64(posi.X)-float64(posi.Y)+b) / ab
distance := int(math.Sqrt(math.Pow(float64(posi.X-firstPixelPosi.X), 2.0)+math.Pow(float64(posi.Y-firstPixelPosi.Y), 2.0)) + math.Sqrt(math.Pow(float64(posi.X-secondPixelPosi.X), 2.0)+math.Pow(float64(posi.Y-secondPixelPosi.Y), 2.0)))
if distance > fouthCornerMaxDistance {
fouthCornerMaxDistance = distance
fouthCornerIndex = index
}
}
fouthCornerPosi := allPixelPositions[fouthCornerIndex]
corners := []img.ValuePosition{
firstPixelPosi,
secondPixelPosi,
thirdPixelPosi,
fouthCornerPosi,
}
// 按照顺时针方向,左上角为第一个
sort.SliceStable(corners, func(i, j int) bool {
return corners[j].Y > corners[i].Y
})
if corners[0].X > corners[1].X {
corners[1], corners[0] = corners[0], corners[1]
}
if corners[3].X > corners[2].X {
corners[3], corners[2] = corners[2], corners[3]
}
corner.corners = corners
corner.matrix = method.PerspectiveMap(&corner.corners, qrCornerSize, qrCornerSize)
corner.center = method.PerspectiveTransform(&corner.matrix, qrCornerSize/2, qrCornerSize/2)
}
// 聚合多个 corner,确认一张二维码
// 一个 corner 有横向和纵向相对应的 corner的时候,才有可能是一个二维码
func (qr *QRCode) polymerizate() {
for index, corner := range qr.corners {
if corner.attachToCode >= 0 {
continue
}
horizontalOppositeCorners := make([]*QRCodeCorner, 0)
horizontalOppositeCornerDistance := make([]float64, 0)
verticalOppositeCorners := make([]*QRCodeCorner, 0)
verticalOppositeCornerDistance := make([]float64, 0)
for testIndex, testCorner := range qr.corners {
if testIndex == index || testCorner.attachToCode >= 0 {
continue
}
newPosi := method.PerspectiveTransformBack(&corner.matrix, testCorner.center)
newPosi.X = int(math.Abs(float64(newPosi.X) - qrCornerSize/2))
newPosi.Y = int(math.Abs(float64(newPosi.Y) - qrCornerSize/2))
if float64(newPosi.X) < 0.2*float64(newPosi.Y) {
horizontalOppositeCorners = append(horizontalOppositeCorners, testCorner)
horizontalOppositeCornerDistance = append(horizontalOppositeCornerDistance, float64(newPosi.Y))
}
if float64(newPosi.Y) < 0.2*float64(newPosi.X) {
verticalOppositeCorners = append(verticalOppositeCorners, testCorner)
verticalOppositeCornerDistance = append(verticalOppositeCornerDistance, float64(newPosi.X))
}
}
if len(horizontalOppositeCorners) == 0 || len(verticalOppositeCorners) == 0 {
continue
}
bestScore := 0.0
bestH := -1
bestV := -1
for hIndex := range horizontalOppositeCorners {
for vIndex := range verticalOppositeCorners {
score := math.Abs(1.0 - horizontalOppositeCornerDistance[hIndex]/verticalOppositeCornerDistance[vIndex])
if score > 2.5 {
continue
}
if bestH < 0 || score < bestScore {
bestH = hIndex
bestV = vIndex
bestScore = score
}
}
}
if bestH < 0 || bestV < 0 {
continue
}
qr.polymerizateExec(corner, horizontalOppositeCorners[bestH], verticalOppositeCorners[bestV])
}
}
// 将3个corner处理成一个二维码
func (qr *QRCode) polymerizateExec(centerCorner *QRCodeCorner, hCorner *QRCodeCorner, vCorner *QRCodeCorner) {
// 转换顺序,顺时针
// kVH := float64(vCorner.center.Y-hCorner.center.Y) / float64(vCorner.center.X-hCorner.center.X)
// kCH := float64(centerCorner.center.Y-hCorner.center.Y) / float64(centerCorner.center.X-hCorner.center.X)
// 避免除以零,转换上述公式
if float64(vCorner.center.Y-hCorner.center.Y)*float64(centerCorner.center.X-hCorner.center.X) < float64(centerCorner.center.Y-hCorner.center.Y)*float64(vCorner.center.X-hCorner.center.X) {
hCorner, vCorner = vCorner, hCorner
}
// 横向的左边与纵向的 上边交点就是第四个点,为什么不能用横向的右边与纵向的下边,是因为避免外围影响
isExistsPoint, pointInfo := method.LineIntesect(
method.PointsToLine(float64(hCorner.corners[0].X), float64(hCorner.corners[0].Y), float64(hCorner.corners[1].X), float64(hCorner.corners[1].Y)),
method.PointsToLine(float64(vCorner.corners[0].X), float64(vCorner.corners[0].Y), float64(vCorner.corners[3].X), float64(vCorner.corners[3].Y)),
)
if !isExistsPoint {
return
}
corners := []*QRCodeCorner{hCorner, centerCorner, vCorner}
qrCodeItem := &QRCodeItem{
corners: corners,
}
qr.codeItems = append(qr.codeItems, qrCodeItem)
codeItemIndex := len(qr.codeItems) - 1
centerCorner.attachToCode = codeItemIndex
hCorner.attachToCode = codeItemIndex
vCorner.attachToCode = codeItemIndex
// 获取二维码版本
qrCodeItem.version = qr.getVersion(qrCodeItem)
qrCodeItem.size = qrCodeItem.version*4 + 17
qrCodeItem.Pixels = make([]int, qrCodeItem.size*qrCodeItem.size)
// 超过7版本,就有 alignPattern 了
if qrCodeItem.version >= 7 {
areaRange := method.PointDistanceInt(centerCorner.corners[0].X, centerCorner.corners[0].Y, hCorner.corners[3].X, hCorner.corners[3].Y) * 3 / qrCodeItem.size
checkPointInfo:
for i := 0; i < areaRange; i++ {
for j := 0; j < areaRange; j++ {
// changeList := []int{i}
// if i != 0 {
// changeList = []int{-i, i}
// }
newPosi := img.ValuePosition{X: pointInfo.X + i, Y: pointInfo.Y + j}
posiIndex := newPosi.X + newPosi.Y*qr.Width
if qr.greyPixels[posiIndex] == 0 {
continue
}
matched, newX, newY := qr.check111(qrCodeItem, newPosi.X, newPosi.Y)
if matched {
pointInfo.X = newX
pointInfo.Y = newY
break checkPointInfo
}
}
}
}
// 获取图像透视矩阵
posies := []img.ValuePosition{corners[1].corners[0], corners[2].corners[0], pointInfo, corners[0].corners[0]}
matrix := method.PerspectiveMap(&posies, float64(qrCodeItem.size)-float64(qrCornerSize), float64(qrCodeItem.size)-float64(qrCornerSize))
qrCodeItem.matrix = matrix
qr.autoAdjustmentMatrix(qrCodeItem, &matrix)
// 获取图像透视像素点
for line := 0; line < qrCodeItem.size; line++ {
for row := 0; row < qrCodeItem.size; row++ {
pixelIndex := line*qrCodeItem.size + row
qrCodeItem.Pixels[pixelIndex] = qrPixelWhite
// 之所以加0.5是为了获取到像素中心
point := method.PerspectiveTransform(&qrCodeItem.matrix, float64(row)+0.5, float64(line)+0.5)
// default isWhite
if point.Y >= 0 && point.Y < qr.Height && point.X >= 0 && point.X < qr.Width {
index := point.Y*qr.Width + point.X
if qr.greyPixels[index] != 0 {
qrCodeItem.Pixels[pixelIndex] = qrPixelBlack
}
}
}
}
qrCodeItem.decode()
}
// 检测是否是 1:1:1
func (qr *QRCode) check111(qrCodeItem *QRCodeItem, x, y int) (bool, int, int) {
xi := x
xj := x
yi := y
yj := y
colorChangeXIndex := make([]int, 0)
colorChangeYIndex := make([]int, 0)
lastColor := qrIsBlack
colorChangedCount := 0
for xi >= 0 {
xi--
index := xi + y*qr.Width
curColor := qrIsBlack
if qr.greyPixels[index] == 0 {
curColor = qrIsWhite
}
if curColor != lastColor {
lastColor = curColor
colorChangedCount++
colorChangeXIndex = method.ConcatArray([]int{xi}, colorChangeXIndex)
if colorChangedCount == 2 {
break
}
}
}
lastColor = qrIsBlack
colorChangedCount = 0
for xj < qr.Width {
xj++
index := xj + y*qr.Width
curColor := qrIsBlack
if qr.greyPixels[index] == 0 {
curColor = qrIsWhite
}
if curColor != lastColor {
lastColor = curColor
colorChangedCount++
colorChangeXIndex = append(colorChangeXIndex, xj)
if colorChangedCount == 2 {
break
}
}
}
diff := []float64{
float64(colorChangeXIndex[1] - colorChangeXIndex[0]),
float64(colorChangeXIndex[2] - colorChangeXIndex[1]),
float64(colorChangeXIndex[3] - colorChangeXIndex[2]),
}
if math.Abs(diff[1]/diff[0]-1) > 0.2 || math.Abs(diff[2]/diff[1]-1) > 0.2 {
return false, 0, 0
}
lastColor = qrIsBlack
colorChangedCount = 0
for yi >= 0 {
yi--
index := x + yi*qr.Width
curColor := qrIsBlack
if qr.greyPixels[index] == 0 {
curColor = qrIsWhite
}
if curColor != lastColor {
lastColor = curColor
colorChangedCount++
colorChangeYIndex = method.ConcatArray([]int{yi}, colorChangeYIndex)
if colorChangedCount == 2 {
break
}
}
}
lastColor = qrIsBlack
colorChangedCount = 0
for yj < qr.Height {
yj++
index := x + yj*qr.Width
curColor := qrIsBlack
if qr.greyPixels[index] == 0 {
curColor = qrIsWhite
}
if curColor != lastColor {
lastColor = curColor
colorChangedCount++
colorChangeYIndex = append(colorChangeYIndex, yj)
if colorChangedCount == 2 {
break
}
}
}
diffY := []float64{
float64(colorChangeYIndex[1] - colorChangeYIndex[0]),
float64(colorChangeYIndex[2] - colorChangeYIndex[1]),
float64(colorChangeYIndex[3] - colorChangeYIndex[2]),
}
if math.Abs(diffY[1]/diffY[0]-1) > 0.2 || math.Abs(diffY[2]/diffY[1]-1) > 0.2 {
return false, 0, 0
}
xRange := (colorChangeXIndex[3] - colorChangeXIndex[0]) / 2
yRange := (colorChangeYIndex[3] - colorChangeYIndex[0]) / 2
startX := x - xRange
endX := x + xRange
if startX < 0 {
startX = 0
}
if endX > qr.Width {
endX = qr.Width
}
startY := y - yRange
endY := y + yRange
if startY < 0 {
startY = 0
}
if endY > qr.Height {
endY = qr.Height
}
pointMap := make([]int, (endX-startX)*(endY-startY))
distance, newX, newY := qr.check111Black(qrCodeItem, &pointMap, startX, endX, startY, endY, x, y)
if distance > 0 {