org.yes.tools.utils.QRCodeUtil Maven / Gradle / Ivy
package org.yes.tools.utils;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/***
* 二维码工具
*
* @author Co.
* @date: 2022/10/22 13:26
* @return
*/
@Component
public class QRCodeUtil {
public static MultipartFile crateQRCode(String content, int width, int height) throws IOException {
QrConfig config = new QrConfig(width, height);
// 设置边距,既二维码和背景之间的边距
config.setMargin(2);
// 设置前景色,既二维码颜色
config.setForeColor(Color.BLACK.getRGB());
// 设置背景色(灰色)
config.setBackColor(Color.WHITE.getRGB());
File file = QrCodeUtil.generate(content, config, FileUtil.file("/www/wwwroot/code/" + RandomUtil.randomString(12) + "-qr.jpg"));
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
file.delete();
return multipartFile;
}
}