QR Codes

From CompSciWiki
Revision as of 23:42, 13 November 2010 by Mdomarat (Talk | contribs)

Jump to: navigation, search

Step 0: What are QR Codes? Why do I care?

QR codes ("quick read' codes) are two-dimensional barcodes which are readable by many smartphones. There are apps for reading QR codes for iOS (iPhone), Android and Blackberry, for instance. Many organizations now use QRcodes to allow people with smartphones to link directly to webpages by scanning a QR code. This process is known as mobile tagging. In general, a QR code represents a block of text -- this will usually be a URL that the user can visit, but could be any text at all.

In this lab, we will show how to create QR codes that could be decoded by a QR code-reading app. But since we won't be able to print our QR codes, this might be a bit artificial.

Step 1: Get QR Code Library

Like the Twitter lab, we need some extra libraries before we can do QR code generation. You can find the zxing library ("zebra crossing") here.

Step 2: Generate the QR Code

	       QRCode qr = new QRCode();
		try {
			Encoder.encode("text to encode", ErrorCorrectionLevel.H, qr);
		} catch (WriterException e) {
			e.printStackTrace();
		}

The code creates a QR code which represents the text "text to encode". The new QR code is stored in the variable qr. The second parameter of the encode method is the correction level (H is high in this case): the QR code has an error correction ability so that even if some of the barcode is read improperly, the entire message can be decoded.

Step 3: Extract the QR Code as a 2-D array

	               ByteMatrix bmat = qr.getMatrix();
			byte[][] x = bmat.getArray();
			for (int i = 0; i < x.length; i++) {
				for (int j = 0; j < x[i].length; j++) {
					System.out.print(x[i][j] + " " );
					
				}
				System.out.println();
			}

This code uses a two-dimensional array (notice that the variable x has two sets of []). The array is of type byte, which is like int, but can store a smaller range of numbers.

When you run this block of code, the output should be a matrix of 0s and 1s.

Step 4: change to an image

The full example is here.