Overlaying Image in QR
- 2 minsWho should read this?
If you are into generating QR Codes and want to generate one which includes an image, like your company logo etc, then you are in the right place. This blog is NOT focused on explaining what QR Codes are and how they are used and the amount of data they can store. If you need information with that, refere Wikipedia, it has more than sufficient information for you.
If you are still with me, then get ready, open your IDE. We will be using Java and ZXing here.
Overlay Image in QR Code
These are pretty common types of QR Codes. This technique is specifically targetted at marketting/branding. Looking at these QR Codes, a user can instantly know who this QR belongs to and what he/she is supposed to do with it.
Steps to be performed
There are a number of steps to be performed while overlaying an image on a QR code. Like you might have noticed, all the QR Codes shows in the above image are layered i.e. the bottom layer is the actual QR and the top layer is the logo. We have to do the same with ZXing. Let us first see the steps :
- Create configuration that specifies the error correction
- Create a QR code as a
BitMatrix
, using ZXing, with your content - Load QR Image into
BufferedImage
- Resize the logo image that you want to overlay and get it in another
BufferedImage
object - Calculate the difference between diamentions of QR Image and the Overlay Image
- Write the final Image
The following piece of code explains how I wrote the function. There may be many different ways to do this but this is the only one I am aware of. If you know of a different way of doing this, speak up in the comments!
The variables WIDTH
, HEIGHT
, BLACK
, WHITE
mark diamentions and color combination of the QR Code. If you are interested in the values that I used, here you go :
- WIDTH: 150
- HEIGHT: 150
- BLACK: 0xFF000000
- WHITE: 0xFFFFFFFF
And this is how I wrote the getOverlay
function.
In the line 8 and 9, I found out that If I reduced the overlay image by 6 times, it gave me the best results. However, in your case, you may want to play around with this value to see what suits you the best.
You can just copy the code and replace LOGO
and content
and it should straightaway work for you. If not, feel free to post queries in the comments.