Added the quiet zone to the QRCodeWriter.
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeWriter.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Writer;
21 import com.google.zxing.WriterException;
22 import com.google.zxing.common.ByteMatrix;
23 import com.google.zxing.common.ByteArray;
24 import com.google.zxing.qrcode.encoder.Encoder;
25 import com.google.zxing.qrcode.encoder.QRCode;
26
27 import java.util.Hashtable;
28
29 /**
30  * This object renders a QR Code as a ByteMatrix 2D array of greyscale values.
31  *
32  * @author dswitkin@google.com (Daniel Switkin)
33  */
34 public final class QRCodeWriter implements Writer {
35
36   private static final int QUIET_ZONE_SIZE = 4;
37
38   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height)
39       throws WriterException {
40
41     return encode(contents, format, width, height, null);
42   }
43
44   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height,
45       Hashtable hints) throws WriterException {
46
47     if (contents == null || contents.length == 0) {
48       throw new IllegalArgumentException("Found empty contents");
49     }
50
51     if (format != BarcodeFormat.QR_CODE) {
52       throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
53     }
54
55     if (width < 0 || height < 0) {
56       throw new IllegalArgumentException("Requested dimensions are too small: " + width + "x" +
57           height);
58     }
59
60     // TODO: Check hints for error correction level instead of hardcoding
61     int errorCorrectionLevel = QRCode.EC_LEVEL_L;
62     QRCode code = new QRCode();
63     if (Encoder.Encode(new ByteArray(contents), errorCorrectionLevel, code)) {
64       return renderResult(code, width, height);
65     } else {
66       throw new WriterException("Could not generate a QR Code");
67     }
68   }
69
70   // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
71   // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
72   private ByteMatrix renderResult(QRCode code, final int width, final int height) {
73     ByteMatrix input = code.matrix();
74     int inputWidth = input.width();
75     int inputHeight = input.height();
76     int qrWidth = inputWidth + (QUIET_ZONE_SIZE * 2);
77     int qrHeight = inputHeight + (QUIET_ZONE_SIZE * 2);
78     int outputWidth = Math.max(width, qrWidth);
79     int outputHeight = Math.max(height, qrHeight);
80
81     int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
82     // Padding includes both the quiet zone and the extra white pixels to accomodate the requested
83     // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
84     // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
85     // handle all the padding from 100x100 (the actual QR) up to 200x160.
86     int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
87     int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
88
89     ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
90     byte[][] outputArray = output.getArray();
91
92     // We could be tricky and use the first row in each set of multiple as the temporary storage,
93     // instead of allocating this separate array.
94     byte[] row = new byte[outputWidth];
95
96     // 1. Write the white lines at the top
97     for (int y = 0; y < topPadding; y++) {
98       setRowColor(outputArray[y], (byte) 255);
99     }
100
101     // 2. Expand the QR image to the multiple
102     final byte[][] inputArray = input.getArray();
103     for (int y = 0; y < inputHeight; y++) {
104       // a. Write the white pixels at the left of each row
105       for (int x = 0; x < leftPadding; x++) {
106         row[x] = (byte) 255;
107       }
108
109       // b. Write the contents of this row of the barcode
110       int offset = leftPadding;
111       for (int x = 0; x < inputWidth; x++) {
112         byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255;
113         for (int z = 0; z < multiple; z++) {
114           row[offset + z] = value;
115         }
116         offset += multiple;
117       }
118
119       // c. Write the white pixels at the right of each row
120       offset = leftPadding + (inputWidth * multiple);
121       for (int x = offset; x < outputWidth; x++) {
122         row[x] = (byte) 255;
123       }
124
125       // d. Write the completed row multiple times
126       offset = topPadding + (y * multiple);
127       for (int z = 0; z < multiple; z++) {
128         System.arraycopy(row, 0, outputArray[offset + z], 0, outputWidth);
129       }
130     }
131
132     // 3. Write the white lines at the bottom
133     int offset = topPadding + (inputHeight * multiple);
134     for (int y = offset; y < outputHeight; y++) {
135       setRowColor(outputArray[y], (byte) 255);
136     }
137
138     return output;
139   }
140
141   private static void setRowColor(byte[] row, byte value) {
142     for (int x = 0; x < row.length; x++) {
143       row[x] = value;
144     }
145   }
146
147 }