1 Maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.2</version>
</dependency>
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
2 调试代码
package com.controller;
import cn.hutool.extra.qrcode.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/QcCode")
public class QcCodeController {
/**
* 获取二维码
*
* @param response
*/
@GetMapping("/getQcCode")
public void getQcCode(HttpServletResponse response) {
try {
QrCodeUtil.generate("https://blog.csdn.net/qq_38974638", 400, 400, "PNG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取带Logo二维码
*
* @param response
*/
@GetMapping("/getLogoQcCode")
public void getLogoQcCode(HttpServletResponse response) {
try {
QrCodeUtil.generate("https://blog.csdn.net/qq_38974638", QrConfig.create().setImg(new ClassPathResource("/img/logo.jpg").getFile()), "PNG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 Logo文件
文件地址:
4 调试结果
正常二维码:
带Logo的二维码: