Implement creation

This commit is contained in:
Fred Boniface 2023-09-01 20:50:47 +01:00
parent 53cae5ebe6
commit 0e1425c1b9
16 changed files with 328 additions and 66 deletions

23
.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
*.png
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work

28
generation/aztec.go Normal file
View File

@ -0,0 +1,28 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/aztec"
)
func generateAztec(parameters Parameters) (barcode.Barcode, error) {
var level uint8
switch parameters.ECCLevel {
case 1:
level = 7
case 2:
level = 15
case 3:
level = 25
case 4:
level = 30
}
aztecCode, err := aztec.Encode([]byte(parameters.Content), int(level), 0)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return aztecCode, err
}

16
generation/codabar.go Normal file
View File

@ -0,0 +1,16 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/codabar"
)
func generateCodabar(parameters Parameters) (barcode.Barcode, error) {
codabar, err := codabar.Encode(parameters.Content)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return codabar, err
}

16
generation/code128.go Normal file
View File

@ -0,0 +1,16 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/code128"
)
func generateCode128(parameters Parameters) (barcode.Barcode, error) {
barcode, err := code128.EncodeWithoutChecksum(parameters.Content)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return barcode, err
}

16
generation/code93.go Normal file
View File

@ -0,0 +1,16 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/code93"
)
func generateCode93(parameters Parameters) (barcode.Barcode, error) {
barcode, err := code93.Encode(parameters.Content, true, true)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return barcode, err
}

16
generation/datamatrix.go Normal file
View File

@ -0,0 +1,16 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/datamatrix"
)
func generateDatamatrix(parameters Parameters) (barcode.Barcode, error) {
barcode, err := datamatrix.Encode(parameters.Content)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return barcode, err
}

31
generation/generate.go Normal file
View File

@ -0,0 +1,31 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
)
func Generate(parameters Parameters) (barcode.Barcode, error) {
var barcode_content barcode.Barcode
var err error
switch parameters.Format {
case Aztec:
barcode_content, err = generateAztec(parameters)
case Codabar:
barcode_content, err = generateCodabar(parameters)
case Code93:
barcode_content, err = generateCode93(parameters)
case Code128:
barcode_content, err = generateCode128(parameters)
case QR:
barcode_content, err = generateQr(parameters)
case Datamatrix:
barcode_content, err = generateDatamatrix(parameters)
default:
fmt.Println("Unsupported barcode type: ", parameters.Format)
}
return barcode_content, err
}

28
generation/qr.go Normal file
View File

@ -0,0 +1,28 @@
package generation
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)
func generateQr(parameters Parameters) (barcode.Barcode, error) {
var level qr.ErrorCorrectionLevel
switch parameters.ECCLevel {
case 1:
level = qr.L
case 2:
level = qr.M
case 3:
level = qr.Q
case 4:
level = qr.H
}
qrCode, err := qr.Encode(parameters.Content, level, qr.Auto)
if err != nil {
fmt.Println("Error creating Barcode", err)
}
return qrCode, err
}

29
generation/types.go Normal file
View File

@ -0,0 +1,29 @@
package generation
type BarcodeType string
const (
Aztec BarcodeType = "aztec"
Codabar BarcodeType = "codabar"
Code93 BarcodeType = "code93"
Code128 BarcodeType = "code128"
QR BarcodeType = "qr"
Datamatrix BarcodeType = "datamatrix"
)
type ECCLevel int
const (
Low ECCLevel = 1
Med ECCLevel = 2
High ECCLevel = 3
Max ECCLevel = 4
)
type Parameters struct {
Format BarcodeType
ECCLevel ECCLevel
Content string
}
//

2
go.mod
View File

@ -1,3 +1,5 @@
module git.fjla.uk/fred.boniface/barcodes module git.fjla.uk/fred.boniface/barcodes
go 1.19 go 1.19
require github.com/boombuler/barcode v1.0.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=

49
main.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"flag"
"fmt"
"image/png"
"os"
"github.com/boombuler/barcode"
"git.fjla.uk/fred.boniface/barcodes/generation"
"git.fjla.uk/fred.boniface/barcodes/validation"
)
func main() {
barcodeType := flag.String("type", "", "Barcode type (aztec, codabar, code93, code128, qr)")
content := flag.String("content", "", "Barcode content")
flag.Parse()
if *barcodeType == "" || *content == "" {
fmt.Println("Usage: ./barcodes --type <barcode_type> --content <content>")
return
}
fmt.Printf("Barcode Type: %s\n", *barcodeType)
fmt.Printf("Barcode Content: %s\n", *content)
validation.Validate(*content, *barcodeType)
parameters := generation.Parameters{
Format: generation.BarcodeType(*barcodeType),
ECCLevel: generation.ECCLevel(3),
Content: *content,
}
generated_barcode, err := generation.Generate(parameters)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
generated_barcode, _ = barcode.Scale(generated_barcode, 200, 200)
file, _ := os.Create("barcode.png")
defer file.Close()
png.Encode(file, generated_barcode)
}

