qr.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package qr encodes QR codes.
  6. */
  7. package qr
  8. import (
  9. "errors"
  10. "image"
  11. "image/color"
  12. "github.com/SKatiyar/qr/coding"
  13. )
  14. // A Level denotes a QR error correction level.
  15. // From least to most tolerant of errors, they are L, M, Q, H.
  16. type Level int
  17. const (
  18. L Level = iota // 20% redundant
  19. M // 38% redundant
  20. Q // 55% redundant
  21. H // 65% redundant
  22. )
  23. // Encode returns an encoding of text at the given error correction level.
  24. func Encode(text string, level Level) (*Code, error) {
  25. // Pick data encoding, smallest first.
  26. // We could split the string and use different encodings
  27. // but that seems like overkill for now.
  28. var enc coding.Encoding
  29. switch {
  30. case coding.Num(text).Check() == nil:
  31. enc = coding.Num(text)
  32. case coding.Alpha(text).Check() == nil:
  33. enc = coding.Alpha(text)
  34. default:
  35. enc = coding.String(text)
  36. }
  37. // Pick size.
  38. l := coding.Level(level)
  39. var v coding.Version
  40. for v = coding.MinVersion; ; v++ {
  41. if v > coding.MaxVersion {
  42. return nil, errors.New("text too long to encode as QR")
  43. }
  44. if enc.Bits(v) <= v.DataBytes(l)*8 {
  45. break
  46. }
  47. }
  48. // Build and execute plan.
  49. p, err := coding.NewPlan(v, l, 0)
  50. if err != nil {
  51. return nil, err
  52. }
  53. cc, err := p.Encode(enc)
  54. if err != nil {
  55. return nil, err
  56. }
  57. // TODO: Pick appropriate mask.
  58. return &Code{cc.Bitmap, cc.Size, cc.Stride, 8}, nil
  59. }
  60. // A Code is a square pixel grid.
  61. // It implements image.Image and direct PNG encoding.
  62. type Code struct {
  63. Bitmap []byte // 1 is black, 0 is white
  64. Size int // number of pixels on a side
  65. Stride int // number of bytes per row
  66. Scale int // number of image pixels per QR pixel
  67. }
  68. // Black returns true if the pixel at (x,y) is black.
  69. func (c *Code) Black(x, y int) bool {
  70. return 0 <= x && x < c.Size && 0 <= y && y < c.Size &&
  71. c.Bitmap[y*c.Stride+x/8]&(1<<uint(7-x&7)) != 0
  72. }
  73. // Image returns an Image displaying the code.
  74. func (c *Code) Image() image.Image {
  75. return &codeImage{c}
  76. }
  77. // codeImage implements image.Image
  78. type codeImage struct {
  79. *Code
  80. }
  81. var (
  82. whiteColor color.Color = color.Gray{0xFF}
  83. blackColor color.Color = color.Gray{0x00}
  84. )
  85. func (c *codeImage) Bounds() image.Rectangle {
  86. d := (c.Size + 8) * c.Scale
  87. return image.Rect(0, 0, d, d)
  88. }
  89. func (c *codeImage) At(x, y int) color.Color {
  90. if c.Black(x, y) {
  91. return blackColor
  92. }
  93. return whiteColor
  94. }
  95. func (c *codeImage) ColorModel() color.Model {
  96. return color.GrayModel
  97. }