java - 如何去除zxing生成的二维码的边距?

标签 java jasper-reports qr-code zxing

我正在使用 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))

最佳答案

添加 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)))

示例(我在周围放置了一个边框来演示边距 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">
    <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>

结果

enter image description here

关于java - 如何去除zxing生成的二维码的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340384/

相关文章:

java - 如何将正确的表达式写入 boolean 值? (不能从 boolean 值转换为 boolean 值)

java - 在 iReport 中使用 Java 类时出错

Android zxing orientation : ResultMetaData is null, 获取旋转/方向

swift - 仅在按下按钮时进行 QR 扫描

java - 如何在 Java 中执行在 where in 子句中单个变量中有多个值的 SQL 语句

java - Switch 语句而不是 if 语句

java - 除了节省代码行之外,lambda 表达式还有其他用途吗?

java - Jaspersoft Studio - net.sf.jasperreports.engine.JRException : java. lang.ClassNotFoundException : org. hibernate.cfg.Configuration

javascript - 使用 Angular JS 请求二维码并显示图像

java - 我应该使用 Java EE 6 中的哪种依赖注入(inject)机制?