QR codes inside the terminal

QR codes inside the terminal

Nowadays most terminal emulators can display color emojis, so why not QR codes as well ? This makes for a simple copy/paste across devices.

Generating QR codes is rather simple, and doesn't require any GUI: you can output the QR Code with ASCII drawing characters, and while they aren't exactly square, they still work.

Scanning/flashing QR codes is a bit more involved as it required webcam access. Command-line software can access the webcam without any issue, but a webcam preview can help you position the QR code in front of the webcam.

Install

To generate QR codes, install zbar-tools and qrencode (ubuntu) from your package manager. Beware of internet downloads for zbar, as the official repo was on Google Code and not Github, so many people have forked it everywhere. That package provides you with many utilities such as zbarcam and zbarimg.

To scan QR codes, you can use zbarcam (but it lanches a graphical preview window), or install https://github.com/sayanarijit/qrscan.

qrscan can display the webcam output inside your terminal, with ASCII drawing characters (in color !), similar to libcaca0 in ffmpeg.

FYI, libcaca comes from a french slang for "poop" ...
GitHub - sayanarijit/qrscan: Scan a QR code in the terminal using the system camera or a given image
Scan a QR code in the terminal using the system camera or a given image - GitHub - sayanarijit/qrscan: Scan a QR code in the terminal using the system camera or a given image

Generate QR code

(this may not render correctly in your browser, but it works in terminals)

$ qrencode -t ANSI256UTF8 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&pp=ygUIcmlja3JvbGw%3D'
█████████████████████████████████████████
█████████████████████████████████████████
████ ▄▄▄▄▄ █   █▄▄ ▄▄▀▄▀▄▀█▄▀█ ▄▄▄▄▄ ████
████ █   █ █ ▀▄ ██▀▀█▄▀█▀▄█  █ █   █ ████
████ █▄▄▄█ █▀██▀▀▄▄▄▄█▀ █▄█▄██ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀▄█ █ █ ▀▄▀▄▀ ▀▄█▄▄▄▄▄▄▄████
████  ██▄▄▄▀█▀▀▄▀▀█▄▀▄█▄▄ ▀▀▄██ █▄ ▄▄████
████▄▄█ ▄█▄▀▄█▀ ▄▄ ▄▀ ███▀█▄ ▄██ ▀▄▄█████
████▀  █▀ ▄ ██▄█▄▀ ▀▄ ▄▀█▄ ▀██▄ ▄▄█▄▄████
████▄▀█▄ ▀▄█ ▄▄█▀█  ▀  ▀ ▀▀▄  ███ ▄▄█████
█████  ██▀▄▄▀▄ ▄▀▀▄▀▀ █▄▄ ▀  ▀▄▀▄▀██▄████
████▄█▀██ ▄▄▄ ▄ ▄▄▀▄  ▀▀  █  █▀▀▄▀▄▄█████
████▀██▀▀ ▄▄ ███▄ ▀ ▀▄▀ █▄▄ ▄▀▄ ▄▀██▄████
██████ ▄▀ ▄▄ ▄▀█▀█▄ ▄▀ █▄ ▀▄  █▄██▄▄█████
████▄▄█▄▄▄▄█▀▄▄▄▀▄  ▀███▀█ ▄ ▄▄▄  ▀ █████
████ ▄▄▄▄▄ █▀▄  ▄ ▄ █  ▀  ▄█ █▄█ █▄ █████
████ █   █ █▄█▀█▄ ▀ ▀▀▀▄█▄ ▀▄  ▄  ▀  ████
████ █▄▄▄█ █▀█▄█▀█▀▄▀ ▀█▀▀█ ▀▀ ▄▄█▄▄█████
████▄▄▄▄▄▄▄█▄█▄▄█▄▄██▄█▄█▄█▄▄▄█▄█▄██▄████
█████████████████████████████████████████
█████████████████████████████████████████

ANSI256UTF8 is one of the many modes that zbar's qrencode supports. It means 256 colors mode, not inverted. It will render as black and white regardless of you terminal's color scheme.

ANSI256 is similar but the output is 4 times bigger, so it can quickly overflow out of your screen.

Scan QR code

qrscan --preview --metadata --qr
# or
zbarcam --oneshot --raw

qrscan will display a color preview and also print back the received QR code alongside its data.

zbarcam is a bit faster, and the preview is graphical, so more pixels :).

If you don't include --raw, zbarcam will prepend QR-Code: to the data (the barcode type, since it supports not only QR codes, but also EAN-13, EAN-8, DataBar, DataBar Expanded, Code 128, Code 93, Code 39, Codabar).