| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package libqrencode |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import "C" |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"image" |
| 20 |
"image/color" |
| 21 |
"unsafe" |
| 22 |
) |
| 23 |
|
| 24 |
type Version int |
| 25 |
|
| 26 |
type Mode int |
| 27 |
|
| 28 |
const ( |
| 29 |
Numeric Mode = C.QR_MODE_NUM |
| 30 |
Alphanumeric Mode = C.QR_MODE_AN |
| 31 |
EightBit Mode = C.QR_MODE_8 |
| 32 |
) |
| 33 |
|
| 34 |
type Level int |
| 35 |
|
| 36 |
const ( |
| 37 |
L Level = C.QR_ECLEVEL_L |
| 38 |
M Level = C.QR_ECLEVEL_M |
| 39 |
Q Level = C.QR_ECLEVEL_Q |
| 40 |
H Level = C.QR_ECLEVEL_H |
| 41 |
) |
| 42 |
|
| 43 |
type Pixel int |
| 44 |
|
| 45 |
const ( |
| 46 |
Black Pixel = 1 << iota |
| 47 |
DataECC |
| 48 |
Format |
| 49 |
PVersion |
| 50 |
Timing |
| 51 |
Alignment |
| 52 |
Finder |
| 53 |
NonData |
| 54 |
) |
| 55 |
|
| 56 |
type Code struct { |
| 57 |
Version int |
| 58 |
Width int |
| 59 |
Pixel [][]Pixel |
| 60 |
Scale int |
| 61 |
} |
| 62 |
|
| 63 |
func (*Code) ColorModel() color.Model { |
| 64 |
return color.RGBAModel |
| 65 |
} |
| 66 |
|
| 67 |
func (c *Code) Bounds() image.Rectangle { |
| 68 |
d := (c.Width + 8) * c.Scale |
| 69 |
return image.Rect(0, 0, d, d) |
| 70 |
} |
| 71 |
|
| 72 |
var ( |
| 73 |
white color.Color = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} |
| 74 |
black color.Color = color.RGBA{0x00, 0x00, 0x00, 0xFF} |
| 75 |
blue color.Color = color.RGBA{0x00, 0x00, 0x80, 0xFF} |
| 76 |
red color.Color = color.RGBA{0xFF, 0x40, 0x40, 0xFF} |
| 77 |
yellow color.Color = color.RGBA{0xFF, 0xFF, 0x00, 0xFF} |
| 78 |
gray color.Color = color.RGBA{0x80, 0x80, 0x80, 0xFF} |
| 79 |
green color.Color = color.RGBA{0x22, 0x8B, 0x22, 0xFF} |
| 80 |
) |
| 81 |
|
| 82 |
func (c *Code) At(x, y int) color.Color { |
| 83 |
x = x/c.Scale - 4 |
| 84 |
y = y/c.Scale - 4 |
| 85 |
if 0 <= x && x < c.Width && 0 <= y && y < c.Width { |
| 86 |
switch p := c.Pixel[y][x]; { |
| 87 |
case p&Black == 0: |
| 88 |
|
| 89 |
case p&DataECC != 0: |
| 90 |
return black |
| 91 |
case p&Format != 0: |
| 92 |
return blue |
| 93 |
case p&PVersion != 0: |
| 94 |
return red |
| 95 |
case p&Timing != 0: |
| 96 |
return yellow |
| 97 |
case p&Alignment != 0: |
| 98 |
return gray |
| 99 |
case p&Finder != 0: |
| 100 |
return green |
| 101 |
} |
| 102 |
} |
| 103 |
return white |
| 104 |
} |
| 105 |
|
| 106 |
type Chunk struct { |
| 107 |
Mode Mode |
| 108 |
Text string |
| 109 |
} |
| 110 |
|
| 111 |
func Encode(version Version, level Level, mode Mode, text string) (*Code, error) { |
| 112 |
return EncodeChunk(version, level, Chunk{mode, text}) |
| 113 |
} |
| 114 |
|
| 115 |
func EncodeChunk(version Version, level Level, chunk ...Chunk) (*Code, error) { |
| 116 |
qi, err := C.QRinput_new2(C.int(version), C.QRecLevel(level)) |
| 117 |
if qi == nil { |
| 118 |
return nil, fmt.Errorf("QRinput_new2: %v", err) |
| 119 |
} |
| 120 |
defer C.QRinput_free(qi) |
| 121 |
for _, ch := range chunk { |
| 122 |
data := []byte(ch.Text) |
| 123 |
n, err := C.QRinput_append(qi, C.QRencodeMode(ch.Mode), C.int(len(data)), (*C.uchar)(&data[0])) |
| 124 |
if n < 0 { |
| 125 |
return nil, fmt.Errorf("QRinput_append %q: %v", data, err) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
qc, err := C.QRcode_encodeInput(qi) |
| 130 |
if qc == nil { |
| 131 |
return nil, fmt.Errorf("QRinput_encodeInput: %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
c := &Code{ |
| 135 |
Version: int(qc.version), |
| 136 |
Width: int(qc.width), |
| 137 |
Scale: 16, |
| 138 |
} |
| 139 |
pix := make([]Pixel, c.Width*c.Width) |
| 140 |
cdat := (*[1000 * 1000]byte)(unsafe.Pointer(qc.data))[:len(pix)] |
| 141 |
for i := range pix { |
| 142 |
pix[i] = Pixel(cdat[i]) |
| 143 |
} |
| 144 |
c.Pixel = make([][]Pixel, c.Width) |
| 145 |
for i := range c.Pixel { |
| 146 |
c.Pixel[i] = pix[i*c.Width : (i+1)*c.Width] |
| 147 |
} |
| 148 |
return c, nil |
| 149 |
} |