0 Flares 0 Flares ×QR Codes in 11 lines of code? Easy. Let’s look at how we can leverage some free APIs from Google to get flexible QR codes in your mobile apps. Let’s start off with the final product — a function we can call to create a Ti.UI.ImageView with a QR code in it: See? 11 lines of code (excluding the comments at the top). Now let’s run through what the lines do.
- Line 1 and 11 make a function; we want to use reuse this code in multiple places in our code, so we stick it in a function.
- Line 2 creates the image URL that we will load. It is something particular to Google’s Charts API. In particular, we set “chs” to the size of our QR code, and “chl” to the text we want encoded. Note how we encodeURI the text so that we have a valid URL!
- Lines 4 through 7 ensure that our ImageView will size properly on Android by adding the “dp” (density independent pixels) unit on to our width and height.
- Lines 8 through 10 make our ImageView and return it! We leverage the ImageView’s capability to download remote images to keep our code extraordinarily simple.