如何去除 zxing 生成的 QRCode 的边距?

我正在使用 com.google.zxing 版本 3.3.2 使用 jasper 报告生成 QRCode。生成的二维码有空格和边距。我怎样才能避免这些空间。


我找到了添加 EncodeHintType.MARGIN, -1 的解决方案,但是如何在 jasper 报告中的图像表达式中添加它。


下面是我现在使用的图像表达式。


com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(

new com.google.zxing.qrcode.QRCodeWriter().encode(

    $F{Code},com.google.zxing.BarcodeFormat.QR_CODE, 300, 300))


泛舟湖上清波郎朗
浏览 542回答 1
1回答

猛跑小猪

添加 EncodeHintType.MARGIN是正确的但是你需要输入0(否则会抛出错误)要添加它,您可以使用QRCodeWriter的第二个构造函数public BitMatrix encode(String contents,BarcodeFormat format,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width,int height,Map<EncodeHintType,?> hints)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws WriterException&nbsp;这意味着您需要传递一个使用键和值初始化的 Map。创建和初始化地图的一种方法是使用Guava,它是ImmutableMapcom.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)因此,结果表达式将是com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(&nbsp; &nbsp; new com.google.zxing.qrcode.QRCodeWriter().encode(&nbsp; &nbsp; $F{Code},com.google.zxing.BarcodeFormat.QR_CODE, 300, 300,&nbsp; &nbsp; com.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)))示例(我放了一个边框来展示边距 0)jrxml<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="QRCode" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ee443473-56d0-44df-b5d4-ac3fe82fd9bc">&nbsp; &nbsp; <queryString>&nbsp; &nbsp; &nbsp; &nbsp; <![CDATA[]]>&nbsp; &nbsp; </queryString>&nbsp; &nbsp; <title>&nbsp; &nbsp; &nbsp; &nbsp; <band height="200" splitType="Stretch">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <image>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <reportElement x="0" y="0" width="200" height="200" uuid="9236a226-c581-4d35-88d3-c65181090d03"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <box>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pen lineWidth="0.25"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </box>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <imageExpression><![CDATA[com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(new com.google.zxing.qrcode.QRCodeWriter().encode(&nbsp; &nbsp; "Hello world",com.google.zxing.BarcodeFormat.QR_CODE, 300, 300,com.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)))]]></imageExpression>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </image>&nbsp; &nbsp; &nbsp; &nbsp; </band>&nbsp; &nbsp; </title></jasperReport>结果
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java