09 Feb qr-send: a simple image-based transmission method

Long time no post. So to make it easy for myself to get things started for this new year, I will post about something small and simple. Namely, qr-send.

qr-send is a solution to the problem of “How do I get short bits of text from my computer to my phone?”

Sometimes there might be a URL or a short bit of text you need to get onto your phone for whatever reason. Maybe you find an article you want to read later on your way home from work, maybe you have a bit of text in a document that you want to text over to a friend, or if you are a web-developer like me, you might want to open a website in your device’s web-browser to make sure everything works on the smaller screen.

Sure, you could just type it out. But unless the text is really short then that might not be that easy. You might have a long and complicated URL or other bit of text which contains a bunch of random looking characters, like a hex-encoded hash. Then it would be both tedious to type it out by hand, and there would be a high risk of making mistakes. So manual typing here is not an option.

Now, depending on your device, you might have a whole range of other options available. You could send it via Wifi, but what protocol would you use, and what about security? You could use Bluetooth, but you would need a Bluetooth dongle in your computer. Also many of these options requires a fair bit of configuration to get working satisfactory. You could also download some App™ to both your computer and your device which sends the text via The Cloud™ somehow, but personally I try and avoid those sorts of solutions as much as I can.

qr-send, however, is a very simple solution to this problem (At least compared to some of my examples above). All it does, is it takes the text you wish to send as it’s input, encodes that text into a QR Code image, and then displays that image on your screen. And then all you have to do is open up your QR Code scanner of choice on your device and scan the code, and the text will appear on your phone ready to be copied opened with your browser or copied to wherever it’s needed. Simple as that.

setup qr-send

So to get this all set up, you first need somewhere where you can enter in the text you want to send into. For this I use zenity. Zenity is a command which makes it easy to add GUI dialogs to shell-scripts which I have used quite a few times due to it’s simplicity. If you run a UNIX-based system then you should have no issues installing it with your system’s package manager.

So to get a dialog that accepts basic text input and then outputs it to stdout you just run the following:

[code lang=”sh”] zenity –entry
[/code]

You can customise the dialog further by adding a title and description etc, but for our purposes a simple input will suffice.

Then you need to generate the QR Code image. I do this with qrencode, which is a QR Code generation command with whole bunch of useful options. It should also be readily available in most package managers.

To get it to encode text from stdin into a png file, you run the following:

[code lang=”sh”] qrencode -o qr-code.png[/code]

Combine it with the zenity command from before, and you now have QR Code images being generated from input provided by the GUI dialog:

[code lang=”sh”] zenity –entry | qrencode -o qr-code.png
[/code]

The QR Code image is still being stored as a file, tho, which you will then have to open separately to be able to scan. And we want to have the QR Code displayed on the screen straight away. For this purpose I use feh, which is a very lightweight but versatile image viewer, very much made with the UNIX-philosophy in mind, where it displays images and not much more. It too is easy enough to install on most UNIX-based systems.

To get feh to display the image generated in the previous example, you just run the following:

[code lang=”sh”] feh qr-code.png
[/code]

But feh can just as easily read the image from stdin by providing the special <code>-</code> filename. So to combine it all together, you just need to get qrencode to output the QR Code image to stdout by also using the special <code>-</code> filename, and then pipe the resulting data to feh, like so:

[code lang=”sh”] zenity –entry | qrencode -o – | feh –
[/code]

And there you have it. Now you can add that one-liner to a shell script, and then have it run by a launcher in your desktop environment of choice, or even setup a keyboard shortcut for it for extra quick access.

TL;DR or “here’s one I prepared earlier”

Since I have already implemented this script for myself, I have also made it available on my github. The repository also comes with the added bonus of a little script which generates a nice icon for use with any launchers for the main script.

Just pull from the repository using git:

[code lang=”sh”] git pull https://github.com/Nimdraug/qr-send
[/code]

or visit the repository directly at https://github.com/Nimdraug/qr-send and download it from there.