String promotionCode = UUID.randomUUID().toString();
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new QRCodeWriter().encode(promotionCode, BarcodeFormat.QR_CODE, 256, 256,hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) == true ? Color.BLACK.getRGB(): Color.WHITE.getRGB());
}
}
File file = new File(path+"/static/code/codeimg/"+promotionCode+".png");
if (!file .exists()) {
file .mkdirs();
}
ImageIO.write(image,"png", new File(String.valueOf(file)));
<!--生成二维码相关依赖-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>