1
strings/wifi.go Normal file
View File

@ -0,0 +1 @@
package strings

View File

@ -5,7 +5,9 @@ import "regexp"
var ( var (
ASCII = regexp.MustCompile(`^[\x00-\x7F]+$`) ASCII = regexp.MustCompile(`^[\x00-\x7F]+$`)
ANY = regexp.MustCompile(`.`) ANY = regexp.MustCompile(`.`)
CODABAR = regexp.MustCompile(`^[0-9-$:/.+]+$`)
CODE39 = regexp.MustCompile(`^[0-9A-Z\-\.\ $%*+/]+$`) CODE39 = regexp.MustCompile(`^[0-9A-Z\-\.\ $%*+/]+$`)
CODE93 = regexp.MustCompile(`^[A-Za-z0-9\-.\$/\+%\s]+$`)
NUMERAL = regexp.MustCompile(`^[0-9]+$`) NUMERAL = regexp.MustCompile(`^[0-9]+$`)
ISO88591 = regexp.MustCompile(`^[\x00-\xFF]+$`) ISO88591 = regexp.MustCompile(`^[\x00-\xFF]+$`)
) )

View File

@ -11,79 +11,82 @@ type rule struct {
divisible uint8 divisible uint8
} }
var aztec = rule{ var formatRules = map[string]rule{
minLength: 1,
maxLength: 3067,
charset: *ANY,
divisible: 1,
}
var code39 = rule{ "2of5": {
minLength: 1,
maxLength: 80,
charset: *CODE39,
divisible: 1,
}
var code128 = rule{
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
}
var datamatrix = rule{
minLength: 1,
maxLength: 2335,
charset: *ANY,
divisible: 1,
}
var ean8 = rule{
minLength: 7,
maxLength: 7,
charset: *NUMERAL,
divisible: 1,
}
var ean13 = rule{
minLength: 12,
maxLength: 12,
charset: *NUMERAL,
divisible: 1,
}
var ean14 = rule{
minLength: 2, minLength: 2,
maxLength: 80, maxLength: 80,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 2, divisible: 2,
} },
var pdf417 = rule{ "aztec": {
minLength: 1, minLength: 1,
maxLength: 1100, maxLength: 3067,
charset: *ANY, charset: *ANY,
divisible: 1, divisible: 1,
} },
var pzn7 = rule{ "codabar": {
minLength: 1,
maxLength: 100,
charset: *CODABAR,
divisible: 1,
},
"code39": {
minLength: 1,
maxLength: 80,
charset: *CODE39,
divisible: 1,
},
"code93": {
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
},
"code128": {
minLength: 1,
maxLength: 80,
charset: *ASCII,
divisible: 1,
},
"datamatrix": {
minLength: 1,
maxLength: 2335,
charset: *ANY,
divisible: 1,
},
"ean8": {
minLength: 7, minLength: 7,
maxLength: 7, maxLength: 7,
charset: *NUMERAL, charset: *NUMERAL,
divisible: 1, divisible: 1,
} },
var qr = rule{ "ean13": {
minLength: 12,
maxLength: 12,
charset: *NUMERAL,
divisible: 1,
},
"pdf417": {
minLength: 1,
maxLength: 1100,
charset: *ANY,
divisible: 1,
},
"qr": {
minLength: 1, minLength: 1,
maxLength: 4296, maxLength: 4296,
charset: *ISO88591, charset: *ISO88591,
divisible: 1, divisible: 1,
} },
var upca = rule{
minLength: 11,
maxLength: 11,
charset: *NUMERAL,
divisible: 1,
} }

View File

@ -3,7 +3,7 @@ package validation
import "fmt" import "fmt"
func Validate(input string, format string) bool { func Validate(input string, format string) bool {
rule, exists := barcodeRules[format] rule, exists := formatRules[format]
if !exists { if !exists {
fmt.Printf("Error: No rule found for format '%s'\n", format) fmt.Printf("Error: No rule found for format '%s'\n", format)
return false return false