Skip to content

Commit

Permalink
增加条码包barcode和token算法包otp
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed May 30, 2016
1 parent 23797b5 commit 021ee8e
Show file tree
Hide file tree
Showing 57 changed files with 5,263 additions and 0 deletions.
21 changes: 21 additions & 0 deletions barcode/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Florian Sundermann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions barcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##Introduction##
This is a package for GO which can be used to create different types of barcodes.

##Supported Barcode Types##
* Codabar
* Code 128
* Code 39
* EAN 8
* EAN 13
* Datamatrix
* QR Codes
* 2 of 5

##Documentation##
See [GoDoc](https://godoc.org/github.com/boombuler/barcode)

To create a barcode use the Encode function from one of the subpackages.
21 changes: 21 additions & 0 deletions barcode/barcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package barcode

import "image"

// Contains some meta information about a barcode
type Metadata struct {
// the name of the barcode kind
CodeKind string
// contains 1 for 1D barcodes or 2 for 2D barcodes
Dimensions byte
}

// a rendered and encoded barcode
type Barcode interface {
image.Image
// returns some meta information about the barcode
Metadata() Metadata
// the data that was encoded in this barcode
Content() string
CheckSum() int
}
49 changes: 49 additions & 0 deletions barcode/codabar/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package codabar can create Codabar barcodes
package codabar

import (
"fmt"
"regexp"

"github.com/lessgo/lessgoext/barcode"
"github.com/lessgo/lessgoext/barcode/utils"
)

var encodingTable = map[rune][]bool{
'0': []bool{true, false, true, false, true, false, false, true, true},
'1': []bool{true, false, true, false, true, true, false, false, true},
'2': []bool{true, false, true, false, false, true, false, true, true},
'3': []bool{true, true, false, false, true, false, true, false, true},
'4': []bool{true, false, true, true, false, true, false, false, true},
'5': []bool{true, true, false, true, false, true, false, false, true},
'6': []bool{true, false, false, true, false, true, false, true, true},
'7': []bool{true, false, false, true, false, true, true, false, true},
'8': []bool{true, false, false, true, true, false, true, false, true},
'9': []bool{true, true, false, true, false, false, true, false, true},
'-': []bool{true, false, true, false, false, true, true, false, true},
'$': []bool{true, false, true, true, false, false, true, false, true},
':': []bool{true, true, false, true, false, true, true, false, true, true},
'/': []bool{true, true, false, true, true, false, true, false, true, true},
'.': []bool{true, true, false, true, true, false, true, true, false, true},
'+': []bool{true, false, true, true, false, false, true, true, false, false, true, true},
'A': []bool{true, false, true, true, false, false, true, false, false, true},
'B': []bool{true, false, true, false, false, true, false, false, true, true},
'C': []bool{true, false, false, true, false, false, true, false, true, true},
'D': []bool{true, false, true, false, false, true, true, false, false, true},
}

// Encode creates a codabar barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
return nil, fmt.Errorf("can not encode \"%s\"", content)
}
resBits := new(utils.BitList)
for i, r := range content {
if i > 0 {
resBits.AddBit(false)
}
resBits.AddBit(encodingTable[r]...)
}
return utils.New1DCode("Codabar", content, resBits, 0), nil
}
32 changes: 32 additions & 0 deletions barcode/codabar/encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package codabar

import (
"image/color"
"testing"
)

func Test_Encode(t *testing.T) {
_, err := Encode("FOOBAR")
if err == nil {
t.Error("\"FOOBAR\" should not be encodable")
}

testEncode := func(txt, testResult string) {
code, err := Encode(txt)
if err != nil || code == nil {
t.Fail()
} else {
if code.Bounds().Max.X != len(testResult) {
t.Errorf("%v: length missmatch", txt)
} else {
for i, r := range testResult {
if (code.At(i, 0) == color.Black) != (r == '1') {
t.Errorf("%v: code missmatch on position %d", txt, i)
}
}
}
}
}

testEncode("A40156B", "10110010010101101001010101001101010110010110101001010010101101010010011")
}
120 changes: 120 additions & 0 deletions barcode/code128/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Package code128 can create Code128 barcodes
package code128

