Skip to content

Instantly share code, notes, and snippets.

@c-nv-s
Created May 7, 2022 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c-nv-s/af1c63e4b6413a51a938a415f4686f5d to your computer and use it in GitHub Desktop.
Save c-nv-s/af1c63e4b6413a51a938a415f4686f5d to your computer and use it in GitHub Desktop.
script to decode qr code from a screenshot
#!/usr/bin/env bash
#
# Siddharth Dushantha 2022
#
# https://github.com/sdushantha/bin
#
image_file="/tmp/ocr.png"
# Check if the needed dependencies are installed
dependencies=(maim notify-send zbarimg xclip)
for dependency in "${dependencies[@]}"; do
type -p "$dependency" &>/dev/null || {
# The reason why we are sending the error as a notification is because
# user is most likely going to run this script by binding it to their
# keyboard, therefor they cant see any text that is outputed using echo
notify-send "ocr" "Could not find '${dependency}', is it installed?"
echo "Could not find '${dependency}', is it installed?"
exit 1
}
done
# Take screenshot by selecting the area
maim -s "$image_file"
# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
status=$?
# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $status -ne 0 ] && exit 1
# Use zbarimg to decode the text from the QR code
decoded_text=$(zbarimg "$image_file" -q --raw)
if [ -z "$decoded_text" ]; then
notify-send "qrshot" "no text was detected"
rm $image_file && exit 1
fi
# Copy text to clipboard
printf %b "$decoded_text" | xclip -selection clip
# Let us know that something was decoded
notify-send "qrshot" "$decoded_text"
# Cleaning up the trash that was left behind
rm $image_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment