Qr codesinjava gg_london

13
Creating QR codes in Java Alison McGreavy

description

QR codes in Java. Presented at Girl Geeks meetup London 29th April 2012

Transcript of Qr codesinjava gg_london

Page 1: Qr codesinjava gg_london

Creating QR codes in JavaAlison McGreavy

Page 2: Qr codesinjava gg_london

QR is short for “Quick Response”.

Developed in Japan by Denso Wave http://www.denso-wave.com/qrcode/index-e.html

Is a 2D barcode.

Can hold • URL• VCard• Text• meCard

http://en.wikipedia.org/wiki/QR_code

What’s a QR code?

Page 3: Qr codesinjava gg_london

Use an app on your smartphone to scan in the QR code

Use an online tool to enter theURL of an image or upload animage

How can I decode a QR code?

Page 4: Qr codesinjava gg_london

Use a smartphone app

Or an online tool such as

http://zxing.appspot.com/generator

How can I create a QR code?

Page 5: Qr codesinjava gg_london

Yes, but how can I create a QR code?

(preferably in Java)

Page 6: Qr codesinjava gg_london

• Download the ZXing-2.0.zip files from http://code.google.com/p/zxing/

• Unzip this on your local machine i.e. C:/zxing-2.0

• Install the ‘core’ package C:/zxing-2.0/core

• Install the ‘javase’ package C:/zxing-2.0/javase

ZXing

Page 7: Qr codesinjava gg_london

Java

• Java download – use JDK for development

http://www.oracle.com/technetwork/java/javase/downloads/index.html

• Set JAVA_HOME environment variable to location of JDK ie C:\Program Files\Java\jdk1.6.0_32\bin

• Add JAVA_HOME variable to PATH variable

• Create Java project

• Add code.jar and jse.jar to your project classpath

• Start to code

Page 8: Qr codesinjava gg_london

Java class to create & decode QR codes package uk.co.girlgeek;

import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import javax.imageio.ImageIO;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.BarcodeFormat;import com.google.zxing.BinaryBitmap;import com.google.zxing.LuminanceSource;import com.google.zxing.MultiFormatReader;import com.google.zxing.Reader;

public class QRCreator {

private static String IMAGEDIR = "C:/Alison/images";private static int IMAGE_WIDTH = 400;private static int IMAGE_HEIGHT = 400;

Page 9: Qr codesinjava gg_london

Main methodpublic static void main(String[] args) { try {

QRCreator qrTest = new QRCreator();

// Create a QR code from a text messageString filenameOfQrFromText = IMAGEDIR + "/qr_fromtext.png";String messageToEncode = "Hi there my name is Alison and my email is [email protected]";qrTest.createQRCode(messageToEncode, filenameOfQrFromText);

// Create a QR code from a URLString filenameofQrfromURL = IMAGEDIR + "/qr_fromurl.png";String urlToEncode = "http://www.geekgirlmeetup.co.uk";qrTest.createQRCode(urlToEncode, filenameofQrfromURL);

// Read a text QR codeqrTest.readQRCode(filenameOfQrFromText);

// Read a URL QR codeqrTest.readQRCode(filenameofQrfromURL);

} catch (Exception e) {e.printStackTrace();System.exit(-1); }}

Page 10: Qr codesinjava gg_london

Method to Create QR code

private void createQRCode(String message, String filename) throws Exception {

File file = new File(filename);BitMatrix bitMatrix = new QRCodeWriter().encode(message, BarcodeFormat.QR_CODE, IMAGE_WIDTH,

IMAGE_HEIGHT);MatrixToImageWriter.writeToStream(bitMatrix, "PNG", new FileOutputStream(file));

System.out.println("Creating QR Image: " + file.getAbsolutePath());}

Page 11: Qr codesinjava gg_london

Method to Decode a QR code

private void readQRCode(String imageFilename) throws Exception {

InputStream barCodeInputStream = new FileInputStream(imageFilename);BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Reader reader = new MultiFormatReader();com.google.zxing.Result result = reader.decode(bitmap);

System.out.println("QR text is: " + result.getText());}

Page 12: Qr codesinjava gg_london

IDE : Eclipse http://www.eclipse.org/ Build Tools : Ant / Maven http://maven.apache.org/ Version Control: CVS http://en.wikipedia.org/wiki/Concurrent_Versions_System Continuous Integration: Jenkins http://en.wikipedia.org/wiki/Jenkins_(software) Unit testing: Junit http://en.wikipedia.org/wiki/JUnit

Jmock http://www.jmock.org/ Quality Metrics: Checkstyle http://checkstyle.sourceforge.net/

PMD http://pmd.sourceforge.net/pmd-5.0.0/ Issue Management: Bugzilla http://www.bugzilla.org/about/

Java Tutorials : http://docs.oracle.com/javase/tutorial/ http://www.javaranch.com/ http://www.coderanch.com/how-to/java/how-to-create-java-program

Java Tools & Tutorials

Page 13: Qr codesinjava gg_london

Alison McGreavy [email protected]