Skip to content

Instantly share code, notes, and snippets.

@lpsmith
Created August 29, 2017 06:33
Show Gist options
  • Save lpsmith/231f219fa21e756ea686283043ac3d10 to your computer and use it in GitHub Desktop.
Save lpsmith/231f219fa21e756ea686283043ac3d10 to your computer and use it in GitHub Desktop.
Bash scripts for working with qr codes
#!/bin/bash
DECODEQR_TMPDIR=$(mktemp --tmpdir=$TMPDIR -d)
if [ $? -eq 0 -a ! -z "$DECODEQR_TMPDIR" ]; then
#mkfifo $DECODEQR_TMPDIR/tmp.png
gnome-screenshot -f $DECODEQR_TMPDIR/tmp.png
zbarimg --quiet $DECODEQR_TMPDIR/tmp.png
rm -rf $DECODEQR_TMPDIR
fi
#!/bin/bash
ENCODEQR_TEMPDIR=$(mktemp --tmpdir=$TMPDIR -d)
if [ $? -eq 0 -a ! -z $ENCODEQR_TEMPDIR ]; then
if [ $# -eq 0 ]; then
qrencode -o $ENCODEQR_TEMPDIR/tmpqrcode.png < /dev/stdin
else
qrencode -o $ENCODEQR_TEMPDIR/tmpqrcode.png "$*" < /dev/stdin
fi
eog -n $ENCODEQR_TEMPDIR/tmpqrcode.png
rm -rf $ENCODEQR_TEMPDIR
fi
@lpsmith
Copy link
Author

lpsmith commented Aug 29, 2017

decodeqr decodes any qr codes currently displayed on the screen, writing the result(s) to stdout. If nothing is found, then nothing is written to stdout. This depends on gnome-screenshot and the zbar-tools packages on Ubuntu.

encodeqr generates a qr code and displays it, temporarily. The content of the qr code can be specified either via command line arguments or via stdin; see the documentation for qrencode for more info. This depends on the qrencode and eog packages on Ubuntu.

For security reasons, ideally TMPDIR would be on a memory-only filesystem such as tmpfs. Note that at least on linux, if the TMPDIR environment variable is empty then mktemp will default to creating a temporary directory inside /tmp.

One could avoid writing the screen captures or temporary images to a file by using a fifo, not sure why uncommenting the mkfifo line (and starting gnome-screenshot in the background using &) doesn't work. Assuming you could find a way to get gnome-screenshot and zbarimg to agree on the file type, one could avoid named fifo's altogether via pipe(2) and /dev/fd, but I'm not sure how to accomplish that with my limited bash scripting skills.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment