Script: QR Code to Carry Source Code

The Quick Response code (QR code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. A short reminder how to create QR code in Linux-based distributions…

A Very Short Theory

There are several versions which support different number and type of characters.

  • Version 1 (21×21)
  • Version 2 (25×25)
  • Version 3 (29×29)
  • Version 4 (33×33)
  • Version 10 (57×57)
  • Version 25 (117×117)
  • Version 40 (177×177)

Damaged QR code can be still decodable based on the correction level used (Reed–Solomon code). There exist four levels and the approximate error correction capability is as follows:

  • Level L (Low) 7% of codewords can be restored.
  • Level M (Medium) 15% of codewords can be restored.
  • Level Q (Quartile) 25% of codewords can be restored.
  • Level H (High) 30% of codewords can be restored.

The combination of version and correction level creates the finite set of charaters which is the specific QR code cabable to contain. The maximum storage capacities occur for 40-L symbols (version 40, error correction level L), i.e.:

  • Numeric only: 7089, i.e. {0; 1; 2; 3; 4; 5; 6; 7; 8; 9}
  • Alphanumeric: 4296, i.e. {0–9; A–Z (upper-case only); space; $; %; *; +; -; .; /; :}
  • Binary/byte: 2953, i.e. {ISO 8859-1}

The use of QR codes is free of any license. The QR code is clearly defined and published as an ISO standard.

The Linux Script

Assuming that we have a nice source code with tons of spaces, tabulators, and other unimportant characters, we can get rid of them, as the source coude will compile even though these will be removed. The following script will “trim” the original source code, split it, and automatically create the QR code EPS figures (vector) using the qrencode tool. Sending big thanks to the author.

# create a temporary file to work with
# elementary replacements
sed -i "s/t/ /g" "$TEMPORARY"		# replace tabs for spaces
sed -i "s/ {1,}/ /g" "$TEMPORARY"	# replace multispaces for single spaces
sed -i "s/^ //g" "$TEMPORARY"		# remove spaces at the beginning of each row
sed -i "/^ *$/d" "$TEMPORARY"		# remove empty rows
# text density taken to the max
sed -i 's/ {/{/g' "$TEMPORARY"		# replace space/bracket combinations
sed -i 's/{ /{/g' "$TEMPORARY"		#
sed -i 's/} /}/g' "$TEMPORARY"		#
sed -i 's/ }/}/g' "$TEMPORARY"		#
sed -i 's/ )/)/g' "$TEMPORARY"		# 
sed -i 's/ =/=/g' "$TEMPORARY"	# replace space/equal-to combinations
sed -i 's/= /=/g' "$TEMPORARY"	# 
sed -i 's/ +/+/g' "$TEMPORARY"		# replace space/plus combinations
sed -i 's/+ /+/g' "$TEMPORARY"		# 
sed -i 's/ -/-/g' "$TEMPORARY"		# replace space/minus combinations
sed -i 's/- /-/g' "$TEMPORARY"		# 
sed -i 's/ */*/g' "$TEMPORARY"	# replace space/times combinations
sed -i 's/* /*/g' "$TEMPORARY"	# 
sed -i 's/ ////g' "$TEMPORARY"	# replace space/division combinations
sed -i 's// ///g' "$TEMPORARY"	# 
sed -i 's/ </</g' "$TEMPORARY"		# replace space/smaller-than combinations
sed -i 's/< //>/g' "$TEMPORARY"		# replace space/greater-than combinations
sed -i 's/> />/g' "$TEMPORARY"		# 
sed -i 's/ !/!/g' "$TEMPORARY"		# replace space/not-equal-to combinations
sed -i 's/, /,/g' "$TEMPORARY"		# replace space/comma combinations
sed -i 's/; /;/g' "$TEMPORARY"		# replace space/semicolon combinations
# split text by specific size (as per the input set of characters)
split --bytes=2953 --suffix-length=1 --numeric-suffixes "$TEMPORARY" Figure # for ISO 8859-1
# create QR codes
for f in `find . -type f -name "Figure*"`
  qrencode -o "$f.eps" -t eps -l L -v 10 -d 600 < "$f"

That’s pretty much it 🙂

This entry was posted in Linux and tagged , , , , , . Bookmark the permalink.