Moved ByteArray up to core/common now that it has no dependencies on qrcode/encoder.
[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   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height)
37       throws WriterException {
38
39     return encode(contents, format, width, height, null);
40   }
41
42   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height,
43       Hashtable hints) throws WriterException {
44
45     if (contents == null || contents.length == 0) {
46       throw new IllegalArgumentException("Found empty contents");
47     }
48
49     if (format != BarcodeFormat.QR_CODE) {
50       throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
51     }
52
53     if (width < 0 || height < 0) {
54       throw new IllegalArgumentException("Requested dimensions are too small: " + width + "x" +
55           height);
56     }
57
58     // TODO: Check hints for error correction level instead of hardcoding
59     int errorCorrectionLevel = QRCode.EC_LEVEL_L;
60     QRCode code = new QRCode();
61     if (Encoder.Encode(new ByteArray(contents), errorCorrectionLevel, code)) {
62       return renderResult(code, width, height);
63     } else {
64       throw new WriterException("Could not generate a QR Code");
65     }
66   }
67
68   // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
69   // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
70   private ByteMatrix renderResult(QRCode code, final int width, final int height) {
71     ByteMatrix input = code.matrix();
72     int inputWidth = input.width();
73     int inputHeight = input.height();
74     int outputWidth = Math.max(width, inputWidth);
75     int outputHeight = Math.max(height, inputHeight);
76
77     int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
78     int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
79     int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
80
81     ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
82     byte[][] outputArray = output.getArray();
83
84     // We could be tricky and use the first row in each set of multiple as the temporary storage,
85     // instead of allocating this separate array.
86     byte[] row = new byte[outputWidth];
87
88     // 1. Write the white lines at the top
89     for (int y = 0; y < topPadding; y++) {
90       setRowColor(outputArray[y], (byte) 255);
91     }
92
93     // 2. Expand the QR image to the multiple
94     final byte[][] inputArray = input.getArray();
95     for (int y = 0; y < inputHeight; y++) {
96       // a. Write the white pixels at the left of each row
97       for (int x = 0; x < leftPadding; x++) {
98         row[x] = (byte) 255;
99       }
100
101       // b. Write the contents of this row of the barcode
102       int offset = leftPadding;
103       for (int x = 0; x < inputWidth; x++) {
104         byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255;
105         for (int z = 0; z < multiple; z++) {
106           row[offset + z] = value;
107         }
108         offset += multiple;
109       }
110
111       // c. Write the white pixels at the right of each row
112       offset = leftPadding + (inputWidth * multiple);
113       for (int x = offset; x < outputWidth; x++) {
114         row[x] = (byte) 255;
115       }
116
117       // d. Write the completed row multiple times
118       offset = topPadding + (y * multiple);
119       for (int z = 0; z < multiple; z++) {
120         System.arraycopy(row, 0, outputArray[offset + z], 0, outputWidth);
121       }
122     }
123
124     // 3. Write the white lines at the bottom
125     int offset = topPadding + (inputHeight * multiple);
126     for (int y = offset; y < outputHeight; y++) {
127       setRowColor(outputArray[y], (byte) 255);
128     }
129
130     return output;
131   }
132
133   private static void setRowColor(byte[] row, byte value) {
134     for (int x = 0; x < row.length; x++) {
135       row[x] = value;
136     }
137   }
138
139 }