diff --git a/go.mod b/go.mod index b779aa9..b193f54 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2bfe4c9..89d963c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/qr/border.go b/qr/border.go new file mode 100644 index 0000000..26d4f31 --- /dev/null +++ b/qr/border.go @@ -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() +} diff --git a/qr/engine.go b/qr/engine.go index 6e82b02..bfde874 100644 --- a/qr/engine.go +++ b/qr/engine.go @@ -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" ) @@ -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 } diff --git a/qr/scale.go b/qr/scale.go new file mode 100644 index 0000000..abc1105 --- /dev/null +++ b/qr/scale.go @@ -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") + } +}