How to Create and Read QR Codes on the Terminal

Hisham recently said something very true:

Remember when we all silently told ourselves “stop trying to make QR Codes happen, it will never catch on”?

And then the one and only Miguel de Icaza replied

I do, and we laughed and laughed, and now it turns out, they are quite useful :-)

Indeed, what seemed just another useless gimmick 15 years ago is now a part of everyday culture: there are QR codes to connect to Wi-Fi networks, there are QR codes to scan on advertising and even on movies, there are QR codes to set Two-Factor Authentication on your authenticator app, your bus, train, or concert tickets are QR codes, and so much more. They are everywhere.

There are lots of websites out there that allow you to create QR codes, but how can you create QR codes privately, on your own computer, without having to use third-party websites?

Encoding

The qrencode command, based on libqrencode, can be used to encode any kind of data into a nice, standard QR code.

qrencode can generate various formats (PNG, plain text, SVG, etc) and this is how to use it:

$ sudo dnf install qrencode # Fedora
$ sudo apt-get install qrencode # Debian, Ubuntu
$ qrencode -o qrcode.png -s 10 "Remember when we all silently told ourselves 'stop trying to make QR Codes happen, it will never catch on'?"

And this is the qrcode.png file generated by the tool:

You might want to man qrencode to learn more about it.

Decoding

You can also read and decode QR codes from an image using the ZBar project. You can use the zbarimg tool to read the previous image as follows:

$ sudo dnf install zbar # Fedora
$ sudo apt-get install zbar-tools # Debian, Ubuntu
$ zbarimg qrcode.png
QR-Code:Remember when we all silently told ourselves 'stop trying to make QR Codes happen, it will never catch on'?
scanned 1 barcode symbols from 1 images in 0.04 seconds

Again, man zbarimg contains lots of information and options.

Bonustrack 1: QR code with your Wi-Fi network details

The Wikipedia page about QR Codes explains the structure of the text string required to help people join a Wi-Fi network just by using the camera in their smartphone; just use the format below as input text for qrencode and you’ll be good to go.

$ qrencode -o wifi.png -s 10 "WIFI:S:MySSID;T:WPA;P:MyPassW0rd;;"`
$ zbarimg wifi.png
QR-Code:WIFI:S:MySSID;T:WPA;P:MyPassW0rd;;
scanned 1 barcode symbols from 1 images in 0.03 seconds

And this is the QR code you’ll get:

If you scan the image above, your mobile device will ask you if you’d like to join the (a priori non-existent) “MySSID” network.

There’s a lot more you can do with QR codes; learn more in this How-To Geek article.

Bonustrack 2: Generic barcodes

If, instead of QR codes, you want barcodes like the ones you see on supermarkets, a good option is GNU Barcode from the Free Software Foundation.

As an example, here’s the command to generate a file named test.pdf with 10 codes inside:

$ sudo dnf install barcode # Fedora
$ sudo apt-get install barcode # Debian, Ubuntu
$ barcode -i numbers.txt -e ean13 -u mm -g 80x30 \
          -m '10,15' -t '2x5' -o test.pdf

(As usual, man barcode is your friend to understand the various switches available.)

Input file named numbers.txt contains series of numbers (which, for EAN-13 barcodes, they must be exactly 12 characters long):

123456789012
123456789035
123456789086
123456789047
123456788943
123456788925
123456788863
123456788961
123456788964
123456788954

The command above will generate a PDF file looking like this: