qrcode.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package qr
  2. import (
  3. "image"
  4. "image/color"
  5. "math"
  6. "github.com/boombuler/barcode"
  7. "github.com/boombuler/barcode/utils"
  8. )
  9. type qrcode struct {
  10. dimension int
  11. data *utils.BitList
  12. content string
  13. }
  14. func (qr *qrcode) Content() string {
  15. return qr.content
  16. }
  17. func (qr *qrcode) Metadata() barcode.Metadata {
  18. return barcode.Metadata{barcode.TypeQR, 2}
  19. }
  20. func (qr *qrcode) ColorModel() color.Model {
  21. return color.Gray16Model
  22. }
  23. func (qr *qrcode) Bounds() image.Rectangle {
  24. return image.Rect(0, 0, qr.dimension, qr.dimension)
  25. }
  26. func (qr *qrcode) At(x, y int) color.Color {
  27. if qr.Get(x, y) {
  28. return color.Black
  29. }
  30. return color.White
  31. }
  32. func (qr *qrcode) Get(x, y int) bool {
  33. return qr.data.GetBit(x*qr.dimension + y)
  34. }
  35. func (qr *qrcode) Set(x, y int, val bool) {
  36. qr.data.SetBit(x*qr.dimension+y, val)
  37. }
  38. func (qr *qrcode) calcPenalty() uint {
  39. return qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
  40. }
  41. func (qr *qrcode) calcPenaltyRule1() uint {
  42. var result uint
  43. for x := 0; x < qr.dimension; x++ {
  44. checkForX := false
  45. var cntX uint
  46. checkForY := false
  47. var cntY uint
  48. for y := 0; y < qr.dimension; y++ {
  49. if qr.Get(x, y) == checkForX {
  50. cntX++
  51. } else {
  52. checkForX = !checkForX
  53. if cntX >= 5 {
  54. result += cntX - 2
  55. }
  56. cntX = 1
  57. }
  58. if qr.Get(y, x) == checkForY {
  59. cntY++
  60. } else {
  61. checkForY = !checkForY
  62. if cntY >= 5 {
  63. result += cntY - 2
  64. }
  65. cntY = 1
  66. }
  67. }
  68. if cntX >= 5 {
  69. result += cntX - 2
  70. }
  71. if cntY >= 5 {
  72. result += cntY - 2
  73. }
  74. }
  75. return result
  76. }
  77. func (qr *qrcode) calcPenaltyRule2() uint {
  78. var result uint
  79. for x := 0; x < qr.dimension-1; x++ {
  80. for y := 0; y < qr.dimension-1; y++ {
  81. check := qr.Get(x, y)
  82. if qr.Get(x, y+1) == check && qr.Get(x+1, y) == check && qr.Get(x+1, y+1) == check {
  83. result += 3
  84. }
  85. }
  86. }
  87. return result
  88. }
  89. func (qr *qrcode) calcPenaltyRule3() uint {
  90. pattern1 := []bool{true, false, true, true, true, false, true, false, false, false, false}
  91. pattern2 := []bool{false, false, false, false, true, false, true, true, true, false, true}
  92. var result uint
  93. for x := 0; x <= qr.dimension-len(pattern1); x++ {
  94. for y := 0; y < qr.dimension; y++ {
  95. pattern1XFound := true
  96. pattern2XFound := true
  97. pattern1YFound := true
  98. pattern2YFound := true
  99. for i := 0; i < len(pattern1); i++ {
  100. iv := qr.Get(x+i, y)
  101. if iv != pattern1[i] {
  102. pattern1XFound = false
  103. }
  104. if iv != pattern2[i] {
  105. pattern2XFound = false
  106. }
  107. iv = qr.Get(y, x+i)
  108. if iv != pattern1[i] {
  109. pattern1YFound = false
  110. }
  111. if iv != pattern2[i] {
  112. pattern2YFound = false
  113. }
  114. }
  115. if pattern1XFound || pattern2XFound {
  116. result += 40
  117. }
  118. if pattern1YFound || pattern2YFound {
  119. result += 40
  120. }
  121. }
  122. }
  123. return result
  124. }
  125. func (qr *qrcode) calcPenaltyRule4() uint {
  126. totalNum := qr.data.Len()
  127. trueCnt := 0
  128. for i := 0; i < totalNum; i++ {
  129. if qr.data.GetBit(i) {
  130. trueCnt++
  131. }
  132. }
  133. percDark := float64(trueCnt) * 100 / float64(totalNum)
  134. floor := math.Abs(math.Floor(percDark/5) - 10)
  135. ceil := math.Abs(math.Ceil(percDark/5) - 10)
  136. return uint(math.Min(floor, ceil) * 10)
  137. }
  138. func newBarcode(dim int) *qrcode {
  139. res := new(qrcode)
  140. res.dimension = dim
  141. res.data = utils.NewBitList(dim * dim)
  142. return res
  143. }