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

6

我正在使用com.google.zxing版本3.3.2来生成Jasper报告中的QR码。

生成的QR码存在空格和边距。如何避免这些空格。

我发现可以添加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))
1个回答

8

添加EncodeHintType.MARGIN是正确的,但需要将其设置为0(否则会抛出错误)

要添加此内容,您可以使用QRCodeWriter的第二个构造函数。

public BitMatrix encode(String contents,BarcodeFormat format,
                        int width,int height,Map<EncodeHintType,?> hints) 
                        throws WriterException 

这意味着您需要传递一个已初始化键值对的Map。创建和初始化Map的一种方法是使用Guava和它的ImmutableMap

com.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)

因此,得出的表达式将会是:
com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(
    new com.google.zxing.qrcode.QRCodeWriter().encode(
    $F{Code},com.google.zxing.BarcodeFormat.QR_CODE, 300, 300,
    com.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)))

例子(我已经加了一个边框来演示margin 0)

jrxml

(说明:该文本为HTML格式,已保留标签)
<?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">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="200" splitType="Stretch">
            <image>
                <reportElement x="0" y="0" width="200" height="200" uuid="9236a226-c581-4d35-88d3-c65181090d03"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <imageExpression><![CDATA[com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(
new com.google.zxing.qrcode.QRCodeWriter().encode(
    "Hello world",com.google.zxing.BarcodeFormat.QR_CODE, 300, 300,com.google.common.collect.ImmutableMap.of(com.google.zxing.EncodeHintType.MARGIN,0)))]]></imageExpression>
            </image>
        </band>
    </title>
</jasperReport>

Result

enter image description here


这基本上意味着我需要将Guava添加到我的类路径中? - kinnu
@kinnu 是的,我确实尝试用 new java.util.HashMap(){{put(com.google.zxing.EncodeHintType.MARGIN,0)}} 进行初始化,但是 jasper 报告在编译时出现了问题(也许你可以找到一个解决方法,比如将其定义为参数),为避免调试,我改用了 Guava,如果您不想使用 Guava,但有 Java 9 版本,则可以使用 Map.of - Petter Friberg
不需要Guava链接。以下提示创建有效:Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.MARGIN, Integer.valueOf(0)); - morbac
@morbac Guava 的链接是如果你想直接从 jrxml 中完成它(你只能在 jrxml 中使用一行代码) - Petter Friberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接