java 实现批量导出二维码压缩包

1、引入依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.5.1</version>
        </dependency>

2、批量导出二维码

    @GetMapping(value = "/downloadQrcode")
    public void downloadQrcode( HttpServletResponse response){
        //自己的业务代码,查询每个需要生成的二维码相关信息
        //其中包括了 二维码的名称(用于下载的文件名),二维码的url内容等信息
        List<T> list= new ArrayList();
        if (list!= null && list.size() > 0) {
            ZipOutputStream zos = null;
            try {
                //压缩的文件名
                String downloadFilename = "场景二维码_" + System.currentTimeMillis() + ".zip";
                // 指明response的返回对象是文件流
                response.setContentType("application/octet-stream");
                // 设置在下载框默认显示的文件名(设置成中文,若有乱码,叫前端重新命名,暂时还没有找到解决办法)
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(downloadFilename.getBytes("gb2312"), "ISO8859-1"));
                zos = new ZipOutputStream(response.getOutputStream());
                for (T  t: list) {
                    //里面每个二维码的命名
                    zos.putNextEntry(new ZipEntry("名字" + ".jpg"));
//                    // 支持中文
//                    zos.setEncoding("GBK");
                    //生成二维码,并写入到流里面
                   getBarCodeImgByUrl(100, 100, "二维码地址", zos);
                }
                zos.flush();
                zos.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (zos != null) {
                    try {
                        zos.flush();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }
        }
    }

    /**
     * 根据微信返回url生成二维码图片后写入输出流里
     *
     * @param width   二维码的宽
     * @param height  二维码的高
     * @param content 二维码的内容
     */
    public static void getBarCodeImgByUrl(int width, int height, String content, OutputStream os) {
        //图片格式,如果是png类型,logo图变成黑白
        String format = "jpg";
        // 1、设置二维码的一些参数
        HashMap hints = new HashMap();
        // 1.1设置字符集
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 1.2设置容错等级;因为有了容错,在一定范围内可以把二维码p成你喜欢的样式 纠错等级【L,M,Q,H】
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 1.3设置外边距;(即白色区域)
        hints.put(EncodeHintType.MARGIN, 2);
        // 2、生成二维码
        try {
            // 2.1定义BitMatrix对象
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 2.3、执行生成二维码,写入到流里面
            MatrixToImageWriter.writeToStream(bitMatrix, format, os);
        } catch (Exception e) {
//            log.error("getBarCodeImgByUrl is error e={}", e);
        }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值