SpringBoot生成二维码/下载压缩包文件/将图片转成base64输出
一、添加依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
二、创建简单页面,方便测试
2.1、页面效果图(比较简陋)
2.2、点击第三个按钮,会在下方回显信息
2.3、页面代码
<template>
<view>
<button @click="createQrcode">生成二维码</button>
<button @click="downQrcodeZip">下载二维码压缩包</button>
<button @click="qrcodeToBase64">二维码图片转成base64</button>
<view v-if="" style="overflow-wrap: break-word;margin: 25rpx;">{{dataBase64}}</view>
</view>
</template>
<script>
export default{
data(){
return{
dataBase64:"",
}
},
methods:{
// 生成二维码
createQrcode(){
var url = "http://IP:端口号/qrcode/createQrcode"
uni.request({
url:url,
})
},
// 下载二维码压缩包
downQrcodeZip(){
var url = "http://IP:端口号/qrcode/downloadQrcodeZip"
uni.request({
url:url,
})
window.location.href = url;
},
// 二维码图片转成base64
qrcodeToBase64(){
var url = "http://IP:端口号/qrcode/qrcodeToBase64"
uni.request({
url:url,
}).then((res) => {
console.log(res);
this.dataBase64 = res.data;
})
}
}
}
</script>
<style>
</style>
三、后台方法
@RestController
@RequestMapping("/qrcode")
@CrossOrigin
public class QrcodeController {
/**
* 生成二维码
*/
@GetMapping("/createQrcode")
public void createQrcode(){
//logo文件位置
String logoPath = "";
//生成位置
String destPath = "";
BufferedImage bufferedImage = null;
try {
String sign = DesUtil.encrypt("22222222222");
bufferedImage = QRCodeUtil.encode(sign, "" ,logoPath,destPath,true,"test二维码1","png");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载二维码压缩包
*/
@GetMapping("/downloadQrcodeZip")
public void createQrcode(HttpServletResponse response) {
response.setContentType("application/octet-stream");
// 压缩包名
response.setHeader("Content-Disposition", "attachment; filename=QrCode-" + "123" + ".zip");
ZipOutputStream zos = null;
//logo文件夹位置,可以为空
String logoPath = "";
//文件位置
String destPath = "";
BufferedImage bufferedImage = null;
try {
String sign = DesUtil.encrypt("121");
zos = new ZipOutputStream(response.getOutputStream());
bufferedImage = QRCodeUtil.encode(sign, "" ,logoPath,destPath,true,"测试二维码","png");
// 将bufferedImage转成inputStream
InputStream inputStream = QRCodeUtil.bufferedImageToInputStream(bufferedImage);
// 压缩文件名称 设置ZipEntry对象
zos.putNextEntry(new ZipEntry(1 + ".JPEG"));
// 设置注释,可以在压缩包属性中注释里查看,不需要则不设置
zos.setComment("code");
int temp = 0;
// 读取内容
while ((temp = inputStream.read()) != -1) {
// 压缩输出
zos.write(temp);
}
// 关闭输入流
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (null != zos) {
zos.flush();
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 二维码图片转成base64
*/
@GetMapping("/qrcodeToBase64")
public String qrcodeToBase64() throws Exception {
//logo文件夹位置,可以为空
String logoPath = "C:\\Users\\Administrator\\Desktop\\bai.jpeg";
//文件位置
String destPath = "C:\\Users\\Administrator\\Desktop";
String sign = DesUtil.encrypt("22222222222");
BufferedImage bufferedImage = QRCodeUtil.encode(sign, "" ,logoPath,destPath,true,"test二维码1","png");
//转成base64
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"png",stream);
String base64 = Base64.getEncoder().encodeToString(stream.toByteArray());
System.out.println("data:image/png;base64," + base64);
return "data:image/png;base64," + base64;
}
}
一个在学习的开发者,勿喷,欢迎交流