一.介绍
这篇博客主要是介绍一下怎么利用zxing.jar这个架包来生成指定网站的二维码图片,下面是Java代码的实现
二.代码实现
package QRCode;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCode {
public static void main(String[] args) throws WriterException, IOException {
//指定生成二维码的网站
String context = "www.baidu.com";
HashMap hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix BitMatrix =new MultiFormatWriter().encode(context ,BarcodeFormat.QR_CODE,300,300);
//将生成的二维码图片存放在电脑的指定位置
Path file=new File("D:\\zxing\\百度二维码.png").toPath();
MatrixToImageWriter.writeToPath(BitMatrix, "png", file);
}
}