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