Insert pictures in QR QR code
QR codes have finally become popular. Now QR code tags on advertisements of large and small products on streets and alleys are everywhere, and most of them are not simple pure QR codes, but QR codes with personality icons in the middle.
I used to do a program that uses Google's open source project zxing to implement QR code and 1R code encoding and decoding, and opened the source code (barcode and QR code encoding and decoder implemented with C#). Today, I continue to add small pictures in the middle of the QR code based on this program.
Background knowledge
QRcode uses Reed-Solomon code to make bug fixes. For us, Reed Solomon coding has two very important characteristics. First, it is an explicit system code, which means that you can directly see the original information in the final encoding. It’s like when we encode “hello world”, we finally see “hello world” and the few fault-tolerant codes that follow it. Second, Reed Solomon encoding can be "exorbited". After the result of two different Reed Solomon encodings is calculated, a new Reed Solomon code will be obtained, and the original code of this new code is the exclusive OR of the original two original codes. If you want to know why these two features work, see Finite Field Arithmetic and Reed-Solomon Coding.
QRcode
A QRcode image defines some unique descriptors to help people or computers identify themselves as a QRcode. This kind of descriptor varies slightly with the size of the QRcode - the larger the QRcode image has more descriptors. However, for human recognition, the most obvious feature is that the symbols of the four corners of the picture are fixed. When seeing such four corners, humans react instinctively: this is a QRcode.
(In fact, we can judge the degree of redundancy of the encoding by reading the two pixel points in the upper left corner of the image. Defining black is 0 and white is 1. Then if you see 00, it is L-level redundancy, 01 is M, 10 is Q, and 11 is the highest H-level redundancy.
With the above work, we can easily know the location of the original code information in the image. Then by changing your original code information, you can change the pixels in the image so that you can draw pictures inside. That being said, some of the following situations can make things more interesting.
I made the QR code to insert the picture:
Previously, I provided you with a free 1QR code encoding codec transformed using the zxing open source project, but failed to insert the picture. After some effort this time, the picture was successfully inserted into the QR code and was able to encode and decode. The key to inserting pictures lies in the adjustment of the fault tolerance coefficient of the QR code, which is described in detail in the source code.
interface:
To insert pictures into the QR code and decode them normally, pay attention to the following two points:
1. The fault tolerance coefficient of setting the QR code should be high;
In the Zxing project, the fault tolerance coefficient can be set when the QR code is generated:
Set EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.H
//Construct QR code writer MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); Hashtable hint=new Hashtable(); hint.Add(EncodeHintType.CHARACTER_SET,"UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION,com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H); //Generate QR code ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300,hint); Bitmap img = bm.ToBitmap();
2. The size and position of the picture inserted in the middle should be appropriate;
If the middle picture is too large and covers most of the area of the QR code, it will definitely not work. If it is too small, it will not have a personalized effect.
So how big is suitable? According to the characteristics of the QR code described in the previous article, the three corners with squares are data areas, which cannot be covered. Only the area in the middle and lower right corners can be covered, but it should not be too large.
After testing, it is more appropriate to have a small image size in the middle account for 2/7 of the total width of the QR code, which not only satisfies the clarity of the image, but also does not affect the encrypted data of the QR code.
Small pictures are best placed in the center, and the shape can be irregular.
Thank you for reading, I hope it can help you. Thank you for your support for this site!