先在idea里面导入jar包
链接:https://pan.baidu.com/s/1UdorNg8o0A4FsElghqkJyw
提取码:c4bt
复制这段内容后打开百度网盘手机App,操作更方便哦
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;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
/***使用zxing生成二维码*/
public class CreateQRCode {public static void main(String[] arge) throws WriterException, IOException {int width = 300;//定义图片宽度int height = 300;//定义图片高度String format ="png";//定义图片格式String content ="https://blog.csdn.net/weixin_43392489";//定义二维码内容//定义二维码参数HashMap hints = new HashMap();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置编码//设置容错等级,等级越高,容量越小hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);hints.put(EncodeHintType.MARGIN,2);//设置边距//生成矩阵BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);//设置路径Path file = new File("D:/Users/ma.png").toPath();MatrixToImageWriter.writeToPath(bitMatrix, format, file);//输出图像}
}
复制上面代码
运行即可生成
最后在d盘user下可以找到