Thursday, June 16, 2011

Generate QR codes with google API


QR codes are used more and more in all kind of applications, where the barcode was used primarily to identify stuff, for example in a logistics system you now see QR codes coming up in all kind of B2C and C2C applications besides only B2B. Reason for this is that most people are now getting the hang of working with smart-phones which are equipped with camera's and free apps can be downloaded to understand the QR code.


You now see Qr codes coming to magazines where you can use your smartphone to go to a specific website for example. Or you can see QR codes on posters int he city, as a extra information link on tickets, in books… the options for which you can use QR codes are almost endless, not only towards customers but also within business applications.


Another reason that QR codes are coming so popular is that you can store a "large" amount of data in a QR code which you could not easily do with a barcode. You can store up to 7089 numeric code characters in a QR code or 4296 alphanumeric characters or 2953 bytes (binary 8 Bit) or 1817 Kanji or Kana characters (Kanji - chinese characters used in Japanese writing / kana - japanese characters) . for a small image that is a large amount of data and with the correct application and the correct mindset you can do a lot with this.


So all find and good,… how do you generate this mess of pixels? You can find a lot of OpenSource applications and libraries you can use to generate a QR code. However if you want a quick solution you can use the Google Chart API to generate it for you. Google is providing a way to request a QR code via a URL pointing towards the chart API.


The base URL for this part of the chart API is https://chart.googleapis.com however this will redirect you to the google search main page if you are not proving some parameters.


First you have to extend it with "/chart?" this will make the URL https://chart.googleapis.com/chart? which is still not very good and will not give you a QR code. secondly you have to play with a couple of parameters.


cht

Must be set to qr, meaning it will look like cht=qr


chs

This states the size of the QR code. x in pixels.


chl

This parameter is holding your data for example if you want to have "abc23" in the QR code your parameter should look like chl=abc123


choe

This indicates the encoding of the data in the QR code. You can pick UTF-8, Shift_JIS or ISO-8859-1. If not provided UTF-8 is used


child

This indicates the level of correction. You can enter the following values for this parameter (if not provided L is used):

L - [Default] Allows recovery of up to 7% data loss

M - Allows recovery of up to 15% data loss

Q - Allows recovery of up to 25% data loss

H - Allows recovery of up to 30% data loss


so, lets say you want to have a QR code with the text "abcdef123456" in a UTF-8 format and a size of 50X50. for this you will have to use the following URL:


https://chart.googleapis.com/chart?cht=qr&chs=50x50&chl=abcdef123456&choe=UTF-8

this will produce the following QR code:


If you for example want to change the size to 100 by 100 you can change the URL to: https://chart.googleapis.com/chart?cht=qr&chs=100x100&chl=abcdef123456&choe=UTF-8 to create this image:

If you play around with the parameters you will see you can have a lot of fun with it and quickly create QR codes without the need to build a custom (or install) application to create QR codes for you. You can even use the URL to automatically download the pictures for with for example a wget in a bash script.

No comments: