This commit is contained in:
Florian Sundermann committed 2013-12-16 14:10:24 +01:00
1 parent a4167688a0
commit 9741f0ff8d
12 files changed
+143 -270

No files matched your search

-47
View File
@@ -1,47 +0,0 @@
package ean
import (
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
"image"
"image/color"
)
type eancode struct {
*utils.BitList
content string
}
func newEANCode(isEAN8 bool) *eancode {
capacity := 95
if isEAN8 {
capacity = 67
}
return &eancode{utils.NewBitList(capacity), ""}
}
func (c *eancode) Content() string {
return c.content
}
func (c *eancode) Metadata() barcode.Metadata {
if c.Len() == 67 {
return barcode.Metadata{"EAN 8", 1}
}
return barcode.Metadata{"EAN 13", 1}
}
func (c *eancode) ColorModel() color.Model {
return color.Gray16Model
}
func (c *eancode) Bounds() image.Rectangle {
return image.Rect(0, 0, c.Len(), 1)
}
func (c *eancode) At(x, y int) color.Color {
if c.GetBit(x) {
return color.Black
}
return color.White
}
+28 -59
View File
@@ -4,6 +4,7 @@ package ean
import (
"errors"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/utils"
)
type encodedNumber struct {
@@ -146,21 +147,14 @@ func calcCheckNum(code string) rune {
return intToRune((10 - (sum % 10)) % 10)
}
func encodeEAN8(code string, result *eancode) bool {
pos := 0
appendBit := func(b bool) {
result.SetBit(pos, b)
pos++
}
appendBit(true)
appendBit(false)
appendBit(true)
func encodeEAN8(code string) *utils.BitList {
result := new(utils.BitList)
result.AddBit(true, false, true)
for cpos, r := range code {
num, ok := encoderTable[r]
if !ok {
return false
return nil
}
var data []bool
if cpos < 4 {
@@ -170,39 +164,24 @@ func encodeEAN8(code string, result *eancode) bool {
}
if cpos == 4 {
appendBit(false)
appendBit(true)
appendBit(false)
appendBit(true)
appendBit(false)
}
for _, bit := range data {
appendBit(bit)
result.AddBit(false, true, false, true, false)
}
result.AddBit(data...)
}
result.AddBit(true, false, true)
appendBit(true)
appendBit(false)
appendBit(true)
return true
return result
}
func encodeEAN13(code string, result *eancode) bool {
pos := 0
appendBit := func(b bool) {
result.SetBit(pos, b)
pos++
}
appendBit(true)
appendBit(false)
appendBit(true)
func encodeEAN13(code string) *utils.BitList {
result := new(utils.BitList)
result.AddBit(true, false, true)
var firstNum []bool
for cpos, r := range code {
num, ok := encoderTable[r]
if !ok {
return false
return nil
}
if cpos == 0 {
firstNum = num.CheckSum
@@ -221,21 +200,12 @@ func encodeEAN13(code string, result *eancode) bool {
}
if cpos == 7 {
appendBit(false)
appendBit(true)
appendBit(false)
appendBit(true)
appendBit(false)
}
for _, bit := range data {
appendBit(bit)
result.AddBit(false, true, false, true, false)
}
result.AddBit(data...)
}
appendBit(true)
appendBit(false)
appendBit(true)
return true
result.AddByte(true, false, true)
return result
}
// encodes the given EAN 8 or EAN 13 number to a barcode image
@@ -249,18 +219,17 @@ func Encode(code string) (barcode.Barcode, error) {
return nil, errors.New("checksum missmatch!")
}
}
ean8 := false
var result *utils.BitList = nil
if len(code) == 8 {
ean8 = true
} else if len(code) != 13 {
return nil, errors.New("invalid ean code data")
result := encodeEAN8(code)
if result != nil {
return utils.New1DCode("EAN 8", code, result), nil
}
} else if len(code) == 13 {
result := encodeEAN13(code)
if result != nil {
return utils.New1DCode("EAN 13", code, result), nil
}
}
result := newEANCode(ean8)
if (ean8 && encodeEAN8(code, result)) || (!ean8 && encodeEAN13(code, result)) {
result.content = code
return result, nil
}
return nil, errors.New("ean code contains invalid characters")
return nil, errors.New("invalid ean code data")
}