Skip to content

This is a small repository showing how to generate a QR code with an optional watermark in Go

Notifications You must be signed in to change notification settings

javdl/go-qr-code-generator

 
 

Repository files navigation

QR code generator in Go

FOSSA Status

This is a small repository showing how to generate a QR code with an optional watermark in Go.

Credits to Matthew Setter, his blogpost "How to Generate a QR Code with Go" explains this code. Note: the blogpost mentions content= instead of url= to use for the content you want to encode in the QR code.

Prerequisites

To follow along with the tutorial, you don't need much, just the following things:

  • Go (a recent version, or the latest, 1.20.5)
  • Curl or Postman
  • A smartphone with a QR code scanner (which, these days, most of them should have)

Start the API

go run main.go

Test if the API works

curl -i -X POST http://localhost:8080/generate

Create a QR code

curl -X POST \
    --form "size=256" \
    --form "url=https://fashionunited.com" \
    --output data/qrcode.png \
    http://localhost:8080/generate

You can also watermark the QR code, by uploading a PNG file using the watermark POST variable. Below is an example of how to do so with curl.

curl -X POST \
    --form "size=256" \
    --form "url=https://fashionunited.com" \
    --form "watermark=@data/twilio-logo.png" \
    --output data/qrcode.png \
    http://localhost:8080/generate

Create multiple QR codes

Put a list of urls in example.txt. Make sure there is a newline at the end.

./generate.sh

Deploy to Google Cloud Run

gcloud config set project PROJECT_ID
gcloud run deploy --region=europe-west1 --allow-unauthenticated

Get the service URL without deploying

gcloud run services describe go-qr-code-generator --platform managed --region europe-west1 --format 'value(status.url)'

Test if the API works

curl -X POST \
    --form "size=256" \
    --form "url=https://fashionunited.nl/modevacatures/werken-bij/ray-ban-vacatures/search/in/rotterdam" \
    --output data/ray-ban-rotterdam.png \
    https://go-qr-code-generator-XXXXXXXXXX.a.run.app/generate

CLI tooling

Local development

  1. Set Project Id:

    export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>
    # in this case:
    export GOOGLE_CLOUD_PROJECT=go-qr-code-generator

License

FOSSA Status

About

This is a small repository showing how to generate a QR code with an optional watermark in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 61.5%
  • Dockerfile 23.3%
  • Shell 15.2%