Skip to content

Commit

Permalink
change codes to use png instead of jpg (#483)
Browse files Browse the repository at this point in the history
* this will improve quality of qr-code, matrix-code and barcode.
  • Loading branch information
johnfercher authored Oct 13, 2024
1 parent 268b9c0 commit 9c854d1
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 61 deletions.
Binary file modified docs/assets/pdf/barcodegridv2.pdf
100644 → 100755
Binary file not shown.
Binary file modified docs/assets/pdf/datamatrixgridv2.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/qrgridv2.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/assets/text/barcodegridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 1.37ms, executions: [1.37ms]
add_row -> avg: 358.60ns, executions: [1.16μs, 0.17μs, 0.15μs, 0.10μs, 0.22μs]
file_size -> 16.54Kb
generate -> avg: 8.66ms, executions: [8.66ms]
add_row -> avg: 329.00ns, executions: [1.21μs, 0.14μs, 0.12μs, 0.07μs, 0.09μs]
file_size -> 6.85Kb
6 changes: 3 additions & 3 deletions docs/assets/text/datamatrixgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 177.74μs, executions: [177.74μs]
add_row -> avg: 315.50ns, executions: [876.00ns, 162.00ns, 134.00ns, 90.00ns]
file_size -> 5.27Kb
generate -> avg: 208.63μs, executions: [208.63μs]
add_row -> avg: 270.75ns, executions: [747.00ns, 152.00ns, 100.00ns, 84.00ns]
file_size -> 5.14Kb
6 changes: 3 additions & 3 deletions docs/assets/text/qrgridv2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
generate -> avg: 199.99μs, executions: [199.99μs]
add_row -> avg: 426.00ns, executions: [1.25μs, 0.21μs, 0.14μs, 0.10μs]
file_size -> 5.34Kb
generate -> avg: 180.99μs, executions: [180.99μs]
add_row -> avg: 292.00ns, executions: [864.00ns, 131.00ns, 100.00ns, 73.00ns]
file_size -> 5.18Kb
17 changes: 12 additions & 5 deletions internal/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package code
import (
"bytes"
"image"
"image/jpeg"
"image/color/palette"
"image/draw"
"image/png"

"github.com/boombuler/barcode/code128"
"github.com/boombuler/barcode/ean"
Expand Down Expand Up @@ -85,17 +87,22 @@ func getBarcodeClosure(barcodeType barcode.Type) func(code string) (libBarcode.B

func (c *code) getImage(img image.Image) (*entity.Image, error) {
var buf bytes.Buffer
err := jpeg.Encode(&buf, img, nil)

dst := image.NewPaletted(img.Bounds(), palette.Plan9)
drawer := draw.Drawer(draw.Src)
drawer.Draw(dst, dst.Bounds(), img, img.Bounds().Min)

err := png.Encode(&buf, dst)
if err != nil {
return nil, err
}

imgEntity := &entity.Image{
Bytes: buf.Bytes(),
Extension: extension.Jpg,
Extension: extension.Png,
Dimensions: &entity.Dimensions{
Width: float64(img.Bounds().Dx()),
Height: float64(img.Bounds().Dy()),
Width: float64(dst.Bounds().Dx()),
Height: float64(dst.Bounds().Dy()),
},
}

Expand Down
14 changes: 7 additions & 7 deletions internal/providers/gofpdf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (g *provider) AddMatrixCode(code string, cell *entity.Cell, prop *props.Rec
return
}

err = g.image.Add(img, cell, g.cfg.Margins, prop, extension.Jpg, false)
err = g.image.Add(img, cell, g.cfg.Margins, prop, extension.Png, false)
if err != nil {
g.fpdf.ClearError()
g.text.Add("could not add matrixcode to document", cell, merror.DefaultErrorText)
Expand All @@ -83,15 +83,15 @@ func (g *provider) AddQrCode(code string, cell *entity.Cell, prop *props.Rect) {
return
}

err = g.image.Add(img, cell, g.cfg.Margins, prop, extension.Jpg, false)
err = g.image.Add(img, cell, g.cfg.Margins, prop, extension.Png, false)
if err != nil {
g.fpdf.ClearError()
g.text.Add("could not add qrcode to document", cell, merror.DefaultErrorText)
}
}

func (g *provider) AddBarCode(code string, cell *entity.Cell, prop *props.Barcode) {
image, err := g.cache.GetImage(g.getBarcodeImageName(fmt.Sprintf("bar-code-%s", code), prop), extension.Jpg)
image, err := g.cache.GetImage(g.getBarcodeImageName(fmt.Sprintf("bar-code-%s", code), prop), extension.Png)
if err != nil {
image, err = g.code.GenBar(code, cell, prop)
}
Expand All @@ -101,7 +101,7 @@ func (g *provider) AddBarCode(code string, cell *entity.Cell, prop *props.Barcod
}

g.cache.AddImage(g.getBarcodeImageName(fmt.Sprintf("bar-code-%s", code), prop), image)
err = g.image.Add(image, cell, g.cfg.Margins, prop.ToRectProp(), extension.Jpg, false)
err = g.image.Add(image, cell, g.cfg.Margins, prop.ToRectProp(), extension.Png, false)
if err != nil {
g.fpdf.ClearError()
g.text.Add("could not add barcode to document", cell, merror.DefaultErrorText)
Expand Down Expand Up @@ -230,7 +230,7 @@ func (g *provider) GetDimensionsByMatrixCode(code string) (*entity.Dimensions, e
return nil, err
}

imgInfo, _ := g.image.GetImageInfo(img, extension.Jpg)
imgInfo, _ := g.image.GetImageInfo(img, extension.Png)

if imgInfo == nil {
return nil, errors.New("could not read image options, maybe path/name is wrong")
Expand All @@ -246,7 +246,7 @@ func (g *provider) GetDimensionsByQrCode(code string) (*entity.Dimensions, error
return nil, err
}

imgInfo, _ := g.image.GetImageInfo(img, extension.Jpg)
imgInfo, _ := g.image.GetImageInfo(img, extension.Png)
if imgInfo == nil {
return nil, errors.New("could not read image options, maybe path/name is wrong")
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func (g *provider) getBarcodeImageName(code string, prop *props.Barcode) string

// loadImage is responsible for loading an codes
func (g *provider) loadCode(code, codeType string, generate func(code string) (*entity.Image, error)) (*entity.Image, error) {
image, err := g.cache.GetImage(codeType+code, extension.Jpg)
image, err := g.cache.GetImage(codeType+code, extension.Png)
if err != nil {
image, err = generate(code)
} else {
Expand Down
Loading

0 comments on commit 9c854d1

Please sign in to comment.