Golang : How to generate QR codes?
In this short tutorial, we will learn how to generate QR codes with the https://godoc.org/code.google.com/p/rsc/qr package. QR codes generation has many applications in the real world and the adoption is growing worldwide, especially on the online security and mobile application side. For mobile devices... beside keying data manually into the mobile phone, scanning QR code is the next most popular form of capturing text data.
Generating QR codes is pretty easy and straight forward with the https://godoc.org/code.google.com/p/rsc/qr package. Without much further ado, here is an example of generating QR code in Golang.
package main
import (
"bytes"
"code.google.com/p/rsc/qr"
"crypto/rand"
"fmt"
"image"
"image/png"
"os"
"runtime"
)
func randStr(strSize int, randType string) string {
var dictionary string
if randType == "alphanum" {
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
if randType == "alpha" {
dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
if randType == "number" {
dictionary = "0123456789"
}
var bytes = make([]byte, strSize)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
func main() {
// maximize CPU usage for maximum performance
runtime.GOMAXPROCS(runtime.NumCPU())
// generate a random string - preferably 6 or 8 characters
randomStr := randStr(6, "alphanum")
fmt.Println(randomStr)
// generate the link or any data you want to
// encode into QR codes
// in this example, we use an example of making purchase by QR code.
// Replace the stuff2buy with yours.
stuff2buy := "stuffpurchaseby?userid=" + randomStr + "&issuer=SomeBigSuperMarket"
// Encode stuff2buy to QR codes
// qr.H = 65% redundant level
// see https://godoc.org/code.google.com/p/rsc/qr#Level
code, err := qr.Encode(stuff2buy, qr.H)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
imgByte := code.PNG()
// convert byte to image for saving to file
img, _, _ := image.Decode(bytes.NewReader(imgByte))
//save the imgByte to file
out, err := os.Create("./QRImg.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = png.Encode(out, img)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("QR code generated and saved to QRimg1.png")
}
Sample output :
NOTE : The QR codes generated by this tutorial is not compatible with Google Authenticator. If you are looking to generate QR codes for two factors authentication purpose with Google Authenticator, you need to use follow the URI format specify in https://github.com/google/google-authenticator/wiki/Key-Uri-Format to generate the QR codes.
See : QR codes for Google Authenticator tutorial
References :
http://blog.gopheracademy.com/advent-2013/day-21-two-factor-auth/
See also : Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12.8k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+12.9k Golang : Listen and Serve on sub domain example
+16.7k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+5.9k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+31.1k Golang : Interpolating or substituting variables in string examples
+13.6k Golang : Increment string example
+7.1k Golang : How to setup a disk space used monitoring service with Telegram bot
+8.5k Golang : Convert word to its plural form example
+19.3k Mac OSX : Homebrew and Golang
+7.5k Golang : How to convert strange string to JSON with json.MarshalIndent
+25.4k Golang : Storing cookies in http.CookieJar example
+18.7k Golang : Send email with attachment