生成二维码

package com.example.demo111;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.UUID;

@RestController
@Api(value = "TestController ", description = "XXX管理")
@RequestMapping("/TestController ")
public class TestController {

@Autowired
private RedisUtils redisUtils;


/**
* Qr
* @param
*/
@ApiOperation(value = "Qr")
@GetMapping("/Qr")
public void Qr(HttpServletResponse response){
QrConfig config = new QrConfig(300, 300);
// 设置边距,既二维码和背景之间的边距
config.setMargin(3);
// 高纠错级别
config.setErrorCorrection(ErrorCorrectionLevel.H);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(new Color(0,60,130).getRGB());
// 设置背景色(灰色)
config.setBackColor(new Color(242,242,242).getRGB());

String uuid=UUID.randomUUID().toString();
redisUtils.set(uuid, uuid, 100);
String uuidValue=String.valueOf(redisUtils.get(uuid));
BufferedImage bufferedImage = QrCodeUtil.generate(//
"http://XXXXXXX:8080/TestController/chekRedisValue?uuid="+uuid, //二维码内容
config
);

try {
//以JPEG格式向客户端发送
ServletOutputStream os = response.getOutputStream();
ImageIO.write(bufferedImage, "PNG",os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}

}

@ApiOperation(value = "检测二维码是否过期")
@GetMapping("/chekRedisValue")
public ResultJson<String> chekRedisValue(String uuid) {
try {
String uuidValue=String.valueOf(redisUtils.get(uuid));
if (StringUtils.isBlank(uuidValue)||StringUtils.equals("null",uuidValue)) {
return ResultJson.ok("123");
}
return ResultJson.ok("456");
} catch (Exception e) {
return ResultJson.badRequest(e.getMessage());
}
}

}

上一篇:Vue安装uuid插件


下一篇:记一次 PXC 集群拆分引发的思考