koldfront

open-qr-code #shell

🕘︎ - 2025-06-10

It's not that often I encounter a QR code I want to open on my computer, but when I do it would be nice to simply be able to select the graphic on the screen, and then have the URL open.

Combining Flameshot and zbar-tools in a small shell script allows me to do just that: the script checks for PNG in the clipboard, and if there is none, launches Flameshot. After that zbarimg is run to extract the URL, which is then opened in the default browser:

#!/bin/sh

# open-qr-code - script to interpret a qr-code in the clipboard and
#                open it.
#
#     Depends on: xclip, zbar-tools, libnotify-bin, flameshot.

# 1) Save png from clipboard in file:
CAPTURE=0
if ! xclip -o -t TARGETS -selection clipboard | grep --quiet image/png; then
    # 0) If there is no image in clipboard, launch flameshot to pick one:
    sed -i 's/showDesktopNotification=true/showDesktopNotification=false/' ~/.config/flameshot/flameshot.ini
    flameshot gui --clipboard --accept-on-select
    sed -i 's/showDesktopNotification=false/showDesktopNotification=true/' ~/.config/flameshot/flameshot.ini
    CAPTURE=1
fi
if xclip -o -t TARGETS -selection clipboard | grep --quiet image/png; then
    IMAGE=$(/bin/mktemp --tmpdir qr-codeXXXXXX.png)
    xclip -t image/png -selection clipboard -o > "$IMAGE"
    # 2) Run QR-code detector:
    if CONTENT=$(zbarimg --quiet "$IMAGE"); then
        # 3) If it is a URL, open it in a browser:
        URL=$(echo "$CONTENT" | sed 's/^QR-Code://')
        if echo "$URL" | grep --quiet '^http'; then
            notify-send --icon /usr/share/icons/Moka/256x256@2x/apps/qr-creator.png "Open QR code" "Opened $URL"
            x-www-browser --new-window "$URL" &
        else
            notify-send --icon /usr/share/icons/Moka/256x256@2x/apps/qr-creator.png "Open QR code" "QR code contained non-url: '$URL'"
        fi
    else
        notify-send --icon /usr/share/icons/Moka/256x256@2x/apps/qr-creator.png "Open QR code" "Did not find a QR code in the image"
    fi
    if [ $CAPTURE = 1 ]; then
        echo "" | xclip -in -selection clipboard
    fi
else
    notify-send --icon /usr/share/icons/Moka/256x256@2x/apps/qr-creator.png "Open QR code" "No image/png in clipboard"
fi

Add comment

How to comment, in excruciating detail…

To avoid spam many websites make you fill out a CAPTCHA, or log in via an account at a corporation such as Twitter, Facebook, Google or even Microsoft GitHub.

I have chosen to use a more old school method of spam prevention.

To post a comment here, you need to:

  • Configure a newsreader¹ to connect to the server koldfront.dk on port 1119 using nntps (nntp over TLS).
  • Open the newsgroup called lantern.koldfront and post a follow up to the article.
¹ Such as Thunderbird, Pan, slrn, tin or Gnus (part of Emacs).

Or, you can fill in this form:

+=