X
Popular Searches

How To Create QR Codes From The Linux Command Line

Shutterstock/shisu_ka

QR Codes can do a whole lot more than just take you to a web site. And they’re easy to create on the Linux command line too. We show you how.

QR Codes

Quick response (QR) codes are enjoying something of a resurgence, thanks to their use in the COVID-19 track and trace apps in many countries. They are a convenient way to share information and to elicit a response from people. Everyone carries a cellphone, so we all carry a suitable scanner with us. There are many free QR and barcode scanning software apps, so there is no extra hardware required and no outlay for the software.

Since its invention in 1994 by a highly commended team at Denso Wave, a subsidiary of Toyota, the QR code has found many new uses.

The QR code is capable of holding much more information than a barcode, and because of its design, it is possible for the scanner to work out which way up the QR code is, and to automatically orient the image when it is read and decoded.

These characteristics are what made it such a success in its original role of tracking inventories, assemblies, and components in factory production facilities. It doesn’t matter if the QR code or the item it is attached to isn’t perfectly aligned when it rolls by on a conveyor belt. The QR scanner can detect and read the code regardless of its orientation.

Why not use an online QR code generator? There are plenty out there, but many of them stipulate they are allowed to retain and store the information you provide them, and they can do what they want with the data. if you’re going to be creating QR codes with personal or sensitive information in them, such as contact details or Wi-Fi connection details, this poses a significant security risk.

All your data stays within your control if you use a command-line tool like qrencode. It also allows you to create scripts and aliases to create QR codes programmatically.

RELATED: Why Threats from QR Codes are Flourishing

Uses for QR Codes

Outside of factories and production environments the QR is probably most often used on promotional literature such as flyers, pamphlets, and posters. They’re also often seen on product packaging. In these scenarios, the QR code typically contains a Uniform Resource Locator (URL) or web address. The scanning app opens the website for the user.

When you scan a QR code with your cellphone it detects the type of data that has been encoded and offers to perform an appropriate action. For example, if the data is a calendar event you’ll be asked if you want to add the event to your calendar.

The actions that can be triggered by a QR code include:

  • Displaying text.
  • Navigating to a point on a map.
  • Making a phone call.
  • Sending an SMS message.
  • Sending an email.
  • Opening a URL, to display a webpage or download a file.
  • Adding a contact to your address book.
  • Adding an event to your calendar.
  • Connecting to a Wi-Fi network.

Installing qrencode

qrencode is a versatile, fast, and richly-featured QR code generator. It provides a library that can be linked into your own programs to encapsulate the QR generation technology into your own applications. It also provides a command-line utility that we’re going to make use of.

To install qrencode on Ubuntu, we type this:

sudo apt-get install qrencode

To install qrencode on Fedora, use this command:

sudo dnf install qrencode

On Manjaro we use pacman:

sudo pacman -Sy qrencode

The qrencode Parameters

There are two distinct sets of parameters that we will need to pass to qrencode. One set relates to the creation and encoding of the QR codes, and the other specifies the data that we wish the QR code to contain.

The parameters that affect the creation of the QR code decide such things as the size of the QR code, the color of the checkered pattern and the background, and the error-correction settings.

The purpose of the QR code—whether it sets up an SMS message for example, or offers to add a contact to your address book—is decided by the information inside the QR code. It isn’t dependent on the way the QR code was created. Keywords and field names identify the type of encoded content and allow the scanning software to identify what it should do with the retrieved information.

Using qrencode

Plain Text QR Codes

The simplest form of QR code contains plain text. It can be used to pass any text-based information to the end user. The qrencode parameters we’re using are:

  • -s 6: This sets the size of the blocks in the QR checkered pattern. The number you provide sets the size in pixels for the length of one side of a block.
  • -l H: This sets the level of error-correction. The options we can use are “L” for low, “M” for medium, “Q” for quite high, and “H” for high or highest. The different levels of error-correction can cope with different amounts of damage or illegibility when the QR code is scanned. “Low” allows the code to be successfully scanned if up to seven percent of the code is obscured or damaged. “Medium” raises this to 15 percent. “Quite high” can cope with up to 25 percent of the code being damaged. The “High” level can cope with a whopping 30 percent of damage or illegibility when the QR code is scanned. The more error correction you use, the larger the overall QR code will be. This is because it needs to store redundant information which means there are more blocks in the pattern.
  • -o “text.png”: The output option allows you to set the name of the generated image. By default, this is a Portable Network Graphics (PNG) image. If you use the -t (type) option you can change the output to a variety of formats including Encapsulated Postcript (EPS), Scalable Vector Graphics (SVG), and many different flavors of American National Standard Institute (ANSI) character-based output.

