美文网首页
通过一些url链接,生成对应的二维码,并打包为zip下载

通过一些url链接,生成对应的二维码,并打包为zip下载

作者: 青休 | 来源:发表于2020-08-28 16:26 被阅读0次

    做一个记录,客户需要将微信生成的带参二维码,打包压缩成zip,并批量下载
    通过一系列的操作获取到带参二维码的url,然后就可以下面的步骤啦

    一开始准备用java库中的java.util.zip.ZipOutputStream,但是一搜,发现有中文乱码的问题,所以就用了org.apache.tools.zip.ZipOutputStream。

            <!-- 二维码生成依赖 -->
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.4.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.4.0</version>
            </dependency>
    
            <!--压缩依赖-->
            <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
                <version>1.9.4</version>
            </dependency>
    
    @RequestMapping(value = "/downloadQrCode", method = RequestMethod.POST)
    public void downloadQrcode(@RequestBody List<Long> ids, HttpServletRequest request, HttpServletResponse response){
          //自己的业务代码,查询每个需要生成的二维码相关信息
          //其中包括了 二维码的名称(用于下载的文件名),二维码的url内容等信息
         List<QrCodePageVo> qrCodePageVos = sceneQrCodeMapper.getSceneQrCodeByIds(ids);
           if (qrCodePageVos != null && qrCodePageVos.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 (QrCodePageVo qrCodePageVo : qrCodePageVos) {
                        //里面每个二维码的命名
                        zos.putNextEntry(new ZipEntry(qrCodePageVo.getId() + "_" + qrCodePageVo.getSceneName() + ".jpg"));
                        // 支持中文
                        zos.setEncoding("GBK");
                        //生成二维码,并写入到流里面
                        QRCodeUtil.getBarCodeImgByUrl(PRODUCT_CODE_WIDTH, PRODUCT_CODE_HEIGHT, qrCodePageVo.getWxImgUrl(), zos);
                    }
                    zos.flush();
                    zos.close();
                } catch (Exception e) {
                    log.error("zip error from sceneQrCode", e);
                } finally {
                    if (zos != null) {
                        try {
                            zos.flush();
                            zos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    
                    }
    
                }
            }
    }
    
    public class QRCodeUtil {
      /**
         * 根据微信返回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);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:通过一些url链接,生成对应的二维码,并打包为zip下载

          本文链接:https://www.haomeiwen.com/subject/spulsktx.html