依赖:
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
代码:
/** * 生成二维码到指定路径 * @param path 目录+文件名:如:D:\qrcode.png * @param format 图片格式:如png * @param content 二维码内容 * @param wh 图片宽高 * @throws Exception */ public static void generateQrcodeImage(String path, String format, String content, int wh) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, wh, wh); MatrixToImageWriter.writeToStream(bitMatrix, format, fileOutputStream); }