The text to be encoded is provided last, and wrapped in quotation marks.

qrencode -s 6 -l H -o "text.png" "This type of QR holds plain text. This text is shown to the user when they scan the QR code. No other action is automatically triggered."

This command creates this QR code:

A plain text QR code

When it is scanned the text is extracted and displayed in the scanning software.

Scan results for a plain text QR code

The scanning app will provide some suggestions that you might want to do. In this case—in this particular app—you can perform a web search for the retrieved text or choose to share the text with others.

Adding Colour

The --foreground and --background options let you set colors for the QR code pattern and background. You can specify the color in the RGB color model six hexadecimal digit notation or the RGBA color model eight hexadecimal digit notation.

We’re using the RGB notation. There are convenient tables to look up color codes on the web. The foreground will be a shade of blue and the background will be white.

qrencode -s 6 -l H --foregound="3599FE" --background="FFFFFF" -o "blue.png" "This QR code will be blue and white."

The QR code is generated in our selected shade of blue.

A QR code in blue and white

It scans just like any other QR code. If you do change the colors ensure you have enough contrast between the foreground and background so that scanning apps can pick out the checkered pattern reliably.

Scan results for a colored QR code

Storing a Location in a QR Code

You can store the location of something within a QR code easily.

  • The information we store must start with the keyword geo.
  • Use a colon “:” to separate the keyword from the location data.
  • The location must be specified in digital degrees.
  • Don’t leave any spaces in the entire string, and use a comma “,” between the latitude and longitude values.

The qrencode options are the same as we’ve used before. We’re changing the purpose of the QR code simply by changing the information stored inside it.

qrencode -s 6 -l H -o "geo.png" "geo:51.185013,-1.859105"

This produces this QR Code. The location is the entrance to the car park at Stonehenge.

A location QR code

When it is scanned the user is offered the chance to look at the location on a map or to get directions to the location.

Scan results for a location QR code

Storing a Phone Number in a QR Code

To store a phone number inside a QR code use the tel keyword, a colon “:“, and the phone number, with no spaces. It’s best not to use hyphens and other punctuation in the number because some scanning apps get confused. Instead of “(123) 456–7890” use “1234567890.”

qrencode -s 6 -l H -o "phone.png" "tel:5555555555"

This is the QR code generated by that command:

A phone number QR code

When it is scanned the app offers to add this as a number as a contact—you’ll need to provide the name yourself—or to ring the number for you.

Scan results for a phone number QR code

Sending an Email From a QR Code

You can add information to a QR code so that the scanning app offers to send an email. You can provide the destination email address, the email subject, and the body of the email.

qrencode -s 6 -l H -o "email.png" "mailto:contact@cloudsavvyit.com?subject=Linux&body=Linux articles are cool."

  • The keyword to use is mailto, with a colon ” : ” separating it from the email address.
  • Use “?subject=” to identify the subject line. Don’t put spaces before or after the “?subject=” flag.
  • Use “&body=” to identify the email message contents. Don’t put spaces before or after the “&body=” flag.

This is the resulting QR code.

An email QR code

When this is scanned the app offers to send the email or to add the email address as a contact.

Scan results for an email QR code

A Website QR Code

To direct the user to a website or other URL, use the http or https keyword, a colon ” : “, and the web address with no spaces.

qrencode -s 6 -l H -o "URL.png" "https://cloudsavvyit.com"

This command creates this QR code.

When the code is scanned the app offers to open the link.

Scan results for a URL QR code

Sending an SMS or MMS Message From a QR Code

