QR Codes are a really easy way to share information between your desktop or laptop and a smartphone, which don’t have the privacy issues inherent in Google’s Chrome To Phone. The excellent FOSS Barcode Scanner app for Android makes scanning these codes and opening the scanned data a breeze. However, generating them on the desktop can be a bit of a pain. There are several websites, browser addons and APIs for generating these codes, but up until now I haven’t found anything really quick and easy.
That is until today, when I discovered the ‘qrencode’ utility. Qrencode does exactly what it says on the tin. It takes some text, encodes it as a QR Code and writes the result as a PNG file. It’s a simple command line tool, which opens up awesome scripting possibilities.
My use case is simply to send the URL of a web page that I’m viewing on my desktop to my phone, via QR Code. I found that qrencode was quite happy to write its output to stdout, which meant I could display the result directly using ImageMagick’s ‘display’ command, e.g:
$ qrencode http://blog.webworxshop.com -o - | display
You’ll notice that by default the QR Code is quite small. We can fix this by increasing the block size:
$ qrencode http://blog.webworxshop.com -s 10 -o - | display
Next, I wanted to automatically pull the text to encode from somewhere to save typing – the clipboard was an ideal candidate. Enter ‘xclip’. Xclip is a command line utility to read and write from/to the X system’s built in clipboard. I used bash’s backtick command substitution to grab text from the clipboard and encode it:
$ qrencode `xclip -o` -s 10 -o - | display
And there you have it, a simple one line command to generate and display a QR Code from the contents of the clipboard. I created a bash script containing the command and assigned it to a keyboard shortcut in Gnome (Ctrl-Shift-Q), so that sharing URLs to my phone is as simple as selecting the text in the location bar and hitting Ctrl-C followed by Ctrl-Shift-Q.
If you want to give this a try, you’ll need to install the utilities discussed, in Fedora these can be installed with:
$ sudo yum install qrencode xclip ImageMagick
4 thoughts on “Quick QR Code Generation”
works great! I use Ubuntu so I just did a
sudo apt-get install qrencode xclip imagemagick
and then your examples worked great. I’m going to try assigning the script to one of my extra keys on my keyboard.
Excellent, glad it was useful.
This is extremely helpful! Thank you so much for posting it!