一:引入依赖
<!--二维码开发工具集-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.10</version>
</dependency>
<!-- zxing生成二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
二:编译QRGenerate 工具类
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.http.HttpUtil;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class QRGenerate {
/**
* 生成二维码
* 如果logo path是Null或者"" 即认为不使用logo
*
* @param info 二维码内容
* @param logoPath 本地logo的路径
* @return
*/
public static String localLogoQRToLocal(String info, String logoPath, HttpServletResponse response) throws IOException {
// 读取Logo文件 创建生成到本地的空文件
File logo_image = FileUtil.file("D://123.png"); //此处应该是logoPath。这里直接填写了本地的地址
// 定义二维码配置
QrConfig config = new QrConfig(300, 300);
// 设置边距,既二维码和背景之间的边距
config.setMargin(2);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(Color.BLACK.getRGB());
// 设置背景色(灰色)
config.setBackColor(Color.WHITE.getRGB());
// 设置自己的Logo 如果Logo文件为空 会报错
if (logoPath != "" && logoPath != null && logo_image != null) {
try {
config.setImg(logo_image);
} catch (Exception e) {
e.printStackTrace();
return "2";// "请传入正确图片路径!";
}
}
//将生成的二维码文件,写入到本地指定文件 file中
QrCodeUtil.generate(info, config,"png", response.getOutputStream());
return "1";// 成功
}
}
三:QRController 调用
/**
* @param response 带logo的
* @throws IOException
*/
@GetMapping("/qrcode")
public void giveH5Qrcode(HttpServletResponse response) throws IOException {
//logoPath logo路径
QRGenerate.localLogoQRToLocal("https://spark.appc02.com/xxxxx", logoPath,response);
}