google 生成二维码

maven依赖

<!--google 二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${google-zxing}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${google-zxing}</version>
</dependency>
<properties>
  <google-zxing>3.2.1</google-zxing>
</properties>

代码

//生成二维码
//1 定义一个json格式字符串
JSONObject jsonObject = new JSONObject();
jsonObject.put("company", "www.baidu.com");
jsonObject.put("name", "百度");
String content = String.valueOf(jsonObject);

int width = 200;
int hight = 200;
//2 编码格式设置等
Map<EncodeHintType, Object> map = new HashMap<EncodeHintType, Object>();
map.put(EncodeHintType.CHARACTER_SET, "UTF-8");

//3 创建一个二维矩阵对象
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, map);


//矩阵 格式(jpj) 路径
//4 生成的路径
String filePath = "D://";
String fileName = "QRCode.jpg";
Path path = FileSystems.getDefault().getPath(filePath, fileName);
//5 将矩阵对象转成图片
MatrixToImageWriter.writeToPath(bitMatrix, "jpg", path);
原文地址:https://www.cnblogs.com/draymond/p/11211278.html