From de6fe4991cf30b37bdff73269384b81156b24eb3 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 12 Dec 2024 19:11:01 +0100 Subject: [PATCH] feat(barcode): Padding --- handlers/barcode.go | 19 ++++++++++++++----- services/barcode.go | 17 ++++++++++++++--- services/templater.go | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/handlers/barcode.go b/handlers/barcode.go index 5aaa4bc..0c8ae95 100644 --- a/handlers/barcode.go +++ b/handlers/barcode.go @@ -11,10 +11,11 @@ import ( // @Summary Generate barcodes // @Description Generate barcodes based on the provided data // @Tags barcodes -// @Param type path string true "Barcode type" Enums(ean13, code128) -// @Param content path string true "Barcode content" MinLength(1) -// @Param width query int false "Barcode width" Minimum(1) Maximum(10000) default(1000) -// @Param height query int false "Barcode height" Minimum(1) Maximum(10000) default(1000) +// @Param type path string true "Barcode type" Enums(ean13, code128) +// @Param content path string true "Barcode content" MinLength(1) +// @Param width query int false "Barcode width" Minimum(1) Maximum(10000) default(1000) +// @Param height query int false "Barcode height" Minimum(1) Maximum(10000) default(1000) +// @Param padding query int false "Padding around the barcode" Minimum(0) Maximum(100) default(10) // @Produce image/png // @Router /v1/barcodes/{type}/{content} [get] func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error { @@ -26,6 +27,7 @@ func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error { // Get the width and height from the query parameters widthStr := c.Query("width", "1000") heightStr := c.Query("height", "1000") + paddingStr := c.Query("padding", "10") // Convert width and height to integers width, err := strconv.Atoi(widthStr) @@ -42,8 +44,15 @@ func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error { }) } + padding, err := strconv.Atoi(paddingStr) + if err != nil { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "error": "Invalid padding parameter", + }) + } + // Generate the barcode - barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, width, height) + barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, width, height, padding) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": err.Error(), diff --git a/services/barcode.go b/services/barcode.go index 29cb5b0..59a3796 100644 --- a/services/barcode.go +++ b/services/barcode.go @@ -4,6 +4,9 @@ import ( "bytes" "context" "fmt" + "image" + "image/color" + "image/draw" "image/png" "log" "slices" @@ -17,7 +20,7 @@ import ( ) type BarcodeService interface { - GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) + GenerateBarcode(format string, content string, width int, height int, padding int) (bytes.Buffer, error) IsTypeSupported(format string) bool } @@ -25,7 +28,7 @@ type DefaultBarcodeService struct { RedisClient *redis.Client } -func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) { +func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int, padding int) (bytes.Buffer, error) { ctx := context.Background() if !b.IsTypeSupported(format) { @@ -71,8 +74,16 @@ func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, w return bytes.Buffer{}, err } + // Create a white background image + bg := image.NewRGBA(image.Rect(0, 0, width+2*padding, height+2*padding)) + white := color.RGBA{255, 255, 255, 255} + draw.Draw(bg, bg.Bounds(), &image.Uniform{white}, image.Point{}, draw.Src) + + // Draw the barcode on top of the white background with padding + draw.Draw(bg, scaledCode.Bounds().Add(image.Point{padding + (width-scaledCode.Bounds().Dx())/2, padding + (height-scaledCode.Bounds().Dy())/2}), scaledCode, image.Point{}, draw.Over) + var buf bytes.Buffer - err = png.Encode(&buf, scaledCode) + err = png.Encode(&buf, bg) if err != nil { return bytes.Buffer{}, err } diff --git a/services/templater.go b/services/templater.go index 46cfa0d..fa644ae 100644 --- a/services/templater.go +++ b/services/templater.go @@ -42,7 +42,7 @@ func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix st } } - buf, err := t.BarcodeService.GenerateBarcode(format, code, 1000, 500) + buf, err := t.BarcodeService.GenerateBarcode(format, code, 1000, 500, 0) return base64.StdEncoding.EncodeToString(buf.Bytes()), err }