Java implements several methods for generating QR codes, as follows:
1: Generate QR code in Java project using SwetakeQRCode
http://swetake.com/qr/ Download address
Or http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip
This is written by the Japanese and generates the square QR code we commonly use.
Can be used in Chinese
For example: 5677777ghjjjjjj
2: Use BarCode4j to generate barcode and QR code
BarCode4j URL: http://sourceforge.NET/projects/barcode4j/
barcode4j is a QR code generation algorithm using datamatrix, which supports qr algorithm
datamatrix is a standard in Europe and America, and qr is a standard in Japan.
barcode4j is usually generated as rectangular
For example: 88777alec000yan
3: zxing
zxing This is from Google
Download address: http://code.google.com/p/zxing/downloads/list
Java code:
import java.io.File; import java.util.Hashtable; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeEvents { public static void main(String []args)throws Exception{ String text = "Hello"; int width = 100; int height = 100; String format = "png"; Hashtable hints= new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints); File outputFile = new File("new.png"); MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); } }4: There is a way to implement QR code in Google Chart API
Using this API, use Google appengine to implement it.
5: JS generates QR code
Generate QR code using jQuery-qrcode
Let’s briefly talk about jquery-qrcode, an open source three-party library (can be obtained from https://github.com/jeromeetienne/jquery-qrcode).
qrcode.js is the core class that implements QR code data calculation.
jquery.qrcode.js encapsulates it in jquery method, and uses it to realize graphic rendering, which is actually drawing (supports two ways of canvas and table)
The main supported functions are:
Js code:
text : "https://github.com/jeromeetienne/jquery-qrcode" //Set the QR code content
Js code:
render : "canvas",//Set rendering mode width : 256, //Set width height : 256, //Set height typeNumber : -1, //Computation mode correctLevel : QRErrorCorrectLevel.H,//Error correction level background : "#ffffff",//Background color foreground : "#000000" //Foreground color
How to use is very simple
Js code:
jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});After simple practice,
The rendering performance using the canvas method is still very good, but if you use the table method, the performance is not ideal, especially for browsers below IE9, so you need to optimize the rendering method of table by yourself, so I won't explain it in detail here.
In fact, the above js has a small disadvantage, that is, it does not support Chinese by default.
This has something to do with the js mechanism. The jquery-qrcode library uses the charCodeAt() method for encoding and conversion.
This method will obtain its Unicode encoding by default. Generally, decoders use UTF-8, ISO-8859-1 and other methods.
There is no problem with English. If it is Chinese, Unicode is generally implemented in UTF-16, with a length of 2 digits, while UTF-8 encoding is 3 digits, so the encoding and decoding of the QR code does not match.
The solution is of course, convert the string to UTF-8 before encoding the QR code. The specific code is as follows:
function utf16to8(str) { var out, i, len, c; out = ""; len = str.length; for(i = 0; i < len; i++) { c = str.charCodeAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { out += str.charAt(i); } else if (c > 0x07FF) { out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c >> 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c >>> 6) & 0x3F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } else { out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } } return out; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.