Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove margin of QRCode generated with zxing?

I am using com.google.zxing version 3.3.2 to generate QRCode using jasper report. The generated QRCode is having spaces and margin. How can I avoid those spaces.

I found solutions to add EncodeHintType.MARGIN, -1 but how to add this in image expression in jasper report.

Below is the image expression as of now I am using.

com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(
new com.google.zxing.qrcode.QRCodeWriter().encode(
    $F{Code},com.google.zxing.BarcodeFormat.QR_CODE, 300, 300))
like image 861
kinnu Avatar asked Jan 24 '19 06:01

kinnu


1 Answers

Adding EncodeHintType.MARGIN is correct but you need to put 0 (otherwise it will throw an error)

To add this you can use the second constructor of QRCodeWriter

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

This means that you need to pass a Map which is initialize with the key and value. One way of creating and initializing maps is to use use Guava and it's ImmutableMap

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

Hence the resulting expression would be

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

Example (I have put a border around to demonstrate margin 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>

Result

enter image description here

like image 86
Petter Friberg Avatar answered Nov 05 '22 22:11

Petter Friberg