批量生成二维码并压缩zip

我是研究僧i 提交于 2021-01-19 14:15:38
        <!-- 二维码生成依赖 -->
        <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>
    @GetMapping("/download")
    public void download(HttpServletResponse response, Long qrCodeId) throws IOException {
        byte[] data = uwpQrCodeService.download(qrCodeId);
        genCode(response,data);
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"qrcode.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
    /**
     * 二维码下载
     *
     * @param qrCodeId
     * @return
     */
    @Override
    public byte[] download(Long qrCodeId) {
        UwpQrCode parentQRCode = uwpQrCodeMapper.selectUwpQrCodeById(qrCodeId);
        if (parentQRCode == null) {
            throw new BaseException("二维码不存在!");
        }
        //查询子节点二维码
        List<UwpQrCode> list = uwpQrCodeMapper.selectChild(qrCodeId);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        try {
            for (UwpQrCode c : list) {
                byte[] bytes = QRCodeUtil.CreateQRCode(c.getQrCodeValue());
                ZipEntry zipEntry = new ZipEntry(c.getQrCodeSn()+".jpg");
                zip.putNextEntry(zipEntry);
                IOUtils.write(bytes, zip);
                zip.flush();
                zip.closeEntry();
            }
            IOUtils.closeQuietly(zip);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream.toByteArray();
    }
public class QRCodeUtil {

    public static byte[] CreateQRCode(String content){
        // 定义二维码的参数
        // 图片宽度
        int width = 300;
        // 图片高度
        int height = 300;
        // 图片格式  如果是png类型,logo图变成黑白的,
        String format = "jpg";
        // 1.定义HashMap hints
        HashMap hints = new HashMap();
        // 2.hints调用put函数设置字符集、间距以及纠错度为M
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //纠错等级【L,M,Q,H】
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 3.最后用MultiformatWriter函数类调用echoed函数并返回一个值 然后写入文件
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToStream(bitMatrix,format,outputStream);
            return outputStream.toByteArray();
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!