Implement EAN and improve Code39 error handling

This commit is contained in:
Fred Boniface 2023-09-04 12:11:45 +01:00
parent 54fa0d34ad
commit 0b2effd135
5 changed files with 33 additions and 16 deletions

View File

@ -8,11 +8,12 @@ import (
) )
func generateCode39(parameters Parameters) (barcode.Barcode, error) { func generateCode39(parameters Parameters) (barcode.Barcode, error) {
var strippedBarcode barcode.Barcode
GeneratedBarcode, err := code39.Encode(parameters.Content, true, true) GeneratedBarcode, err := code39.Encode(parameters.Content, true, true)
if err != nil { if err != nil {
fmt.Println("Error creating Barcode", err) fmt.Println("Error creating Barcode", err)
} else {
strippedBarcode = GeneratedBarcode.(barcode.Barcode)
} }
strippedBarcode := GeneratedBarcode.(barcode.Barcode)
return strippedBarcode, err return strippedBarcode, err
} }

20
generation/ean.go Normal file
View File

@ -0,0 +1,20 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/ean"
)
func generateEAN(parameters Parameters) (barcode.Barcode, error) {
var strippedBarcode barcode.Barcode
GeneratedBarcode, err := ean.Encode(parameters.Content)
if err != nil {
fmt.Println("Error creating Barcode", err)
} else {
strippedBarcode = GeneratedBarcode.(barcode.Barcode)
}
return strippedBarcode, err
}

View File

@ -31,6 +31,8 @@ func Generate(parameters Parameters) (barcode.Barcode, error) {
barcode_content, err = generate2of5(parameters) barcode_content, err = generate2of5(parameters)
case Code39: case Code39:
barcode_content, err = generateCode39(parameters) barcode_content, err = generateCode39(parameters)
case EAN:
barcode_content, err = generateEAN(parameters)
default: default:
fmt.Println("Unsupported barcode type: ", parameters.Format) fmt.Println("Unsupported barcode type: ", parameters.Format)
} }

View File

@ -13,6 +13,7 @@ const (
TwoOfFive BarcodeType = "2of5" TwoOfFive BarcodeType = "2of5"
TwoOfFiveInterleaved BarcodeType = "2of5interleaved" TwoOfFiveInterleaved BarcodeType = "2of5interleaved"
Code39 BarcodeType = "code39" Code39 BarcodeType = "code39"
EAN BarcodeType = "ean"
) )
type ECCLevel int type ECCLevel int

View File

@ -68,21 +68,14 @@ var FormatRules = map[string]rule{
charset: *ANY, charset: *ANY,
divisible: 1, divisible: 1,
}, },
/*
"ean8": {
minLength: 7,
maxLength: 7,
charset: *NUMERAL,
divisible: 1,
},
"ean13": { "ean": {
minLength: 12, minLength: 7,
maxLength: 12, maxLength: 12,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 1, divisible: 1,
}, },
*/
"pdf417": { "pdf417": {
minLength: 1, minLength: 1,
maxLength: 1100, maxLength: 1100,