Skip to content

Commit

Permalink
Merge pull request #3 from ashleyprimo/v1.2.1-dev
Browse files Browse the repository at this point in the history
v1.2.1: Fix Scale + Render of QR codes
  • Loading branch information
ashleyprimo authored Jan 17, 2022
2 parents 026f5a4 + 5f0668c commit 501dd98
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
31 changes: 31 additions & 0 deletions qr/border.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package qr

import (
"github.com/boombuler/barcode"
"image"
"image/color"
)

type WhiteBorder struct {
width int
barcode barcode.Barcode
}

func (wb WhiteBorder) At(x, y int) color.Color {
bounds := wb.barcode.Bounds()
w := bounds.Dx()
h := bounds.Dy()
if x < wb.width || x >= w+wb.width || y < wb.width || y >= h+wb.width {
return color.White
}
return wb.barcode.At(x-wb.width, y-wb.width)
}

func (wb WhiteBorder) Bounds() image.Rectangle {
b := wb.barcode.Bounds()
return image.Rect(0, 0, b.Dx()+2*wb.width, b.Dy()+2*wb.width)
}

func (wb WhiteBorder) ColorModel() color.Model {
return wb.barcode.ColorModel()
}
17 changes: 9 additions & 8 deletions qr/engine.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package qr

import (
"fmt"
"net"
// "flag"
"bytes"
"fmt"
"image"
"image/png"
"net"
"net/http"
"net/url"
"strconv"

"github.com/ashleyprimo/go-qr-generator/initialize"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"

log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -119,18 +119,19 @@ func Engine(w http.ResponseWriter, r *http.Request) {
return
}

// Add border
var codeBordered image.Image = WhiteBorder{1, code}

// Scale the barcode to the appropriate size
code, err = barcode.Scale(code, sizeInt, sizeInt)
codeScaled, err := scale(codeBordered, uint(sizeInt))
if err != nil {
InternalServerError(w, r, "Unable to scale QR code.")
return
} else {
log.Debugf("Generated QR Code: %s", code)
}

// Encode PNG
buffer := new(bytes.Buffer)
if err := png.Encode(buffer, code); err != nil {
if err := png.Encode(buffer, codeScaled); err != nil {
InternalServerError(w, r, "Unable to encode PNG from code buffer.")
return
}
Expand Down
22 changes: 22 additions & 0 deletions qr/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package qr

import (
"errors"
"image"

"github.com/nfnt/resize"

log "github.com/sirupsen/logrus"
)

func scale(code image.Image, sizeInt uint) (image.Image, error) {
log.Debugf("Scaling Image")

// Resize 'image.Image' to requested size
img := resize.Resize(sizeInt, 0, code, resize.NearestNeighbor)
if img != nil {
return img, nil
} else {
return nil, errors.New("Failed to resize image")
}
}

0 comments on commit 501dd98

Please sign in to comment.