A QR (Quick Response) code is a grid of dark and light dots that can hold up to 7,089 characters. These codes are widely used for URL sharing, personal identification, passes, ticketing, geolocation, contact information, WiFi network configuration, and other purposes that require access to data with a simple scan.
In this tutorial, we’ll see how to read a QR code from a webcam or a file using some Linux command-line and graphical open-source tools. As an example, we’ll use this downloadable QR code, which contains a link to the Baeldung website:
2. Command-Line Tools
Here we’ll see how to use the Linux terminal to recognize and decode QR codes from images or video streams.
2.1. zbarimg & zbarcam
First, we need to install ZBar Bar Code Reader, a software suite that includes the two utilities we’re interested in:
$ sudo apt install zbar-tools
zbarimg takes an image file as input and outputs the decoded data to standard output or to a file:
$ zbarimg Baeldung-website-QR-code.png
scanned 1 barcode symbols from 1 images in 0,05 seconds
If we’re interested in clean output, i.e., without any extra information beyond the decoded string, we can use the -q and –raw options:
$ zbarimg -q --raw Baeldung-website-QR-code.png
zbarcam captures frames from a video device and scans them for QR codes, displaying the results on the screen or sending them to another program. While framing a QR code, let’s observe the terminal:
The Examples section of the man page shows how to pipe the resulting data through a script. It’s also possible to disable the preview window with the –nodisplay option, but this has an unfixed bug at the moment.
2.2. QR Scanner CLI
QR Scanner CLI is another solution for decoding QR codes stored in image files.
For the installation, we need to use npm, the Node Package Manager, which is the default tool to manage Node.js packages. The -g flag specifies that the software should be installed globally, making its command-line tool available to all users:
$ sudo npm i -g qr-scanner-cli
After that, it’s easy to use:
$ qrscanner ./Baeldung-website-QR-code.png
║ https://www.baeldung.com/ ║
To get clean output, we can use the –clear option:
$ qrscanner --clear ./Baeldung-website-QR-code.png
We can also take a look at the –clipboard option to copy the decoded string to the clipboard. This is interesting because, in Linux, we can automate the use of the clipboard using various command-line tools and scripting languages.
3. GUI Tools
Now let’s look at some more user-friendly utilities.
CoBang allows us to decode QR codes from both local and remote pictures and webcams. Its documentation includes installation instructions for some popular distributions, as well as Flatpak support in all other cases:
$ sudo add-apt-repository ppa:ng-hong-quan/ppa
$ sudo apt update
$ sudo apt install cobang
Using the webcam is pretty intuitive:
3.2. QR Code Reader (Firefox and Chrome)
As noted in the FAQ, this extension only works locally and doesn’t perform any server-side interaction.
QtQR is first and foremost a QR code generator with a clear and clean interface. Let’s install it:
$ sudo apt install qtqr
The options we’re interested in are Decode from File and Decode from Webcam:
In this article, we’ve seen some command-line and graphical tools for decoding QR codes on Linux:
- zbarimg & zbarcam
- QR Scanner CLI
- QR Code Reader extension for Firefox and Chrome
The choice of tool depends on our preferences. Command-line tools are especially good for automated workflows.