Java 如何去除zxing生成的QR码的余量?
我正在使用com.google.zxing版本3.3.2使用jasper report生成QRCode。 生成的QRCode具有空格和边距。我怎样才能避免这些空间 我找到了添加EncodeHintType.MARGIN,-1的解决方案,但是如何在jasper报告的图像表达式中添加这个 下面是我现在使用的图像表达式Java 如何去除zxing生成的QR码的余量?,java,jasper-reports,qr-code,zxing,Java,Jasper Reports,Qr Code,Zxing,我正在使用com.google.zxing版本3.3.2使用jasper report生成QRCode。 生成的QRCode具有空格和边距。我怎样才能避免这些空间 我找到了添加EncodeHintType.MARGIN,-1的解决方案,但是如何在jasper报告的图像表达式中添加这个 下面是我现在使用的图像表达式 com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage( new com.google.zxing.qrcod
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(否则将抛出错误)
要添加此项,可以使用的第二个构造函数
因此,得到的表达式将是
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>
结果
这基本上意味着我需要将番石榴添加到我的类路径?@kinnu是的,我实际上尝试了初始化映射,比如
new java.util.HashMap(){{put(com.google.zxing.EncodeHintType.MARGIN,0)}
但是jasper reports在编译它时遇到了麻烦(也许你可以找到一种方法,比如将它定义为参数),为了不调试,我改为使用番石榴,如果你不想要番石榴,但是有Java9,你可以使用Map.of
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)))
<?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>