生成二维码 hutool 有相关的工具类可以调用 hutoolAPI
直接上代码实现:
/**
* 自定义参数,二维码样式
*
* @param
* @return QrConfig
* @author 小乌龟
* @date 2022/3/3 13:33
*/
private static QrConfig initQrConfig() {
QrConfig config = new QrConfig(300, 300);
// 设置边距,既二维码和背景之间的边距
config.setMargin(3);
// 设置前景色,既二维码颜色(黑)
config.setForeColor(Color.black);
// 设置背景色(白)
config.setBackColor(Color.white);
return config;
}
/**
*生成二维码图片
* @param content 二维码内容
* @param filePath 图片路径 默认有 /home/yqs/upload/img/ 如填 QRCode
* @return String
* @author 小乌龟
* @date 2022/3/3 13:55
*/
public static String createQRCode(String content, String filePath) {
try {
File imagePath = getWebAppImagePath(fileUtil.webAppDataProperties.getMappingLocation(), filePath); //创建一个文件 路径用自己的
String imageSuffix = ".png";
// 使用雪压算法作为保存时的文件名
String newImageName = IdUtil.getSnowflake(16).nextId() + imageSuffix;
File QrImage = new File(imagePath, newImageName);
QrCodeUtil.generate(content,initQrConfig(), QrImage);
log.info("生成二维码成功!");
StringBuilder imageUrl = new StringBuilder() //这里为路径拼接
.append("img/")
.append(filePath)
.append("/")
.append(newImageName);
return imageUrl.toString();
} catch (QrCodeException e) {
log.error("发生错误! {}!", e.getMessage());
}
return null;
}
然后就是结果展示了:

该博客介绍了如何利用Hutool库在Java中生成自定义样式的二维码。通过设置边距、前景色和背景色,可以创建具有特定尺寸和样式的二维码图片,并将其保存到指定的文件路径。代码示例详细展示了生成过程。
1744

被折叠的 条评论
为什么被折叠?



