/ code

QR codes in Factor

Wow, so Factor is one of the most amazing programming languages that I’ve ever used. And I’ve used a bucketful of them in my checkered past, that’s for sure. But there’s something about using this odd little stack-based “concatenative” language that’s really got me going on a creative high.

Perhaps it’s the interactivity, perhaps it’s how cleanly and concisely concepts can be expressed; I can’t quite define what it is that makes me so happy with this. Not only that, the people in the community are amazing, helpful, friendly, and really freaking smart!

Anyway, I was playing around and brainstorming, and somehow got on the topic of barcodes, and ran across the Zebra Crossing or zxing demo page which uses the Google Charts API to create all manner of useful QR (“Quick Response”) Codes. They look like this:

That’s a 2-dimensional barcode representing the URL http://factorcode.org. If you have a mobile phone that groks these, you can take a picture with the phone’s camera and be automatically taken to that URL. I use RedLaser on my iPhone, because it’s not built into the platform like with Android phones. (In fact the ZXing library is used in the Android software to parse these codes; pretty nifty, eh?)

Anyhow as you can see if you hit the zxing demo page, you can stick a URL, a calendar event, a contact, plain text, a “send sms to” command, and more on one of these codes.

I had just gotten a scrap of paper with my friend’s phone number on it and, programmer that I am, I decided that the best way to enter this into my phone was to write some software to generate a QR code that I could scan on my phone and then add to my contact book. (Yep, all that instead of just taking 20 seconds to type it in. I am broken.)

So, first stop was making a qr-image word that, given a string, would fetch the image of the QR code from the GCharts API. After all, these little symbols are just computer vision-friendly encodings of unicode characters. Yep, just tested that, “Good morning” in Japanese scans perfectly.

To get the phone to recognize contact info, it’s got to be encoded using the MECARD format, which is an abbreviated vCard. (As noted here, vCard is way too chatty for a tiny barcode).

The end result? A mecard vocabulary to talk about contact information and generate the MECARD-formatted string to pass to qr-image and generate the QR image.

T{ mecard { name "Fred Alger" } { email "fred@fredalger.net" } { nickname "@_phred" } } >mecard
--> "MECARD:N:Fred Alger;EMAIL:fred@fredalger.net;NICKNAME:@_phred;;"
qr-image image.

I’d be lying if I said that it worked the first time, but it took me less than an hour to implement. Such is the power of Factor.

Remember that I said that the Factor community are all really smart? While I was implementing my little amusement, mrjbq7 just happened to write a vocabulary for the Google Charts API. This is gonna be epic when it’s done, and it’s going to let me replace my qr-image word with just <qr-code> chart, as well as generate all manner of graphs from my Factor primitives. So awesome.

OK, enough said. Check it all out on GitHub.