maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
生成代码
public static void main(String[] args) throws Exception {
String content = "Hello world";
//配置
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix encode = new MultiFormatWriter()
.encode(
content, //二维码内容
BarcodeFormat.QR_CODE, //类型为二维码
200, //大小
200,
hints);
Path path = FileSystems.getDefault().getPath(
"D://", //生成二维码的路径
"myqcode.jpg"); //二维码名称
//写入磁盘
MatrixToImageWriter.writeToPath(encode, "jpg", path);
}