利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片。代码如下
import java.awt.Color;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class test {
public static void main(String[] args) {
try{
String contents = "http://www.jsst.cn";
String path = "e:\\test.png";
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
MultiFormatWriter barcodeWriter = new MultiFormatWriter();
BitMatrix matrix = barcodeWriter.encode(contents, BarcodeFormat.QR_CODE, 300, 300, hints);
File file = new File(path);
Color yellow = new Color(255, 255, 0);
Color white = new Color(255, 255, 255);
MatrixToImageWriter.writeToFile(matrix, "png", file, new MatrixToImageConfig(yellow.getRGB(), white.getRGB()));
} catch (Exception e) {
e.printStackTrace();
}
}
}