1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.components.barcode4j;
25
26 import java.awt.Color;
27 import java.io.ByteArrayOutputStream;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import javax.xml.transform.Result;
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.TransformerFactory;
36 import javax.xml.transform.dom.DOMSource;
37 import javax.xml.transform.stream.StreamResult;
38
39 import org.krysalis.barcode4j.output.BarcodeCanvasSetupException;
40 import org.krysalis.barcode4j.output.svg.SVGCanvasProvider;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
43
44 import com.google.zxing.EncodeHintType;
45 import com.google.zxing.WriterException;
46 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
47 import com.google.zxing.qrcode.encoder.ByteMatrix;
48 import com.google.zxing.qrcode.encoder.Encoder;
49 import com.google.zxing.qrcode.encoder.QRCode;
50
51 import net.sf.jasperreports.engine.JRComponentElement;
52 import net.sf.jasperreports.engine.JRPropertiesUtil;
53 import net.sf.jasperreports.engine.JRRuntimeException;
54 import net.sf.jasperreports.engine.JRStyle;
55 import net.sf.jasperreports.engine.JasperReportsContext;
56 import net.sf.jasperreports.engine.util.JRColorUtil;
57 import net.sf.jasperreports.renderers.Renderable;
58 import net.sf.jasperreports.renderers.SimpleRenderToImageAwareDataRenderer;
59
60
61 /**
62  * 
63  * @author Teodor Danciu (teodord@users.sourceforge.net)
64  */

65 public class QRCodeSVGImageProducer implements QRCodeImageProducer
66 {
67
68     public static final int DEFAULT_MARGIN = 4;//same as zxing's QRCodeWriter.QUIET_ZONE_SIZE
69     
70     @Override
71     public Renderable createImage(
72         JasperReportsContext jasperReportsContext,
73         JRComponentElement componentElement,
74         QRCodeBean qrCodeBean, 
75         String message
76         )
77     {
78         Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
79         
80         String encoding = JRPropertiesUtil.getInstance(jasperReportsContext).getProperty(
81                 componentElement, QRCodeComponent.PROPERTY_QRCODE_CHARACTER_ENCODING, QRCodeComponent.PROPERTY_DEFAULT_ENCODING);
82         if (!encoding.isEmpty())
83         {
84             hints.put(EncodeHintType.CHARACTER_SET, encoding);
85         }
86         
87         ErrorCorrectionLevel errorCorrectionLevel = qrCodeBean.getErrorCorrectionLevel().getErrorCorrectionLevel();
88         hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
89
90         Integer qrVersion = qrCodeBean.getQrVersion();
91         if(qrVersion != null)
92         {
93             hints.put(EncodeHintType.QR_VERSION, qrVersion);
94         }
95
96         ByteMatrix matrix = null;
97         SVGCanvasProvider provider = null;
98         try
99         {
100             QRCode qrCode = Encoder.encode(message, errorCorrectionLevel, hints);
101             matrix = qrCode.getMatrix();
102             
103             provider = new SVGCanvasProvider(false, OrientationEnum.UP.getValue());
104         }
105         catch (WriterException e)
106         {
107             throw new JRRuntimeException(e);
108         }
109         catch (BarcodeCanvasSetupException e)
110         {
111             throw new JRRuntimeException(e);
112         }
113
114         Document svgDoc = provider.getDOM();
115         Element svg = svgDoc.getDocumentElement();
116         int codeWidth = matrix.getWidth();
117         int codeHeight = matrix.getHeight();
118         
119         JRStyle elementStyle = componentElement.getStyle();
120         int elementWidth = componentElement.getWidth() - (elementStyle == null ? 0
121                 : (elementStyle.getLineBox().getLeftPadding() + elementStyle.getLineBox().getRightPadding()));
122         int elementHeight = componentElement.getHeight() - (elementStyle == null ? 0
123                 : (elementStyle.getLineBox().getTopPadding() + elementStyle.getLineBox().getBottomPadding()));
124         
125         int margin = qrCodeBean.getMargin() == null ? DEFAULT_MARGIN : qrCodeBean.getMargin();
126         int matrixWidth = codeWidth + 2 * margin;
127         int matrixHeight = codeHeight + 2 * margin;
128         
129         // scaling to match the image size as closely as possible so that it looks good in html.
130         // the resolution is taken into account because the html exporter rasterizes to a png 
131         // that has the same size as the svg.
132         int resolution = JRPropertiesUtil.getInstance(jasperReportsContext).getIntegerProperty(
133                 componentElement, BarcodeRasterizedImageProducer.PROPERTY_RESOLUTION, 300);
134         int imageWidth = (int) Math.ceil(elementWidth * (resolution / 72d));
135         int imageHeight = (int) Math.ceil(elementHeight * (resolution / 72d));
136         
137         double horizontalScale = ((double) imageWidth) / matrixWidth;
138         double verticalScale = ((double) imageHeight) / matrixHeight;
139         
140         // we are scaling with integer units, not considering fractional coordinates for now
141         int scale = Math.max(1, (int) Math.min(Math.ceil(horizontalScale), Math.ceil(verticalScale)));
142         int qrWidth = scale * matrixWidth;
143         int qrHeight = scale * matrixHeight;
144
145         // scaling again because of the integer units
146         double qrScale = Math.max(1d, Math.max(((double) qrWidth) / imageWidth, ((double) qrHeight) / imageHeight));
147         int svgWidth = (int) Math.ceil(qrScale * imageWidth);
148         int svgHeight = (int) Math.ceil(qrScale * imageHeight);
149         svg.setAttribute("width", String.valueOf(svgWidth));
150         svg.setAttribute("height",String.valueOf(svgHeight));
151         svg.setAttribute("viewBox""0 0 " + svgWidth + " " + svgHeight);
152
153         int xOffset = Math.max(0, (svgWidth - qrWidth) / 2);
154         int yOffset = Math.max(0, (svgHeight - qrHeight) / 2);
155         
156         Color color = componentElement.getForecolor();
157         String fill = "#" + JRColorUtil.getColorHexa(color);
158         String fillOpacity = color.getAlpha() < 255 ? Float.toString(((float) color.getAlpha()) / 255) : null;
159         String rectangleSize = Integer.toString(scale);
160         for (int x = 0; x < codeWidth; x++) {
161             for (int y = 0; y < codeHeight; y++) {
162                 if (matrix.get(x,y) == 1) {
163                     Element element = svgDoc.createElementNS(svg.getNamespaceURI(), "rect");
164                     element.setAttribute("x", String.valueOf(xOffset + scale * (x + margin)));
165                     element.setAttribute("y", String.valueOf(yOffset + scale * (y + margin)));
166                     element.setAttribute("width", rectangleSize);
167                     element.setAttribute("height", rectangleSize);
168                     element.setAttribute("fill", fill);
169                     if (fillOpacity != null) {
170                         element.setAttribute("fill-opacity", fillOpacity);
171                     }
172                     svgDoc.getFirstChild().appendChild(element);
173                 }
174             }
175         }        
176
177         Source source = new DOMSource(svgDoc);
178         ByteArrayOutputStream baos = new ByteArrayOutputStream();
179         Result output = new StreamResult(baos);
180
181         try
182         {
183             Transformer transformer = TransformerFactory.newInstance()
184                     .newTransformer();
185             transformer.transform(source, output);
186
187         }
188         catch (TransformerException e)
189         {
190             throw new JRRuntimeException(e);
191         }
192
193         return SimpleRenderToImageAwareDataRenderer.getInstance(baos.toByteArray());
194     }
195
196 }
197