Add 2of5 and pdf417
This commit is contained in:
parent
f1c8eb82b8
commit
d236acb2d7
16
generation/2of5.go
Normal file
16
generation/2of5.go
Normal file
@ -0,0 +1,16 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/twooffive"
|
||||
)
|
||||
|
||||
func generate2of5(parameters Parameters) (barcode.Barcode, error) {
|
||||
barcode, err := twooffive.Encode(parameters.Content, false)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating Barcode", err)
|
||||
}
|
||||
return barcode, err
|
||||
}
|
16
generation/2of5interleaved.go
Normal file
16
generation/2of5interleaved.go
Normal file
@ -0,0 +1,16 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/twooffive"
|
||||
)
|
||||
|
||||
func generate2of5Interleaved(parameters Parameters) (barcode.Barcode, error) {
|
||||
barcode, err := twooffive.Encode(parameters.Content, true)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating Barcode", err)
|
||||
}
|
||||
return barcode, err
|
||||
}
|
@ -8,9 +8,9 @@ import (
|
||||
)
|
||||
|
||||
func generateCodabar(parameters Parameters) (barcode.Barcode, error) {
|
||||
codabar, err := codabar.Encode(parameters.Content)
|
||||
codabarItem, err := codabar.Encode(parameters.Content)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating Barcode", err)
|
||||
}
|
||||
return codabar, err
|
||||
return codabarItem, err
|
||||
}
|
||||
|
@ -23,6 +23,12 @@ func Generate(parameters Parameters) (barcode.Barcode, error) {
|
||||
barcode_content, err = generateQr(parameters)
|
||||
case Datamatrix:
|
||||
barcode_content, err = generateDatamatrix(parameters)
|
||||
case PDF417:
|
||||
barcode_content, err = generatePDF417(parameters)
|
||||
case TwoOfFiveInterleaved:
|
||||
barcode_content, err = generate2of5Interleaved(parameters)
|
||||
case TwoOfFive:
|
||||
barcode_content, err = generate2of5(parameters)
|
||||
default:
|
||||
fmt.Println("Unsupported barcode type: ", parameters.Format)
|
||||
}
|
||||
|
28
generation/pdf417.go
Normal file
28
generation/pdf417.go
Normal file
@ -0,0 +1,28 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/pdf417"
|
||||
)
|
||||
|
||||
func generatePDF417(parameters Parameters) (barcode.Barcode, error) {
|
||||
var level uint8
|
||||
switch parameters.ECCLevel {
|
||||
case 1:
|
||||
level = byte(1)
|
||||
case 2:
|
||||
level = byte(3)
|
||||
case 3:
|
||||
level = byte(5)
|
||||
case 4:
|
||||
level = byte(8)
|
||||
}
|
||||
pdf417Code, err := pdf417.Encode(parameters.Content, level)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating Barcode", err)
|
||||
}
|
||||
|
||||
return pdf417Code, err
|
||||
}
|
@ -3,12 +3,15 @@ package generation
|
||||
type BarcodeType string
|
||||
|
||||
const (
|
||||
Aztec BarcodeType = "aztec"
|
||||
Codabar BarcodeType = "codabar"
|
||||
Code93 BarcodeType = "code93"
|
||||
Code128 BarcodeType = "code128"
|
||||
QR BarcodeType = "qr"
|
||||
Datamatrix BarcodeType = "datamatrix"
|
||||
Aztec BarcodeType = "aztec"
|
||||
Codabar BarcodeType = "codabar"
|
||||
Code93 BarcodeType = "code93"
|
||||
Code128 BarcodeType = "code128"
|
||||
QR BarcodeType = "qr"
|
||||
Datamatrix BarcodeType = "datamatrix"
|
||||
PDF417 BarcodeType = "pdf417"
|
||||
TwoOfFive BarcodeType = "2of5"
|
||||
TwoOfFiveInterleaved BarcodeType = "2of5interleaved"
|
||||
)
|
||||
|
||||
type ECCLevel int
|
||||
|
@ -14,6 +14,13 @@ type rule struct {
|
||||
var formatRules = map[string]rule{
|
||||
|
||||
"2of5": {
|
||||
minLength: 2,
|
||||
maxLength: 80,
|
||||
charset: *NUMERAL,
|
||||
divisible: 1,
|
||||
},
|
||||
|
||||
"2of5interleaved": {
|
||||
minLength: 2,
|
||||
maxLength: 80,
|
||||
charset: *NUMERAL,
|
||||
|
62
web/routeGenerate.go
Normal file
62
web/routeGenerate.go
Normal file
@ -0,0 +1,62 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"net/http"
|
||||
|
||||
"git.fjla.uk/fred.boniface/barcodes/generation"
|
||||
"git.fjla.uk/fred.boniface/barcodes/validation"
|
||||
"github.com/boombuler/barcode"
|
||||
)
|
||||
|
||||
func generateBarcode(w http.ResponseWriter, r *http.Request) {
|
||||
var req BarcodeRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
parameters := generation.Parameters{
|
||||
Format: generation.BarcodeType(req.BarcodeType),
|
||||
ECCLevel: generation.ECCLevel(req.ECCLevel),
|
||||
Content: req.Content,
|
||||
}
|
||||
|
||||
validate := validation.Validate(req.Content, req.BarcodeType)
|
||||
if !validate {
|
||||
fmt.Println("Validation Failed")
|
||||
http.Error(w, "Validation Failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
barcodeGen, err := generation.Generate(parameters)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if barcodeGen == nil {
|
||||
fmt.Println("Generation failed, no barcode created")
|
||||
http.Error(w, "Barcode Generation Failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
barcodeGen, _ = barcode.Scale(barcodeGen, int(req.Width), int(req.Height))
|
||||
|
||||
image, isImage := barcodeGen.(image.Image)
|
||||
if !isImage {
|
||||
fmt.Println("Generation failed - no image")
|
||||
http.Error(w, "Generated Barcode is not an image", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
err = png.Encode(w, image)
|
||||
if err != nil {
|
||||
http.Error(w, "Error streaming barcode", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
@ -1,15 +1,8 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"net/http"
|
||||
|
||||
"git.fjla.uk/fred.boniface/barcodes/generation"
|
||||
"git.fjla.uk/fred.boniface/barcodes/validation"
|
||||
"github.com/boombuler/barcode"
|
||||
)
|
||||
|
||||
func StartServer() {
|
||||
@ -27,52 +20,3 @@ func StartServer() {
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func generateBarcode(w http.ResponseWriter, r *http.Request) {
|
||||
var req BarcodeRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
parameters := generation.Parameters{
|
||||
Format: generation.BarcodeType(req.BarcodeType),
|
||||
ECCLevel: generation.ECCLevel(req.ECCLevel),
|
||||
Content: req.Content,
|
||||
}
|
||||
|
||||
validate := validation.Validate(req.Content, req.BarcodeType)
|
||||
if !validate {
|
||||
fmt.Println("Validation Failed")
|
||||
http.Error(w, "Validation Failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
barcodeGen, err := generation.Generate(parameters)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if barcodeGen == nil {
|
||||
fmt.Println("Generation failed, no barcode created")
|
||||
http.Error(w, "Barcode Generation Failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
barcodeGen, _ = barcode.Scale(barcodeGen, req.Width, req.Height)
|
||||
|
||||
image, isImage := barcodeGen.(image.Image)
|
||||
if !isImage {
|
||||
fmt.Println("Generation failed - no image")
|
||||
http.Error(w, "Generated Barcode is not an image", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
err = png.Encode(w, image)
|
||||
if err != nil {
|
||||
http.Error(w, "Error streaming barcode", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user