依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
代码
public static void createQRCode(String contents, int width, int height) {
Map<EncodeHintType, Object> hints = new HashMap<>();
//指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//指定边框
hints.put(EncodeHintType.MARGIN, 2);
try {
//二维码
BitMatrix matrix = (new MultiFormatWriter()).encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
//二维码转图片
BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
ImageIO.write(image, "png", new FileOutputStream("D:\\pptImage\\as.png"));
} catch (Exception var9) {
var9.printStackTrace();
}
}
这个都是默认黑白的图的二维码,如果想生成彩色二维码见Java 生成彩色二维码
这里提供一个创建二维码的工具:二维码生成工具