Compare commits
No commits in common. "b79280648112c103049ff8768b7b5efcac8e8318" and "1d068b2655dfe83e32c2e32ca66d1f44d09241dc" have entirely different histories.
b792806481
...
1d068b2655
@ -70,15 +70,6 @@ const docTemplate = `{
|
|||||||
"description": "Barcode height",
|
"description": "Barcode height",
|
||||||
"name": "height",
|
"name": "height",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
|
||||||
{
|
|
||||||
"maximum": 100,
|
|
||||||
"minimum": 0,
|
|
||||||
"type": "integer",
|
|
||||||
"default": 10,
|
|
||||||
"description": "Padding around the barcode (included in image size)",
|
|
||||||
"name": "padding",
|
|
||||||
"in": "query"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {}
|
"responses": {}
|
||||||
|
@ -61,15 +61,6 @@
|
|||||||
"description": "Barcode height",
|
"description": "Barcode height",
|
||||||
"name": "height",
|
"name": "height",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
|
||||||
{
|
|
||||||
"maximum": 100,
|
|
||||||
"minimum": 0,
|
|
||||||
"type": "integer",
|
|
||||||
"default": 10,
|
|
||||||
"description": "Padding around the barcode (included in image size)",
|
|
||||||
"name": "padding",
|
|
||||||
"in": "query"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {}
|
"responses": {}
|
||||||
|
@ -190,13 +190,6 @@ paths:
|
|||||||
minimum: 1
|
minimum: 1
|
||||||
name: height
|
name: height
|
||||||
type: integer
|
type: integer
|
||||||
- default: 10
|
|
||||||
description: Padding around the barcode (included in image size)
|
|
||||||
in: query
|
|
||||||
maximum: 100
|
|
||||||
minimum: 0
|
|
||||||
name: padding
|
|
||||||
type: integer
|
|
||||||
produces:
|
produces:
|
||||||
- image/png
|
- image/png
|
||||||
responses: {}
|
responses: {}
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
// @Param content path string true "Barcode content" MinLength(1)
|
// @Param content path string true "Barcode content" MinLength(1)
|
||||||
// @Param width query int false "Barcode width" Minimum(1) Maximum(10000) default(1000)
|
// @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 height query int false "Barcode height" Minimum(1) Maximum(10000) default(1000)
|
||||||
// @Param padding query int false "Padding around the barcode (included in image size)" Minimum(0) Maximum(100) default(10)
|
|
||||||
// @Produce image/png
|
// @Produce image/png
|
||||||
// @Router /v1/barcodes/{type}/{content} [get]
|
// @Router /v1/barcodes/{type}/{content} [get]
|
||||||
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
||||||
@ -27,7 +26,6 @@ func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
|||||||
// Get the width and height from the query parameters
|
// Get the width and height from the query parameters
|
||||||
widthStr := c.Query("width", "1000")
|
widthStr := c.Query("width", "1000")
|
||||||
heightStr := c.Query("height", "1000")
|
heightStr := c.Query("height", "1000")
|
||||||
paddingStr := c.Query("padding", "10")
|
|
||||||
|
|
||||||
// Convert width and height to integers
|
// Convert width and height to integers
|
||||||
width, err := strconv.Atoi(widthStr)
|
width, err := strconv.Atoi(widthStr)
|
||||||
@ -44,15 +42,8 @@ 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
|
// Generate the barcode
|
||||||
barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, width, height, padding)
|
barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, width, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
@ -4,9 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"image/color"
|
|
||||||
"image/draw"
|
|
||||||
"image/png"
|
"image/png"
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
"slices"
|
||||||
@ -20,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BarcodeService interface {
|
type BarcodeService interface {
|
||||||
GenerateBarcode(format string, content string, width int, height int, padding int) (bytes.Buffer, error)
|
GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error)
|
||||||
IsTypeSupported(format string) bool
|
IsTypeSupported(format string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +25,7 @@ type DefaultBarcodeService struct {
|
|||||||
RedisClient *redis.Client
|
RedisClient *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int, padding int) (bytes.Buffer, error) {
|
func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
if !b.IsTypeSupported(format) {
|
if !b.IsTypeSupported(format) {
|
||||||
@ -36,7 +33,7 @@ func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, w
|
|||||||
}
|
}
|
||||||
|
|
||||||
if b.RedisClient != nil {
|
if b.RedisClient != nil {
|
||||||
cachedBarcode, err := b.RedisClient.Get(ctx, fmt.Sprintf("barcode:%s:%s:%d:%d:%d", format, content, width, height, padding)).Result()
|
cachedBarcode, err := b.RedisClient.Get(ctx, fmt.Sprintf("barcode:%s:%s:%d:%d", format, content, width, height)).Result()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Printf("Cache hit for barcode:%s:%s:%d:%d", format, content, width, height)
|
log.Printf("Cache hit for barcode:%s:%s:%d:%d", format, content, width, height)
|
||||||
buf := bytes.Buffer{}
|
buf := bytes.Buffer{}
|
||||||
@ -69,26 +66,13 @@ func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, w
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a white background image
|
scaledCode, err := barcode.Scale(generatedCode, width, height)
|
||||||
bg := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
||||||
white := color.RGBA{255, 255, 255, 255}
|
|
||||||
draw.Draw(bg, bg.Bounds(), &image.Uniform{white}, image.Point{}, draw.Src)
|
|
||||||
|
|
||||||
// Calculate the new size for the barcode to fit within the padding
|
|
||||||
newWidth := width - 2*padding
|
|
||||||
newHeight := height - 2*padding
|
|
||||||
|
|
||||||
// Scale the barcode to the new size
|
|
||||||
scaledCode, err := barcode.Scale(generatedCode, newWidth, newHeight)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytes.Buffer{}, err
|
return bytes.Buffer{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the barcode on top of the white background with padding
|
|
||||||
draw.Draw(bg, scaledCode.Bounds().Add(image.Point{padding, padding}), scaledCode, image.Point{}, draw.Over)
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = png.Encode(&buf, bg)
|
err = png.Encode(&buf, scaledCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytes.Buffer{}, err
|
return bytes.Buffer{}, err
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := t.BarcodeService.GenerateBarcode(format, code, 1000, 500, 0)
|
buf, err := t.BarcodeService.GenerateBarcode(format, code, 1000, 500)
|
||||||
|
|
||||||
return base64.StdEncoding.EncodeToString(buf.Bytes()), err
|
return base64.StdEncoding.EncodeToString(buf.Bytes()), err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user