This post will show you how to easily create QR codes using the zxing library using groovy and how to programmatically add a image overlay on top of a QRCode.
First download the zxing library from there github.
Now that we have the library let’s start with something easy like creating a QRCode that represents the url for this post:
QRCODE_IMAGE_HEIGHT = 250 QRCODE_IMAGE_WIDTH = 250 IMAGE_PATH = "/home/skrymer/Pictures/" qrWriter = new QRCodeWriter() matrix = qrWriter.encode("http://wp.me/p2yj7Y-q", BarcodeFormat.QR_CODE, QRCODE_IMAGE_WIDTH, QRCODE_IMAGE_HEIGHT) [1] image = MatrixToImageWriter.toBufferedImage(matrix) [2] imageFile = new File(IMAGE_PATH, "qrcode.png") ImageIO.write(image, "PNG", imageFile) [3]
- 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.
- Create the image using MatrixToImageWriter.toBufferedImage() from the previously generated matrix.
- 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 other types of barcodes and the library contains a lot more functionality than showed here – 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
QRCODE_IMAGE_HEIGHT = 300 QRCODE_IMAGE_WIDTH = 300 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", BarcodeFormat.QR_CODE, QRCODE_IMAGE_WIDTH, QRCODE_IMAGE_HEIGHT, hints) image = MatrixToImageWriter.toBufferedImage(matrix) overlay = ImageIO.read(new File("/home/skrymer/Pictures/", "skull.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!! ================================="
- Create a map of hints and pass them to the writer to create a QRCode with error correction set to level H (30%)
- Instantiate a new image to use as a overlay
- 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
- 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 – see my qrcode builder on github for more examples
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.
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???
cheers
The source (Java) is located on github: https://github.com/skrymer/qrbuilder
The QR Code with the wifi logo can’t be recognized in your article, have you ever tried?
True – I’ll update the qr code so that it works.
Dear ,Code is based on JRE8, Our application server has jre6, DO you have source code on jre6 as well.
How about to change the color of the background/foreground?
There is an example on how to color the QR code (foreground) on github https://github.com/skrymer/qrbuilder You can add you own decorator to color the background.
Reblogged this on shebella1014.
[…] via QR code generation with zxing […]