2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.encoder;
19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
20 import com.google.zxing.qrcode.decoder.Mode;
23 * @author satorux@google.com (Satoru Takabayashi) - creator
24 * @author dswitkin@google.com (Daniel Switkin) - ported from C++
26 public final class QRCode {
28 public static final int NUM_MASK_PATTERNS = 8;
31 private ErrorCorrectionLevel ecLevel;
33 private int matrixWidth;
34 private int maskPattern;
35 private int numTotalBytes;
36 private int numDataBytes;
37 private int numECBytes;
38 private int numRSBlocks;
39 private ByteMatrix matrix;
54 // Mode of the QR Code.
55 public Mode getMode() {
59 // Error correction level of the QR Code.
60 public ErrorCorrectionLevel getECLevel() {
64 // Version of the QR Code. The bigger size, the bigger version.
65 public int getVersion() {
69 // ByteMatrix width of the QR Code.
70 public int getMatrixWidth() {
74 // Mask pattern of the QR Code.
75 public int getMaskPattern() {
79 // Number of total bytes in the QR Code.
80 public int getNumTotalBytes() {
84 // Number of data bytes in the QR Code.
85 public int getNumDataBytes() {
89 // Number of error correction bytes in the QR Code.
90 public int getNumECBytes() {
94 // Number of Reedsolomon blocks in the QR Code.
95 public int getNumRSBlocks() {
99 // ByteMatrix data of the QR Code.
100 public ByteMatrix getMatrix() {
105 // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They
106 // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.
107 public int at(int x, int y) {
108 // The value must be zero or one.
109 int value = matrix.get(x, y);
110 if (!(value == 0 || value == 1)) {
111 // this is really like an assert... not sure what better exception to use?
112 throw new RuntimeException("Bad value");
117 // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
119 public boolean isValid() {
121 // First check if all version are not uninitialized.
127 numTotalBytes != -1 &&
128 numDataBytes != -1 &&
131 // Then check them in other ways..
132 isValidMaskPattern(maskPattern) &&
133 numTotalBytes == numDataBytes + numECBytes &&
136 matrixWidth == matrix.getWidth() &&
137 // See 7.3.1 of JISX0510:2004 (p.5).
138 matrix.getWidth() == matrix.getHeight(); // Must be square.
141 // Return debug String.
142 public String toString() {
143 StringBuffer result = new StringBuffer(200);
144 result.append("<<\n");
145 result.append(" mode: ");
147 result.append("\n ecLevel: ");
148 result.append(ecLevel);
149 result.append("\n version: ");
150 result.append(version);
151 result.append("\n matrixWidth: ");
152 result.append(matrixWidth);
153 result.append("\n maskPattern: ");
154 result.append(maskPattern);
155 result.append("\n numTotalBytes: ");
156 result.append(numTotalBytes);
157 result.append("\n numDataBytes: ");
158 result.append(numDataBytes);
159 result.append("\n numECBytes: ");
160 result.append(numECBytes);
161 result.append("\n numRSBlocks: ");
162 result.append(numRSBlocks);
163 if (matrix == null) {
164 result.append("\n matrix: null\n");
166 result.append("\n matrix:\n");
167 result.append(matrix.toString());
169 result.append(">>\n");
170 return result.toString();
173 public void setMode(Mode value) {
177 public void setECLevel(ErrorCorrectionLevel value) {
181 public void setVersion(int value) {
185 public void setMatrixWidth(int value) {
189 public void setMaskPattern(int value) {
193 public void setNumTotalBytes(int value) {
194 numTotalBytes = value;
197 public void setNumDataBytes(int value) {
198 numDataBytes = value;
201 public void setNumECBytes(int value) {
205 public void setNumRSBlocks(int value) {
209 // This takes ownership of the 2D array.
210 public void setMatrix(ByteMatrix value) {
214 // Check if "mask_pattern" is valid.
215 public static boolean isValidMaskPattern(int maskPattern) {
216 return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
219 // Return true if the all values in the matrix are binary numbers.
221 // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
222 // production. I'm leaving it because it may be useful for testing. It should be removed entirely
223 // if ByteMatrix is changed never to contain a -1.
225 private static boolean EverythingIsBinary(final ByteMatrix matrix) {
226 for (int y = 0; y < matrix.height(); ++y) {
227 for (int x = 0; x < matrix.width(); ++x) {
228 int value = matrix.get(y, x);
229 if (!(value == 0 || value == 1)) {
230 // Found non zero/one value.