874cc0edd62cc14808c6548bb913714b4ed8e471
[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.EncodeHintType;
21 import com.google.zxing.Writer;
22 import com.google.zxing.WriterException;
23 import com.google.zxing.common.ByteMatrix;
24 import com.google.zxing.qrcode.encoder.Encoder;
25 import com.google.zxing.qrcode.encoder.QRCode;
26 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
27
28 import java.util.Hashtable;
29
30 /**
31  * This object renders a QR Code as a ByteMatrix 2D array of greyscale values.
32  *
33  * @author dswitkin@google.com (Daniel Switkin)
34  */
35 public final class QRCodeWriter implements Writer {
36
37   private static final int QUIET_ZONE_SIZE = 4;
38
39   public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height)
40       throws WriterException {
41
42     return encode(contents, format, width, height, null);
43   }
44
45   public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,
46       Hashtable hints) throws WriterException {
47
48     if (contents == null || contents.length() == 0) {
49       throw new IllegalArgumentException("Found empty contents");
50     }
51
52     if (format != BarcodeFormat.QR_CODE) {
53       throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
54     }
55
56     if (width < 0 || height < 0) {
57       throw new IllegalArgumentException("Requested dimensions are too small: " + width + "x" +
58           height);
59     }
60
61     ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
62     if (hints != null) {
63       ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
64       if (requestedECLevel != null) {
65         errorCorrectionLevel = requestedECLevel;
66       }
67     }
68
69     QRCode code = new QRCode();
70     Encoder.encode(contents, errorCorrectionLevel, code);
71     return renderResult(code, width, height);
72   }
73
74   // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
75   // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
76   private ByteMatrix renderResult(QRCode code, int width, int height) {
77     ByteMatrix input = code.getMatrix();
78     int inputWidth = input.width();
79     int inputHeight = input.height();
80     int qrWidth = inputWidth + (QUIET_ZONE_SIZE * 2);
81     int qrHeight = inputHeight + (QUIET_ZONE_SIZE * 2);
82     int outputWidth = Math.max(width, qrWidth);
83     int outputHeight = Math.max(height, qrHeight);
84
85     int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
86     // Padding includes both the quiet zone and the extra white pixels to accomodate the requested
87     // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
88     // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
89     // handle all the padding from 100x100 (the actual QR) up to 200x160.
90     int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
91     int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
92
93     ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
94     byte[][] outputArray = output.getArray();
95
96     // We could be tricky and use the first row in each set of multiple as the temporary storage,
97     // instead of allocating this separate array.
98     byte[] row = new byte[outputWidth];
99
100     // 1. Write the white lines at the top
101     for (int y = 0; y < topPadding; y++) {
102       setRowColor(outputArray[y], (byte) 255);
103     }
104
105     // 2. Expand the QR image to the multiple
106     byte[][] inputArray = input.getArray();
107     for (int y = 0; y < inputHeight; y++) {
108       // a. Write the white pixels at the left of each row
109       for (int x = 0; x < leftPadding; x++) {
110         row[x] = (byte) 255;
111       }
112
113       // b. Write the contents of this row of the barcode
114       int offset = leftPadding;
115       for (int x = 0; x < inputWidth; x++) {
116         byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255;
117         for (int z = 0; z < multiple; z++) {
118           row[offset + z] = value;
119         }
120         offset += multiple;
121       }
122
123       // c. Write the white pixels at the right of each row
124       offset = leftPadding + (inputWidth * multiple);
125       for (int x = offset; x < outputWidth; x++) {
126         row[x] = (byte) 255;
127       }
128
129       // d. Write the completed row multiple times
130       offset = topPadding + (y * multiple);
131       for (int z = 0; z < multiple; z++) {
132         System.arraycopy(row, 0, outputArray[offset + z], 0, outputWidth);
133       }
134     }
135
136     // 3. Write the white lines at the bottom
137     int offset = topPadding + (inputHeight * multiple);
138     for (int y = offset; y < outputHeight; y++) {
139       setRowColor(outputArray[y], (byte) 255);
140     }
141
142     return output;
143   }
144
145   private static void setRowColor(byte[] row, byte value) {
146     for (int x = 0; x < row.length; x++) {
147       row[x] = value;
148     }
149   }
150
151 }