import (
"fmt"
"strings"
"unicode/utf8"

"github.com/lessgo/lessgoext/barcode"
"github.com/lessgo/lessgoext/barcode/utils"
)

func strToRunes(str string) []rune {
result := make([]rune, utf8.RuneCountInString(str))
i := 0
for _, r := range str {
result[i] = r
i++
}
return result
}

func shouldUseCTable(nextRunes []rune, curEncoding byte) bool {
requiredDigits := 4
if curEncoding == startCSymbol {
requiredDigits = 2
}
if len(nextRunes) < requiredDigits {
return false
}
for i := 0; i < requiredDigits; i++ {
if nextRunes[i] < '0' || nextRunes[i] > '9' {
return false
}
}
return true
}

func getCodeIndexList(content []rune) *utils.BitList {
result := new(utils.BitList)
curEncoding := byte(0)
for i := 0; i < len(content); i++ {

if shouldUseCTable(content[i:], curEncoding) {
if curEncoding != startCSymbol {
if curEncoding == byte(0) {
result.AddByte(startCSymbol)
} else {
result.AddByte(codeCSymbol)
}
curEncoding = startCSymbol
}
idx := (content[i] - '0') * 10
i++
idx = idx + (content[i] - '0')

result.AddByte(byte(idx))
} else {
if curEncoding != startBSymbol {
if curEncoding == byte(0) {
result.AddByte(startBSymbol)
} else {
result.AddByte(codeBSymbol)
}
curEncoding = startBSymbol
}
var idx int
switch content[i] {
case FNC1:
idx = 102
break
case FNC2:
idx = 97
break
case FNC3:
idx = 96
break
case FNC4:
idx = 100
break
default:
idx = strings.IndexRune(bTable, content[i])
break
}

if idx < 0 {
return nil
}
result.AddByte(byte(idx))
}
}
return result
}

// Encode creates a Code 128 barcode for the given content
func Encode(content string) (barcode.Barcode, error) {
contentRunes := strToRunes(content)
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
}
idxList := getCodeIndexList(contentRunes)

if idxList == nil {
return nil, fmt.Errorf("\"%s\" could not be encoded", content)
}

result := new(utils.BitList)
sum := 0
for i, idx := range idxList.GetBytes() {
if i == 0 {
sum = int(idx)
} else {
sum += i * int(idx)
}
result.AddBit(encodingTable[idx]...)
}
result.AddBit(encodingTable[sum%103]...)
result.AddBit(encodingTable[stopSymbol]...)
return utils.New1DCode("Code 128", content, result, sum%103), nil
}
51 changes: 51 additions & 0 deletions barcode/code128/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package code128

import (
"image/color"
"testing"
)

func testEncode(t *testing.T, txt, testResult string) {
code, err := Encode(txt)
if err != nil || code == nil {
t.Error(err)
} else {
if code.Bounds().Max.X != len(testResult) {
t.Errorf("%v: length missmatch", txt)
} else {
for i, r := range testResult {
if (code.At(i, 0) == color.Black) != (r == '1') {
t.Errorf("%v: code missmatch on position %d", txt, i)
}
}
}
}
}

func Test_EncodeFunctionChars(t *testing.T) {
encFNC1 := "11110101110"
encFNC2 := "11110101000"
encFNC3 := "10111100010"
encFNC4 := "10111101110"
encStartB := "11010010000"
encStop := "1100011101011"

testEncode(t, string(FNC1)+"123", encStartB+encFNC1+"10011100110"+"11001110010"+"11001011100"+"11001000010"+encStop)
testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
}

func Test_Unencodable(t *testing.T) {
if _, err := Encode(""); err == nil {
t.Fail()
}
if _, err := Encode("ä"); err == nil {
t.Fail()
}
}

func Test_EncodeCTable(t *testing.T) {
testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
}
Loading

0 comments on commit 021ee8e

Please sign in to comment.