index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

30 July 2023 Emacs: scan this QR/bar code

Another day, another tool brought to my Emacs fingertips. A while ago, I wrote about easily copying text from desktop to mobile via QR codes. Later on, I brought it under dwim-shell-command as dwim-shell-commands-clipboard-to-qr.

This time around, I needed the opposite: to scan a code from an image file. This is where zbar's zbarimg comes in. These days, I'm mostly on macOS, so I installed via Homebrew:

$ brew install zbar

There's really nothing to the command. You feed it an image, and it outputs the scanned details. Perfect.

$ zbarimg path/to/code-128.png
CODE-128:hello world
scanned 1 barcode symbols from 1 images in 0.02 seconds

The only challenge is my brain. I probably won't remember the name of this wonderful tool next time I need it, so I'll just add it to my dwim-shell-commands.el arsenal with a memorable name:

(defun dwim-shell-commands-image-scan-code ()
  "Scan any code from image(s)."
  (interactive)
  (dwim-shell-command-on-marked-files
   "Scan code"
   "zbarimg '<<f>>'"
   :utils "zbarimg"))

In the future, rather than reaching out to zbarimg directly, I'll use my trusty fuzzy search and… voilà!

scan-dired.gif

Because dwim-shell-command operates on either dired files or current file, we can also apply our new function when viewing the QR code itself.

scan-image.gif

dwim-shell-commands-image-scan-code is now pushed to dwim-shell-commands.el, the optional package in dwim-shell-command.