NP-Complete Breakfast

Behold: the QR card

2017-01-22

I’m inordinately pleased with thisThat’s me, in case you can’t tell.

How it works

It turns out that getting an arbitrary image into a QR code is annoyingly difficult. Or at least it took me quite a while to do it—partially due to inertia, as it required the concentrated boredom of a trip home for the holidays to get me focused enough to crack it. In the end, the code is fairly simple.

In short, I had to reverse-engineer a QR code library, figure out where the data was stored, put in the data I wanted, and then calculate the corresponding error correction code so that the thing would scan properly.

In more detail

The whole thing fits in a GitHub Gist, if you don’t include the required python-qrcode library. The workflow is fairly simple:

  1. Get the layout of the QR codeThis took the longest to figure out. The simplest way was to create a blank code.
  2. Load an image and extract the data from it, using the coordinates from 1.
  3. For each of the bit masks:
    • Decode the data from the image, insert the desired URL, and correct any disallowed or undesirable bits
    • Recode the data and calculate the correct EC bits
    • Insert data and EC into the image and display it
  4. Choose which of the masks is best