001/*
002 * $Id: BarcodeQRCode.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045
046import com.itextpdf.text.pdf.qrcode.EncodeHintType;
047import com.itextpdf.text.pdf.qrcode.WriterException;
048import com.itextpdf.text.pdf.qrcode.ByteMatrix;
049import com.itextpdf.text.pdf.qrcode.QRCodeWriter;
050import com.itextpdf.text.BadElementException;
051import com.itextpdf.text.ExceptionConverter;
052import com.itextpdf.text.Image;
053import com.itextpdf.text.pdf.codec.CCITTG4Encoder;
054import java.awt.Canvas;
055import java.awt.image.MemoryImageSource;
056import java.util.Map;
057
058/**
059 * A QRCode implementation based on the zxing code.
060 * @author Paulo Soares
061 * @since 5.0.2
062 */
063public class BarcodeQRCode {
064    ByteMatrix bm;
065
066    /**
067     * Creates the QR barcode. The barcode is always created with the smallest possible size and is then stretched
068     * to the width and height given. Set the width and height to 1 to get an unscaled barcode.
069     * @param content the text to be encoded
070     * @param width the barcode width
071     * @param height the barcode height
072     * @param hints modifiers to change the way the barcode is create. They can be EncodeHintType.ERROR_CORRECTION
073     * and EncodeHintType.CHARACTER_SET. For EncodeHintType.ERROR_CORRECTION the values can be ErrorCorrectionLevel.L, M, Q, H.
074     * For EncodeHintType.CHARACTER_SET the values are strings and can be Cp437, Shift_JIS and ISO-8859-1 to ISO-8859-16. The default value is
075     * ISO-8859-1.
076     * @throws WriterException
077     */
078    public BarcodeQRCode(String content, int width, int height, Map<EncodeHintType,Object> hints) {
079        try {
080            QRCodeWriter qc = new QRCodeWriter();
081            bm = qc.encode(content, width, height, hints);
082        }
083        catch (WriterException ex) {
084            throw new ExceptionConverter(ex);
085        }
086    }
087
088    private byte[] getBitMatrix() {
089        int width = bm.getWidth();
090        int height = bm.getHeight();
091        int stride = (width + 7) / 8;
092        byte[] b = new byte[stride * height];
093        byte[][] mt = bm.getArray();
094        for (int y = 0; y < height; ++y) {
095            byte[] line = mt[y];
096            for (int x = 0; x < width; ++x) {
097                if (line[x] != 0) {
098                    int offset = stride * y + x / 8;
099                    b[offset] |= (byte)(0x80 >> (x % 8));
100                }
101            }
102        }
103        return b;
104    }
105
106    /** Gets an <CODE>Image</CODE> with the barcode.
107     * @return the barcode <CODE>Image</CODE>
108     * @throws BadElementException on error
109     */
110    public Image getImage() throws BadElementException {
111        byte[] b = getBitMatrix();
112        byte g4[] = CCITTG4Encoder.compress(b, bm.getWidth(), bm.getHeight());
113        return Image.getInstance(bm.getWidth(), bm.getHeight(), false, Image.CCITTG4, Image.CCITT_BLACKIS1, g4, null);
114    }
115
116    /** Creates a <CODE>java.awt.Image</CODE>.
117     * @param foreground the color of the bars
118     * @param background the color of the background
119     * @return the image
120     */
121    public java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background) {
122        int f = foreground.getRGB();
123        int g = background.getRGB();
124        Canvas canvas = new Canvas();
125
126        int width = bm.getWidth();
127        int height = bm.getHeight();
128        int pix[] = new int[width * height];
129        byte[][] mt = bm.getArray();
130        for (int y = 0; y < height; ++y) {
131            byte[] line = mt[y];
132            for (int x = 0; x < width; ++x) {
133                pix[y * width + x] = line[x] == 0 ? f : g;
134            }
135        }
136
137        java.awt.Image img = canvas.createImage(new MemoryImageSource(width, height, pix, 0, width));
138        return img;
139    }
140}