生成二维码
常用的二维码生成器hutool 可以生成各种格式的图片 按需调用
所谓的二维码 其实就是一个get请求的连接
所有内置方法都在QrCodeUtil中
简单介绍一下使用Base64的生成
@GetMapping("/getQRCode")
public AjaxResult registrationQRCode() {
return AjaxResult.success("操作成功",
QrCodeUtil.generateAsBase64(String.format("跳转地址", "参数1", "参数2"), new QrConfig(), ImgUtil.IMAGE_TYPE_JPG));
}
public static void ByteToFile(byte[] bytes) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
BufferedImage bi1 = ImageIO.read(bais);
try {
File w2 = new File("D:\\img\\00000000003.jpg");//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动
} catch (IOException e) {
e.printStackTrace();
} finally {
bais.close();
}
}
public static void main(String[] args) {
String s = QrCodeUtil.generateAsBase64(String.format("http://#####/index/", "2"), new QrConfig(), ImgUtil.IMAGE_TYPE_JPG);
System.out.println(s);
s = s.substring(22);
String str = "data:image/jpg;base64,";
byte[] decode = Base64.getDecoder().decode(s);
System.out.println("解码=====" + decode);
try {
ByteToFile(decode);
} catch (Exception e) {
e.printStackTrace();
}
}