-
Notifications
You must be signed in to change notification settings - Fork 3
/
microqr.go
100 lines (81 loc) · 1.65 KB
/
microqr.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
package microqr
//go:generate go run genbch/main.go
import "strconv"
type QRCode struct {
Version Version
Level Level
Mask Mask
Segments []Segment
}
// Version is a version of microQR code.
type Version int
// Level is a error correction level.
type Level int
const (
levelMin Level = 0
LevelCheck Level = 0b10
LevelL Level = 0b01
LevelM Level = 0b00
LevelQ Level = 0b11
levelMax Level = 4
)
// IsValid returns true if the level is valid.
func (lv Level) IsValid() bool {
return levelMin <= lv && lv < levelMax
}
func (lv Level) String() string {
switch lv {
case LevelCheck:
return "Check"
case LevelL:
return "L"
case LevelM:
return "M"
case LevelQ:
return "Q"
}
return "invalid(" + strconv.Itoa(int(lv)) + ")"
}
// Mask is a mask pattern.
type Mask int
const (
Mask0 Mask = iota
Mask1
Mask2
Mask3
maskMax
MaskAuto Mask = -1
)
type Mode uint8
const (
// ModeNumeric is number mode.
// The Data must be ascii characters [0-9].
ModeNumeric Mode = 0b000
// ModeAlphanumeric is alphabet and number mode.
// The Data must be ascii characters [0-9A-Z $%*+\-./:].
ModeAlphanumeric Mode = 0b001
// ModeBytes is 8-bit bytes mode.
// The Data can include any bytes.
ModeBytes Mode = 0b010
// ModeKanji is Japanese Kanji mode.
ModeKanji Mode = 0b011
ModeTerminated Mode = 0b0000
)
func (mode Mode) String() string {
switch mode {
case ModeNumeric:
return "numeric"
case ModeAlphanumeric:
return "alphanumeric"
case ModeBytes:
return "bytes"
case ModeKanji:
return "kanji"
default:
return "(unknown mode: " + strconv.Itoa(int(mode)) + ")"
}
}
type Segment struct {
Mode Mode
Data []byte
}