itext生成的二维码有灰色框

福如东海寿比南山 2023-05-09 16:07:54

有个sku标签打印的需求,所以写了用sku生成二维码,并且生成pdf的代码,但是有些用户打出来的二维码会有灰色区域,我怎么尝试都试不出来,50x30mm的纸张,求助各位大佬

    /**
     * 生成二维码
     * pdfCellDTO 生成参数
     * @return
     */
    public Image generateQRCode(PdfCellDTO pdfCellDTO, PdfContentByte cb) {
        String code = pdfCellDTO.getBarCode();
        Float width = pdfCellDTO.getBarSize();
        BarcodeQRCode qrCode = new BarcodeQRCode(code, 1, 1,null);
        Image image = null;
        try {
            image = qrCode.getImage();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成二维码失败", e);
        }
        return image;
    }


/**
     * 生成PDF文件
     * @param document
     * @param pdfCellDTOS
     * @param pdfTableDataDTO
     * @throws Exception
     */
    public void initPdfContent(Document document, List<List<PdfCellDTO>> pdfCellDTOS
            , PDFTableDataDTO pdfTableDataDTO, PdfWriter writer) {
        for (List<PdfCellDTO> pdfCellDataDTOList : pdfCellDTOS) {
            // 建立表格
            PdfPTable table = createTable(pdfTableDataDTO.getHorizontalAlignment(), pdfTableDataDTO.getWidthPercents(), pdfTableDataDTO.getWidth());
            // 建立单元格
            for (PdfCellDTO pdfCellDataDTO : pdfCellDataDTOList) {
                if (PdfImageType.BAR_CODE.getVal().equals(pdfCellDataDTO.getIsImage())) { // 条形码
                    PdfPCell pdfPCell = pdfCellDataDTO.getPdfPCell();
                    pdfPCell.setImage(generateBarCode(pdfCellDataDTO, writer.getDirectContent()));
                    table.addCell(pdfPCell);
                } else if (PdfImageType.QR_CODE.getVal().equals(pdfCellDataDTO.getIsImage())) { // 二维码
                    PdfPCell pdfPCell = pdfCellDataDTO.getPdfPCell();
                    pdfPCell.setImage(generateQRCode(pdfCellDataDTO, writer.getDirectContent()));
                    table.addCell(pdfPCell);
                } else {
                    table.addCell(pdfCellDataDTO.getPdfPCell());
                }
            }
            try {
                document.add(table);
            } catch (Exception e) {
                log.error("dpf生成添加表格失败!");
            }
            // 多个表格默认下一页
            document.newPage();
        }

    }

 

 

 

 

 

 

...全文
6300 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

解决了,因为new BarcodeQRCode的参数里宽高太小的问题

67,528

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