Spring Boot 生成和显示二维码
Posted yqj234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 生成和显示二维码相关的知识,希望对你有一定的参考价值。
package com.myapp.controller;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.myapp.util.QrCodeUtil;
@RequestMapping("/barcode")
@Controller
public class QrCodeController
// 生成带logo的二维码到response
@RequestMapping("/qrcode")
public void qrcode(HttpServletRequest request, HttpServletResponse response)
String requestUrl = "http://www.baidu.com";
try
String path = ClassUtils.getDefaultClassLoader().getResource("static/logo.jpeg").getPath();
OutputStream os = response.getOutputStream();
QrCodeUtil.encode(requestUrl, path, os);
catch (Exception e)
e.printStackTrace();
// 生成不带logo的二维码到response
@RequestMapping("/qrnologo")
public void qrnologo(HttpServletRequest request, HttpServletResponse response)
String requestUrl = "http://www.baidu.com";
try
OutputStream os = response.getOutputStream();
QrCodeUtil.encode(requestUrl, null, os);
catch (Exception e)
e.printStackTrace();
// 把二维码保存成文件
@RequestMapping("/qrsave")
@ResponseBody
public String qrsave()
String requestUrl = "http://www.baidu.com";
try
QrCodeUtil.save(requestUrl, null, "/data/qrcode2.jpg");
return "文件已保存";
catch (Exception e)
e.printStackTrace();
return "文件保存失败" + e.getMessage();
// 解析二维码中的文字
@RequestMapping("/qrtext")
@ResponseBody
public String qrtext()
String url = "";
try
url = QrCodeUtil.decode("/data/qrcode2.jpg");
catch (Exception e)
e.printStackTrace();
return "解析到的url:" + url;
以上是关于Spring Boot 生成和显示二维码的主要内容,如果未能解决你的问题,请参考以下文章