Parcourir la source

Merge pull request #8 from alexjg/master

Add checksum calculation to relevant barcode types
Florian il y a 9 ans
Parent
commit
d1c739eed5
10 fichiers modifiés avec 36 ajouts et 10 suppressions
  1. 1 0
      barcode.go
  2. 1 1
      codabar/encoder.go
  3. 1 1
      code128/encode.go
  4. 6 1
      code39/encoder.go
  5. 4 0
      datamatrix/datamatrixcode.go
  6. 5 2
      ean/encoder.go
  7. 4 0
      qr/qrcode.go
  8. 4 0
      scaledbarcode.go
  9. 1 1
      twooffive/encoder.go
  10. 9 4
      utils/base1dcode.go

+ 1 - 0
barcode.go

@@ -17,4 +17,5 @@ type Barcode interface {
 	Metadata() Metadata
 	// the data that was encoded in this barcode
 	Content() string
+	CheckSum() int
 }

+ 1 - 1
codabar/encoder.go

@@ -45,5 +45,5 @@ func Encode(content string) (barcode.Barcode, error) {
 		}
 		resBits.AddBit(encodingTable[r]...)
 	}
-	return utils.New1DCode("Codabar", content, resBits), nil
+	return utils.New1DCode("Codabar", content, resBits, 0), nil
 }

+ 1 - 1
code128/encode.go

@@ -116,5 +116,5 @@ func Encode(content string) (barcode.Barcode, error) {
 	}
 	result.AddBit(encodingTable[sum%103]...)
 	result.AddBit(encodingTable[stopSymbol]...)
-	return utils.New1DCode("Code 128", content, result), nil
+	return utils.New1DCode("Code 128", content, result, sum%103), nil
 }

+ 6 - 1
code39/encoder.go

@@ -3,6 +3,7 @@ package code39
 
 import (
 	"errors"
+	"strconv"
 	"strings"
 
 	"github.com/boombuler/barcode"
@@ -143,5 +144,9 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
 		result.AddBit(info.data...)
 	}
 
-	return utils.New1DCode("Code 39", content, result), nil
+	checkSum, err := strconv.ParseInt(getChecksum(content), 10, 64)
+	if err != nil {
+		checkSum = 0
+	}
+	return utils.New1DCode("Code 39", content, result, int(checkSum)), nil
 }

+ 4 - 0
datamatrix/datamatrixcode.go

@@ -40,6 +40,10 @@ func (c *datamatrixCode) At(x, y int) color.Color {
 	return color.White
 }
 
+func (c *datamatrixCode) CheckSum() int {
+	return 0
+}
+
 func (c *datamatrixCode) get(x, y int) bool {
 	return c.GetBit(x*c.Rows + y)
 }

+ 5 - 2
ean/encoder.go

@@ -159,25 +159,28 @@ func encodeEAN13(code string) *utils.BitList {
 
 // Encode returns a EAN 8 or EAN 13 barcode for the given code
 func Encode(code string) (barcode.Barcode, error) {
+	var checkSum int
 	if len(code) == 7 || len(code) == 12 {
 		code += string(calcCheckNum(code))
+		checkSum = utils.RuneToInt(calcCheckNum(code))
 	} else if len(code) == 8 || len(code) == 13 {
 		check := code[0 : len(code)-1]
 		check += string(calcCheckNum(check))
 		if check != code {
 			return nil, errors.New("checksum missmatch")
 		}
+		checkSum = utils.RuneToInt(rune(code[len(code)-1]))
 	}
 
 	if len(code) == 8 {
 		result := encodeEAN8(code)
 		if result != nil {
-			return utils.New1DCode("EAN 8", code, result), nil
+			return utils.New1DCode("EAN 8", code, result, checkSum), nil
 		}
 	} else if len(code) == 13 {
 		result := encodeEAN13(code)
 		if result != nil {
-			return utils.New1DCode("EAN 13", code, result), nil
+			return utils.New1DCode("EAN 13", code, result, checkSum), nil
 		}
 	}
 	return nil, errors.New("invalid ean code data")

+ 4 - 0
qr/qrcode.go

@@ -46,6 +46,10 @@ func (qr *qrcode) Set(x, y int, val bool) {
 	qr.data.SetBit(x*qr.dimension+y, val)
 }
 
+func (qr *qrcode) CheckSum() int {
+	return 0
+}
+
 func (qr *qrcode) calcPenalty() uint {
 	return qr.calcPenaltyRule1() + qr.calcPenaltyRule2() + qr.calcPenaltyRule3() + qr.calcPenaltyRule4()
 }

+ 4 - 0
scaledbarcode.go

@@ -36,6 +36,10 @@ func (bc *scaledBarcode) At(x, y int) color.Color {
 	return bc.wrapperFunc(x, y)
 }
 
+func (bc *scaledBarcode) CheckSum() int {
+	return bc.wrapped.CheckSum()
+}
+
 // Scale returns a resized barcode with the given width and height.
 func Scale(bc Barcode, width, height int) (Barcode, error) {
 	switch bc.Metadata().Dimensions {

+ 1 - 1
twooffive/encoder.go

@@ -134,5 +134,5 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) {
 	if interleaved {
 		kindTxt = " (interleaved)"
 	}
-	return utils.New1DCode("2 of 5"+kindTxt, content, resBits), nil
+	return utils.New1DCode("2 of 5"+kindTxt, content, resBits, -1), nil
 }

+ 9 - 4
utils/base1dcode.go

@@ -10,8 +10,9 @@ import (
 
 type base1DCode struct {
 	*BitList
-	kind    string
-	content string
+	kind     string
+	content  string
+	checksum int
 }
 
 func (c *base1DCode) Content() string {
@@ -37,7 +38,11 @@ func (c *base1DCode) At(x, y int) color.Color {
 	return color.White
 }
 
+func (c *base1DCode) CheckSum() int {
+	return c.checksum
+}
+
 // New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
-func New1DCode(codeKind, content string, bars *BitList) barcode.Barcode {
-	return &base1DCode{bars, codeKind, content}
+func New1DCode(codeKind, content string, bars *BitList, checksum int) barcode.Barcode {
+	return &base1DCode{bars, codeKind, content, checksum}
 }