Skip to content

Instantly share code, notes, and snippets.

@arafathusayn
Created August 23, 2019 20:55
Show Gist options
  • Save arafathusayn/d16f369621b59defdedd08e3794744a4 to your computer and use it in GitHub Desktop.
Save arafathusayn/d16f369621b59defdedd08e3794744a4 to your computer and use it in GitHub Desktop.
Binary file to multiple QR code image generation and validation (dependencies: qrencode & zbarimg). It takes one file name as the first command-line argument. Second argument is optional which sets how many characters you need to embed in each QR image (default: 1000).
#!/bin/bash
input_file_name="$1"
base64_file_name="__out.b64"
dir="_qr"
mkdir -p $dir
max_length=${2:-1000}
echo QR Code generation is in process...
echo
base64 -w0 $input_file_name > $base64_file_name
char_count=$(($(wc -c $base64_file_name | cut -d' ' -f1)+2400))
i=0
k=0
while (( $i < $char_count+1 )); do
k=$(($k+1))
content=$(dd skip=$i count=$max_length bs=1 if=$base64_file_name 2>&-)
qrencode "$(echo -n $content)" -o ./$dir/qr_$k.png
if (( ${#content} < $max_length )); then
break
fi
i=$(($i+$max_length))
done
echo $char_count characters have been embeded in $k QR code image files.
echo
echo Output directory: $(pwd)/$dir/
echo
echo Validating generated QR codes...
echo
echo -n > $input_file_name.b64
j=1
while (( $j < $k+1 )); do
content=$(zbarimg -q --raw ./$dir/qr_$j.png)
if [[ $max_length != $(echo -n $content | wc -c) && $j < $k ]]; then
echo Not matching: ./$dir/qr_$j.png - truncated to $max_length characters
echo
content=$(echo -n $content | head -c $max_length)
fi
echo -n $content >> $input_file_name.b64
j=$(($j+1))
done
r1=$(sha1sum $base64_file_name)
r2=$(sha1sum $input_file_name.b64)
echo $r1
echo $r2
echo
r1=$(echo -n $r1 | cut -d' ' -f1)
r2=$(echo -n $r2 | cut -d' ' -f1)
if [ "$r1" = "$r2" ]; then
echo Validation Successful.
else
echo Validation Failed!
rm -r ./$dir/qr_*
rm -r ./$dir
fi
rm $base64_file_name
rm $input_file_name.b64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment