Creating and reading QR codes

KDE Applications

KDE users have an easy way to convert text into QR code: Copy some text to the clipboard, click the Klipper tool (the scissors) in the system section of the control bar at the bottom, and select Show Barcode. Unfortunately, the interface does not let you save the QR code shown on the left side (Figure 6) or copy it to the clipboard. Your only workaround is to grab a screenshot with KSnapshot. For a single QR code this will still be faster than installing one of the other tools.

Figure 6: The KDE Klipper tool, which manages the Clipboard, can produce QR code on the fly.

A possible alternative is the slightly older KBarcode4-light. To use it, first install PyQt via the package manager. On openSUSE 12.3 and Ubuntu, it resides in the python-qt4 package. Next, unpack the KBarcode4-light archive, which you downloaded off the web [7]. In the src subdirectory, run the python command.

In the window that now appears (Figure 7), select the QR Code item in the Encoding drop-down, then type the text to encode in Value, and press Generate. The resulting QR code appears at the bottom. Pressing Save as PDF lets you save the code in a text file.

Figure 7: KBarcode4-light offers less functionality than Qrencode.

Theoretically, it's possible to export the QR code as an image file, but clicking Save on modern distributions only leads to an error message in the terminal. Another alternative is to click Copy and then paste the Clipboard contents into another program. The options offered in the Basic Options and Extra Options tabs are useless for QR codes.


Although dozens of apps are out there for reading QR codes with smartphones, only a few fairly rudimentary programs exist for Linux. If you have QtQR installed, press Decode in the bottom right corner of the main window. The program either accepts the QR code as an image file (Decode from File), or reads it via a webcam (Decode from Webcam).

However, QtQR is quite picky: For example, it does not interpret QR codes for dots of exactly one pixel. Additionally, the program stubbornly insists that the text contains ISO-8859-1 character encoding. If you have a QR code image file, one option is to use the Libdecodeqr-simpletest program, which is actually only intended as an example program for programmers on how to read codes in their own programs with the Libdecodeqr library. However, it is excellent for reading codes at the command line.

On Ubuntu, you can install the reader from the libdecodeqr-examples package with:

sudo apt-get install libdecodeqr-examples

Owners of other distros first need to install a C++ compiler, make, and developer files from OpenCV (on openSUSE, in the opencv-devel package). Then, use the archive of the current version [8] from the Latest Stable Source Code section. Extract it on your hard drive, and in the resulting src subdirectory, enter the following three commands: ./configure, make and sudo make install.

As soon as the program exists on your hard drive, you can decipher QR code in the qrcode.png file with the following command:

$ libdecodeqr-simpletest qrcode.png

… at least in theory; the practical results will be disappointing in many cases. In my tests, libdecodeqr-simpletest simply refused to see any data in the QR codes (Figure 8).

Figure 8: libdecodeqr-simpletest identifies the QR code but does not return the text.

Touch the Bar!

A much better alternative is ZBar [9]. This program reads QR codes from files or via a connected webcam. Ubuntu includes the small tool in the zbar-tools package. On other distributions, you first need to run the package manager to install the developer packages for Qt4, PyGTK and ImageMagick. On openSUSE 12.3, they are named libqt4-devel, python-gtk-devel, and ImageMagick-devel.

If you want to read the QR code with the webcam, you additionally need the kernel header files; on openSUSE 12.3, they are located in the kernel-devel package. If you want to do without this function, you must use the --disable-video configuration parameter to disable it explicitly. After unpacking the source code, you can install the software with ./configure && make && sudo make install. To decode a PNG file with a QR code in ZBar, use:

zbarimg -d <file>.png

The -d parameter ensures that a window with the QR code appears (Figure 9) in addition to the decoded information in the terminal window. If you want to read a QR code with a camera, you need to launch zbarcam. This pops up a preview window with the image from the webcam. Now hold up the QR code to the camera. Once the program has detected the code, it draws a green border around the QR code and prints the data in the terminal window (see the Big and Colorful" box).

Figure 9: ZBar delivers all stored address data and even detects the vCard format used; unfortunately, it does not fully understand UTF-8.

Big and Colorful

Whether a webcam or smartphone can recognize a QR code depends on the quality of the built-in camera. To make sure even models with bad sensors can detect all the dots, you should not print the code too small. Generally, the image should be not less than 2.5 by 2.5 centimeters. To achieve this, simply enlarge the generated QR code with an image editing program, but make sure the software does not modify image sharpness. In GIMP, choose Image | Scale image, and then in the new window, say No to Interpolation.

Most QR codes are black and white to achieve the best possible contrast, but in principle, nothing is wrong with coloring the pixels, although you run the risk that some smartphones would not be able to distinguish individual pixels. Testing sample code with two or more different, older smartphones would give you a clue as to the quality of the results.

You can disable the preview with the --nodisplay switch; raw delivers the raw data, and xml packages the data in an XML format. The above three parameters help if you want to integrate the tool into a script. The parameters --raw and --xml can also be used with zbarimg.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More