HelloWorld2.0 2021-07-15 14:45 采纳率: 100%
浏览 399
已采纳

使用ZXing生成二维码,将二维码流形式输入到execl(poi)中 出现覆盖叠加?

在execl单元格中一直覆盖叠加 , 一个execl中会带一个二维码,如果下载两份,第二份就会在同一个位置出现两个二维码,新的二维码会覆盖第一份的二维码。

    String content ="123cs";

    //获取一个二维码图片
    BitMatrix bitMatrix =null;

    bitMatrix = QRCodeUtils.createCode(content);

    System.out.println(bitMatrix);


    ByteArrayOutputStream pngOutputStream =null;
    pngOutputStream = new ByteArrayOutputStream();

    //以流的形式输出
    MatrixToImageWriter.writeToStream(bitMatrix , "png" , pngOutputStream);

    /**
     * 该构造函数有8个参数
     * 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离
     * 后四个参数,前连个表示图片左上角所在的cellNum和 rowNum,个参数对应的表示图片右下角所在的cellNum和 rowNum,
     * excel中的cellNum和rowNum的index都是从0开始的
     *
     */

    //图片一导出到单元格B5中
    XSSFClientAnchor anchor = null;

    anchor = new XSSFClientAnchor(10000, 10000, 0, 0,
            (short) 8, 18, (short) 9, 20);


    // 插入图片
    st1.createDrawingPatriarch().createPicture(anchor, wb.addPicture(pngOutputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));

    try {

        pngOutputStream.reset();
        pngOutputStream.flush();
        pngOutputStream.close();
        wb.write(out);
        wb.close();


    } catch (IOException e) {
        e.printStackTrace();
    }

public class QRCodeUtils {

/**
 *  生成二维码
 * @param content 二维码的内容
 * @return BitMatrix对象
 * */
public static BitMatrix createCode(String content) throws IOException {
    //二维码的宽高
    int width = 200;
    int height = 200;

    //其他参数,如字符集编码
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    //容错级别为H
    hints.put(EncodeHintType.ERROR_CORRECTION , ErrorCorrectionLevel.H);
    //白边的宽度,可取0~4
    hints.put(EncodeHintType.MARGIN , 0);

    BitMatrix bitMatrix = null;
    try {
        //生成矩阵,因为我的业务场景传来的是编码之后的URL,所以先解码
        bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);

        //bitMatrix = deleteWhite(bitMatrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bitMatrix;
}

/**
 *  删除生成的二维码周围的白边,根据审美决定是否删除
 * @param matrix BitMatrix对象
 * @return BitMatrix对象
 * */
private static BitMatrix deleteWhite(BitMatrix matrix) {
    int[] rec = matrix.getEnclosingRectangle();
    int resWidth = rec[2] + 1;
    int resHeight = rec[3] + 1;

    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
    resMatrix.clear();
    for (int i = 0; i < resWidth; i++) {
        for (int j = 0; j < resHeight; j++) {
            if (matrix.get(i + rec[0], j + rec[1]))
                resMatrix.set(i, j);
        }
    }
    return resMatrix;
}

}

// 收前台传入的ID 重复调用 赋值下载代码生成Execl表格    

@RequestMapping(value = "/slId.do", name = "获取ID 重复调用赋值下载功能")
public void slId(String ids) throws Exception {


    String[] id = ids.split(",");


    //导出zip
    ByteOutputStream outXlsx = new ByteOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(outXlsx);

    Calendar now = Calendar.getInstance();

    for (String s : id) {

        // 拿到明细单ID 根据ID 查询出数据

        ltfmMxd companyMxdById = mxdService.findCompanyMxdById(Integer.parseInt(s));


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        csprintExcel(companyMxdById, baos);
        // 将baos 变成图片
        compressFileToZipStream(zipOutputStream, baos, companyMxdById.getReportNumHead() + "-" + companyMxdById.getReportNumMain() + "-" + now.get(Calendar.YEAR) + "-" + companyMxdById.getReportNum() + ".xlsx");
        baos.flush();
        baos.reset();
        baos.close();


    }

    zipOutputStream.flush();
    zipOutputStream.close();

    String fileNameTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String filename = fileNameTime + ".zip";
    // the response variable is just a standard HttpServletResponse
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    response.setContentType("application/zip");

    try {
        response.getOutputStream().write(outXlsx.toByteArray());
        response.flushBuffer();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        outXlsx.flush();
        outXlsx.reset();
        outXlsx.close();

    }

}


/**
 * 压缩单个excel文件的输出流 到zip输出流,注意zipOutputStream未关闭,需要交由调用者关闭之
 *
 * @param zipOutputStream   zip文件的输出流
 * @param excelOutputStream excel文件的输出流
 * @param excelFilename     文件名可以带目录,例如 TestDir/test1.xlsx
 */
public static void compressFileToZipStream(ZipOutputStream zipOutputStream,
                                           ByteArrayOutputStream excelOutputStream, String excelFilename) {
    byte[] buf = new byte[1024];
    try {
        // Compress the files
        byte[] content = excelOutputStream.toByteArray();
        ByteArrayInputStream is = new ByteArrayInputStream(content);
        BufferedInputStream bis = new BufferedInputStream(is);
        // Add ZIP entry to output stream.
        zipOutputStream.putNextEntry(new ZipEntry(excelFilename));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = bis.read(buf)) > 0) {
            zipOutputStream.write(buf, 0, len);
        }
        // Complete the entry
        excelOutputStream.close();//关闭excel输出流
        zipOutputStream.closeEntry();
        bis.close();
        is.close();
        // Complete the ZIP file
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 写回答

6条回答 默认 最新

  • 曦陽惜夏 2021-07-20 10:34
    关注

    HttpServletResponse response 这个对象使用局部的 public void slId(String ids,HttpServletResponse response) throws Exception,改成这样试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 已采纳回答 7月21日
  • 创建了问题 7月15日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加