DEV Community

donvitocodes
donvitocodes

Posted on

Generate a QRCode vCard using Go

Every time I receive a business card, I always check if there is a QR code in it. QR codes are very useful since most mobile phone cameras nowadays can automatically recognise QR codes. Once a QR code which contains vCard data is detected, phones automatically launch "Add Contact" with the details read from the QR code. So all you have to do is just tap "Save" to add the new contact. There is no need to fill in your contacts' information from the business card. This saves a few seconds of our lives. Who wants filling in data? :)

I've always been curious how to create these QR codes which contain vCard data so I searched on how to generate them using Go. So after a few minutes, I've come up with a solution using an existing library called go-qrcode. The solution is pretty simple using go-qrcode.

Below is the full source code of the example. Since Go is very straightforward, it just takes a few lines to do it. The example reads data from a vcard.txt file and the vcard data is passed into the qrcode library which generates the qrcode image with the encoded vCard data.

package main

import (
    "io/ioutil"
    "log"
    "os"

    qrcode "github.com/skip2/go-qrcode"
)

func main() {

    file, err := os.Open("vcard.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    b, err := ioutil.ReadAll(file)
    if err != nil {
        log.Fatal(err)
    }

    str := string(b)
    qrFname := "vcard-qr.png"

    err = qrcode.WriteFile(str, qrcode.Medium, 256, qrFname)
    if err != nil {
        log.Fatal(err)
    }

}
Enter fullscreen mode Exit fullscreen mode

You can use the ZXing generator to generate the vCard data. Below is sample content of vcard.txt

BEGIN:VCARD
VERSION:3.0
N:Melvin Vivas
ORG:Freelance
TITLE:Senior Software Engineer
TEL:+1234567
URL:https://www.melvinvivas.com
EMAIL:melvindave@gmail.com
ADR:Singapore
END:VCARD
Enter fullscreen mode Exit fullscreen mode

To run the example, you need to first download go-qrcode.

Then, just do the usual, run main.go

$ cd src
$ go run main.go
Enter fullscreen mode Exit fullscreen mode

After running the application, this will generate a vcard-qr.png image file in the same directory as your application.

Alt Text

That's it! Hope you find the example useful! Source code for this example is in my github repo.

GitHub logo donvito / go-qrcode-vcard-example

Here is an example to generate qrcode containing vcard data which can be used in business cards.

go-qrcode-vcard-example

Download go-qrcode library so we can use in our code

go get -u github.com/skip2/go-qrcode/...

In our code, we imported the go-qrcode library

import qrcode "github.com/skip2/go-qrcode"
  • Example code to read vcard data from a file and produce a qrcode image

     func main() {
    
        file, err := os.Open("vcard.txt")
        if err != nil {
          log.Fatal(err)
        }
        defer file.Close()
    
        b, err := ioutil.ReadAll(file)
        if err != nil {
          log.Fatal(err)
        }
    
        str := string(b)
        qrFname := "vcard-qr.png"
    
        err = qrcode.WriteFile(str, qrcode.Medium, 256, qrFname)
        if err != nil {
          log.Fatal(err)
        }
    
    }
    

Here is sample data which you can use for vcard.txt

BEGIN:VCARD
VERSION:3.0
N:name
ORG:company
TITLE:title
TEL:123
URL:https://www.website.com
EMAIL:test@email.com
ADR:address1 address2
NOTE:memo
END:VCARD

References

Blog reposted from my Tech Blog
https://www.melvinvivas.com

By the way, I'm looking for some Go-related development work. Ping me if you have any requirement. :) http://www.melvinvivas.com/looking-for-remote-software-development-work-outsource

Top comments (0)