added codabar support
This commit is contained in:
parent
e5d11acdbf
commit
987cf1bccc
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue