From 987cf1bccc14c43f7044d046f6778a303b52562f Mon Sep 17 00:00:00 2001 From: boombuler Date: Fri, 13 Dec 2013 18:43:18 +0100 Subject: [PATCH] added codabar support --- codabar/codabarcode.go | 36 +++++++++++++++++++++++++++++ codabar/encoder.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 codabar/codabarcode.go create mode 100644 codabar/encoder.go diff --git a/codabar/codabarcode.go b/codabar/codabarcode.go new file mode 100644 index 0000000..c7ef230 --- /dev/null +++ b/codabar/codabarcode.go @@ -0,0 +1,36 @@ +package codabar + +import ( + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/utils" + "image" + "image/color" +) + +type codabarcode struct { + *utils.BitList + content string +} + +func (c *codabarcode) Content() string { + return c.content +} + +func (c *codabarcode) Metadata() barcode.Metadata { + return barcode.Metadata{"Codabar", 1} +} + +func (c *codabarcode) ColorModel() color.Model { + return color.Gray16Model +} + +func (c *codabarcode) Bounds() image.Rectangle { + return image.Rect(0, 0, c.Len(), 1) +} + +func (c *codabarcode) At(x, y int) color.Color { + if c.GetBit(x) { + return color.Black + } + return color.White +} diff --git a/codabar/encoder.go b/codabar/encoder.go new file mode 100644 index 0000000..4a4be1d --- /dev/null +++ b/codabar/encoder.go @@ -0,0 +1,52 @@ +package codabar + +import ( + "fmt" + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/utils" + "regexp" +) + +var encodingTable map[rune][]bool = 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}, +} + +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) + } + bits, ok := encodingTable[r] + if !ok { + return nil, fmt.Errorf("can not encode \"%s\"", content) + } + for _, bit := range bits { + resBits.AddBit(bit) + } + } + return &codabarcode{resBits, content}, nil +}