Several methods for java to generate qr code (recommended)
- 2020-05-24 05:33:18
- OfStack
java implements several methods of 2d code generation, specifically as follows:
1: generate 2-dimensional code in the 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 it generates the 2-dimensional code that we see in squares
You can use Chinese
Such as: 5677777 ghjjjjj
2: generate barcode and 2d code using BarCode4j
BarCode4j url: http: / / sourceforge NET/projects/barcode4j /
barcode4j is a 2-d code generation algorithm using datamatrix, which supports qr
datamatrix is the European and American standard, qr is the Japanese standard,
barcode4j1 looks like a rectangle
Such as: 88777 alec000yan
3: zxing
zxing this is 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: google chart api has a way to implement 2-d code
With this api, it is implemented using google appengine.
5: JS generates 2-dimensional code
Generate 2-dimensional code using jQuery-qrcode
jquery - qrcode under simple say 1 first, the open source 3 party libraries (from https: / / github com/jeromeetienne/jquery - qrcode access),
qrcode.js is the core class to realize the calculation of 2d code data.
jquery.qrcode.js encapsulates it in jquery mode and USES it to render graphics, which is actually drawing (canvas and table are supported).
The main supported functions are:
Js code:
text : "https://github.com/jeromeetienne/jquery-qrcode" // Set up the 2 D code content
Js code:
render : "canvas",// Set render mode
width : 256, // Set the width
height : 256, // Set the height
typeNumber : -1, // Calculation mode
correctLevel : QRErrorCorrectLevel.H,// Error correction level
background : "#ffffff",// The background color
foreground : "#000000" // The foreground color
It's very simple to use
Js code:
jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});
With simple practice,
The rendering performance of canvas is very good, but if you use table, the performance is not so good, especially for browsers below IE9, so you need to optimize the rendering of table by yourself. I won't go into details here.
One small drawback of js is that it does not support Chinese by default.
This has something to do with the mechanism of js. The library jquery-qrcode USES the method of charCodeAt() for encoding conversion.
By default, this method will get its Unicode encoder, and the decoder like 1 is UTF-8, ISO-8859-1 and so on.
English is no problem, if it is Chinese, Unicode is generally UTF-16, with a length of 2 bits, while UTF-8 is 3 bits, so the encoding and decoding of 2-d code will not match.
The solution, of course, is to convert the string to UTF-8 before encoding the 2-d code. The 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 >> 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;
}