安装
maven项目只需引入依赖:
com.google.zxing
core
3.3.0
com.google.zxing
javase
3.3.0
如果非maven项目,就去官网下载发布版本:下载地址
生成二维码图片
ZXing 生成二维码图片有以下步骤:
1、com.google.zxing.MultiFormatWriter 根据内容以及图像编码参数生成图像2D矩阵。
2、com.google.zxing.client.j2se.MatrixToImageWriter 根据图像矩阵生成图片文件或图片缓存 BufferedImage 。
public void encode(String content, String filepath) throws WriterException, IOException {
int width = 100;
int height = 100;
Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
encodeHints.put(EncodeHintType.CHARACTER_SET, “UTF-8”);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
Path path = FileSystems.getDefault().getPath(filepath);
MatrixToImageWriter.writeToP