63 lines
2.0 KiB
Java
63 lines
2.0 KiB
Java
package com.gxwebsoft.common.core.utils;
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
import cn.hutool.extra.qrcode.QrConfig;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
|
|
/**
|
|
* 常用工具方法
|
|
*
|
|
* @author WebSoft
|
|
* @since 2017-06-10 10:10:22
|
|
*/
|
|
public class MyQrCodeUtil {
|
|
private static final String logoUrl = "https://file.wsdns.cn/20230430/6fa31aca3b0d47af98a149cf2dd26a4f.jpeg";
|
|
|
|
/**
|
|
* 生成二维码
|
|
* 用户ID
|
|
*/
|
|
public static String getCode(Integer loginUserId) throws IOException {
|
|
return createQrCode(loginUserId,loginUserId.toString());
|
|
}
|
|
|
|
/**
|
|
* 生成二维码
|
|
* 支持自定义类型和内容
|
|
*/
|
|
public String getCode(Integer loginUserId,String content) throws IOException {
|
|
return createQrCode(loginUserId,content);
|
|
}
|
|
|
|
/**
|
|
* 生成带水印的二维码
|
|
* @return https://file.gxwebsoft.com/qrcode/870.jpg?v=1682925307569
|
|
*/
|
|
public static String createQrCode(Integer userId,String content) throws IOException {
|
|
// 将URL转为BufferedImage
|
|
BufferedImage bufferedImage = ImageIO.read(new URL(logoUrl));
|
|
// 生成二维码
|
|
QrConfig config = new QrConfig(300, 300);
|
|
// 设置边距,既二维码和背景之间的边距
|
|
config.setMargin(1);
|
|
// 附带小logo
|
|
config.setImg(bufferedImage);
|
|
// 保存路径
|
|
// String filePath = "/Users/gxwebsoft/Documents/uploads/" + "file/qrcode/" + userId + ".jpg";
|
|
String filePath = "/www/wwwroot/file.ws/" + "file/qrcode/" + userId + ".jpg";
|
|
String qrcodeUrl = "https://file.websoft.top/qrcode/" + userId + ".jpg" + "?v=" + DateUtil.current();
|
|
// 生成二维码
|
|
System.out.println("config = " + config);
|
|
System.out.println("con = " + content);
|
|
System.out.println("filePath = " + filePath);
|
|
QrCodeUtil.generate(content, config, FileUtil.file(filePath));
|
|
return qrcodeUrl;
|
|
}
|
|
}
|