added unicode support for qr codes and some minor fixes

This commit is contained in:
boombuler 2013-12-11 20:26:35 +01:00
parent 1af72b7408
commit 29d3fcfa98
4 changed files with 61 additions and 13 deletions

View File

@ -4,14 +4,25 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/boombuler/barcode" "github.com/boombuler/barcode"
"strings"
) )
var alphaNumericTable map[byte]int = map[byte]int{ const charSet string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, func stringToAlphaIdx(content string) <-chan int {
'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, result := make(chan int)
'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35, ' ': 36, '$': 37, '%': 38, '*': 39, go func() {
'+': 40, '-': 41, '.': 42, '/': 43, ':': 44, for _, r := range content {
idx := strings.IndexRune(charSet, r)
result <- idx
if idx < 0 {
break
}
}
close(result)
}()
return result
} }
func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) { func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) {
@ -30,20 +41,22 @@ func encodeAlphaNumeric(content string, ecl ErrorCorrectionLevel) (*barcode.BitL
res.AddBits(int(alphaNumericMode), 4) res.AddBits(int(alphaNumericMode), 4)
res.AddBits(len(content), vi.charCountBits(alphaNumericMode)) res.AddBits(len(content), vi.charCountBits(alphaNumericMode))
encoder := stringToAlphaIdx(content)
for idx := 0; idx < len(content)/2; idx++ { for idx := 0; idx < len(content)/2; idx++ {
c1, ok1 := alphaNumericTable[content[idx*2]] c1 := <-encoder
c2, ok2 := alphaNumericTable[content[(idx*2)+1]] c2 := <-encoder
if !ok1 || !ok2 { if c1 < 0 || c2 < 0 {
return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric) return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric)
} }
res.AddBits(c1*45+c2, 11) res.AddBits(c1*45+c2, 11)
} }
if contentLenIsOdd { if contentLenIsOdd {
c1, ok := alphaNumericTable[content[len(content)-1]] c := <-encoder
if !ok { if c < 0 {
return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric) return nil, nil, fmt.Errorf("\"%s\" can not be encoded as %s", content, AlphaNumeric)
} }
res.AddBits(c1, 6) res.AddBits(c, 6)
} }
addPaddingAndTerminator(res, vi) addPaddingAndTerminator(res, vi)

View File

@ -14,6 +14,9 @@ func encodeAuto(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *ve
if bits != nil && vi != nil { if bits != nil && vi != nil {
return bits, vi, nil return bits, vi, nil
} }
bits, vi, _ = Unicode.getEncoder()(content, ecl)
if bits != nil && vi != nil {
return bits, vi, nil
}
return nil, nil, fmt.Errorf("No encoding found to encode \"%s\"", content) return nil, nil, fmt.Errorf("No encoding found to encode \"%s\"", content)
} }

View File

@ -18,6 +18,8 @@ const (
Numeric Numeric
// Encode only uppercase letters, numbers and [Space], $, %, *, +, -, ., /, : // Encode only uppercase letters, numbers and [Space], $, %, *, +, -, ., /, :
AlphaNumeric AlphaNumeric
// Encodes string as utf-8
Unicode
) )
func (e Encoding) getEncoder() encodeFn { func (e Encoding) getEncoder() encodeFn {
@ -28,6 +30,8 @@ func (e Encoding) getEncoder() encodeFn {
return encodeNumeric return encodeNumeric
case AlphaNumeric: case AlphaNumeric:
return encodeAlphaNumeric return encodeAlphaNumeric
case Unicode:
return encodeUnicode
} }
return nil return nil
} }
@ -40,6 +44,8 @@ func (e Encoding) String() string {
return "Numeric" return "Numeric"
case AlphaNumeric: case AlphaNumeric:
return "AlphaNumeric" return "AlphaNumeric"
case Unicode:
return "Unicode"
} }
return "" return ""
} }

26
qr/utf8.go Normal file
View File

@ -0,0 +1,26 @@
package qr
import (
"errors"
"github.com/boombuler/barcode"
)
func encodeUnicode(content string, ecl ErrorCorrectionLevel) (*barcode.BitList, *versionInfo, error) {
data := []byte(content)
vi := findSmallestVersionInfo(ecl, byteMode, len(data)*8)
if vi == nil {
return nil, nil, errors.New("To much data to encode")
}
// It's not correct to add the unicode bytes to the result directly but most readers can't handle the
// required ECI header...
res := new(barcode.BitList)
res.AddBits(int(byteMode), 4)
res.AddBits(len(content), vi.charCountBits(byteMode))
for _, b := range data {
res.AddByte(b)
}
addPaddingAndTerminator(res, vi)
return res, vi, nil
}