SpringBoot生成二维码/下载压缩包文件/将图片转成base64输出

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;
    }
}

一个在学习的开发者,勿喷,欢迎交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值