QR code generation with zxing

This post will show you how to easily create QR codes using the zxing library using groovy – the code can easily be implemented in Java as well. I’ll also show you how to programmatically add a image overlay on top of the generated QRCode.

First step is to download the zxing library from there Google-code page.

Now that we have the library let’s start with something easy like creating a QRCode that represents the url for this post:

IMAGE_PATH = "/home/skrymer/Pictures/"

qrWriter = new QRCodeWriter()
matrix = qrWriter.encode("http://wp.me/p2yj7Y-q",
                         QRCODE_IMAGE_HEIGHT) [1]

image = MatrixToImageWriter.toBufferedImage(matrix) [2]
imageFile = new File(IMAGE_PATH, "qrcode.png")
ImageIO.write(image, "PNG", imageFile) [3]
  1. Create a new QRCodeWriter and invoke it’s encode method with the data to be stored in the QRCode, the Barcode type we want generated (zxing supports more formats than QRCode) and last we put in the width and height of the QRCode.
  2. Create the image using MatrixToImageWriter.toBufferedImage() from the previously generated matrix.
  3. Last create the actual image and save it on disk

If we execute the code (without errors) the following QRCode will be generated:

The zxing library makes it really easy to create barcodes and the library contains a lot more functionality than showed here, so go and explore the source!

As promised I’ll show you how to add a image overlay on your newly created QRCode but first I want to talk a bit about error correction.

Error correction

Sometimes your QRCode will get damaged or covered up by something – like an image overlay for instance – therefore the designers of the QRCode has added four levels; 7% (L), 15 % (M), 25% (Q), 30% (H) of error correction were a error correction of level H should result in a QRCode that are still valid even when it’s 30% obscured – for more info on error correction check this post

Adding a image overlay

Here is the code for creating a QRCode and adding a overlay.. Don’t worry I’ll walk you through the code further down the post

IMAGE_PATH = "/home/skrymer/Pictures/"

hints = new HashMap<EncodeHintType, ?>()
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H) [1]

qrWriter = new  QRCodeWriter()
matrix = qrWriter.encode("http://wp.me/p2yj7Y-q",

image = MatrixToImageWriter.toBufferedImage(matrix)
overlay = ImageIO.read(new File("/home/skrymer/Pictures/", "wifi-logo.gif")) [2]

//Calculate the delta height and width
deltaHeight = image.height - overlay.height
deltaWidth  = image.width  - overlay.width [3]

//Draw the new image
combined = new BufferedImage(QRCODE_IMAGE_HEIGHT, QRCODE_IMAGE_WIDTH, BufferedImage.TYPE_INT_ARGB)
g = (Graphics2D)combined.getGraphics()
g.drawImage(image, 0, 0, null)
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)) [4]
g.drawImage(overlay, (int)Math.round(deltaWidth/2), (int)Math.round(deltaHeight/2), null)

imageFile = new File(IMAGE_PATH, "qrcode_logo.png")
ImageIO.write(combined, "PNG", imageFile)

println "Generating QRCode ================================="
println "Image width,height: ${image.width},${image.height}"
println "Overlay width,height: ${overlay.width},${overlay.height}"
println "Delta width,height: $deltaWidth, $deltaHeight"
println "QRCode saved to: ${imageFile.absolutePath}"
println "DONE!! ================================="
  1. Create a map of hints and  pass them to the writer to create a QRCode with error correction set to level H (30%)
  2. Instantiate a new image to use as a overlay
  3. Calculate the width and height diff between the overlay image and the QRCode. These values are used in order to align the overlay image in the center of the QRCode
  4. Here we set the composite before the overlay is drawn. This allows us to set the transparency (Alpha value) and how the overlay is to be rendered on top of the QRCode. See this page for more info

Executing the code should generate the following QRCode

I have created a small QRCode-builder using ZXing in Java and added the source to github.

I’ll end this post on QRCode generation with a collection of links that I found useful in my quest to master the art of QRCoding.

4 thoughts on “QR code generation with zxing

  1. Hey,

    nice that you have create some source to generate QR with Image inside.
    BUT what kind of code is it??? it isn’t Java. I’am searching for the classes what u gonna used…

    can you post the right code that anybody can use it???


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s