The formats to send a Short Message Service (SMS) or a Multimedia Messaging Service (MMS) message are identical. Use the keyword smsto keyword, a colon ” : “, the phone number, a comma “,” with no spaces, then the body of the message.


qrencode -s 6 -l H -o "SMS.png" "smsto:5555555555,This is the text of the SMS message."

That command produces this QR code:

An SMS QR code

When it is scanned the user has the option to send either an SMS or an MMS message.

Scan results for an SMS QR code

Connect to a Wi-Fi Network from a QR Code

It’s possible to encode the details of a Wi-Fi network in a QR code. Visitors can scan the QR code to easily connect to your guest Wi-Fi.

The keyword is WIFI, followed by a colon ” : ” and several other parameters.

  • T: The type of security. This can be WEP, WPA, or WPA2. Note there is a colon between the “T” and the security value, and there is a semicolon ” ; ” after the security setting.
  • S: The Service Set ID (SSID) of the Wi-Fi network. Note there is a colon between the “S” and the SSID, and there is a semicolon ” ; ” after the SSID.
  • P: The password or security key of the Wi-Fi network. Note there is a colon between the “P” and the security key, and are two semicolons ” ;; ” after the security key.
qrencode -s 6 -l H -o "wifi.png" "WIFI:T:WPA;S:<SSID>;P:<PSWD>;;"

Put your real SSID and security key where <SSID> and <>PSWD> appear in the command. The QR code looks like this:

A Wi-Fi QR code

When the QR code is scanned, the app offers to connect to the Wi-Fi network or to copy the password to the clipboard.

Scan results for a Wi-Fi QR code

Adding a Contact With a QR Code

Contact details in the vCard format can be added to a QR code and scanned into the user’s address book. This removes the risk of mistyping something. There’s a lot of information that can be stored, so it is best to add the details to a file and to redirect that file into qrencode.

This text was added to a file called “contact.txt.”

BEGIN:VCARD
VERSION:2.1
N:McKay,D
FN:Dave McKay
TEL;TYPE=voice,cell,pref:5555555555
TITLE:Journalist
ORG:CloudSavvyIT
EMAIL:dave@madeupdomain.com
URL:www.cloudsavvyit.com
END:VCARD

We can redirect this file into qrencode like this:

qrencode -s 6 -l H -o "contact.png" < contact.txt

The QR code it produces has many more blocks in the code pattern than we’ve seen so far. This is because we have more information encoded in the pattern and we’re using the highest level of error-correction.

A contact details QR code

As you’d expect, the scanning app offers to add this as a contact. But because it has detected there is a phone number in the information it also offers to ring the number. Similarly, it has also detected the email address and it offers to send an email.

Scan results for a contact details QR code

Adding a Calendar Event With a QR Code

Adding an event to a calendar is very similar to adding a contact. The data is best added to a file which is redirected into qrencode.

This text was added to a file called “event.txt.”

BEGIN:VEVENT
SUMMARY:Works Picnic
DESCRIPTION:Fun, food, and games in the sun
LOCATION:Eastwood Park
DTSTART:20210710T103000
DTEND:20210710T154500
END:VEVENT

This was redirected into qrencode like this:

qrencode -s 6 -l H -o "event.png" < event.txt

This is the QR containing the calendar event:

A calendar event QR code

The scanning app decodes the event like this:

Scan results for a calendar event QR code

The user can add the event to their calendar with a tap on the “Add event” icon.

So Many Uses

QR codes can be placed anywhere: in email signatures, on promotional literature, on posters, flyers, as stickers on hardware, on websites, on product packaging, on product labels, and on and on.

They can deliver all sorts of information or functionality. A QR code on a sticker attached to a piece of hardware could open the latest version of the service manual or user guide.

A poster could feature a QR code to add the event to your calendar, and another QR code to give you directions to the venue.

All it takes is a little creativity, some know-how, and qrencode.

Dave McKay Dave McKay
Dave McKay first used computers in the industry when punched paper tape was in vogue and he has been programming ever since. His use of computers pre-dates the birth of the PC and the public release of Unix. He has programmed in everything from 6502 assembly to Lisp, and from Forth to C#. He is now a technology journalist and independent Data Protection and Compliance consultant. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.