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.common.ByteMatrix;
20 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
21 import com.google.zxing.qrcode.decoder.Mode;
24 * @author satorux@google.com (Satoru Takabayashi) - creator
25 * @author dswitkin@google.com (Daniel Switkin) - ported from C++
27 public final class QRCode {
29 public static final int NUM_MASK_PATTERNS = 8;
32 private ErrorCorrectionLevel ecLevel;
34 private int matrixWidth;
35 private int maskPattern;
36 private int numTotalBytes;
37 private int numDataBytes;
38 private int numECBytes;
39 private int numRSBlocks;
40 private ByteMatrix matrix;
55 // Mode of the QR Code.
56 public Mode getMode() {
60 // Error correction level of the QR Code.
61 public ErrorCorrectionLevel getECLevel() {
65 // Version of the QR Code. The bigger size, the bigger version.
66 public int getVersion() {
70 // ByteMatrix width of the QR Code.
71 public int getMatrixWidth() {
75 // Mask pattern of the QR Code.
76 public int getMaskPattern() {
80 // Number of total bytes in the QR Code.
81 public int getNumTotalBytes() {
85 // Number of data bytes in the QR Code.
86 public int getNumDataBytes() {
90 // Number of error correction bytes in the QR Code.
91 public int getNumECBytes() {
95 // Number of Reedsolomon blocks in the QR Code.
96 public int getNumRSBlocks() {
100 // ByteMatrix data of the QR Code.
101 public ByteMatrix getMatrix() {
106 // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They
107 // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.
108 public int at(int x, int y) {
109 // The value must be zero or one.
110 int value = matrix.get(y, x);
111 if (!(value == 0 || value == 1)) {
112 // this is really like an assert... not sure what better exception to use?
113 throw new RuntimeException("Bad value");
118 // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
120 public boolean isValid() {
122 // First check if all version are not uninitialized.
128 numTotalBytes != -1 &&
129 numDataBytes != -1 &&
132 // Then check them in other ways..
133 isValidMaskPattern(maskPattern) &&
134 numTotalBytes == numDataBytes + numECBytes &&
137 matrixWidth == matrix.width() &&
138 // See 7.3.1 of JISX0510:2004 (p.5).
139 matrix.width() == matrix.height(); // Must be square.
142 // Return debug String.
143 public String toString() {
144 StringBuffer result = new StringBuffer(200);
145 result.append("<<\n");
146 result.append(" mode: ");
148 result.append("\n ecLevel: ");
149 result.append(ecLevel);
150 result.append("\n version: ");
151 result.append(version);
152 result.append("\n matrixWidth: ");
153 result.append(matrixWidth);
154 result.append("\n maskPattern: ");
155 result.append(maskPattern);
156 result.append("\n numTotalBytes: ");
157 result.append(numTotalBytes);
158 result.append("\n numDataBytes: ");
159 result.append(numDataBytes);
160 result.append("\n numECBytes: ");
161 result.append(numECBytes);
162 result.append("\n numRSBlocks: ");
163 result.append(numRSBlocks);
164 if (matrix == null) {
165 result.append("\n matrix: null\n");
167 result.append("\n matrix:\n");
168 result.append(matrix.toString());
170 result.append(">>\n");
171 return result.toString();
174 public void setMode(Mode value) {
178 public void setECLevel(ErrorCorrectionLevel value) {
182 public void setVersion(int value) {
186 public void setMatrixWidth(int value) {
190 public void setMaskPattern(int value) {
194 public void setNumTotalBytes(int value) {
195 numTotalBytes = value;
198 public void setNumDataBytes(int value) {
199 numDataBytes = value;
202 public void setNumECBytes(int value) {
206 public void setNumRSBlocks(int value) {
210 // This takes ownership of the 2D array.
211 public void setMatrix(ByteMatrix value) {
215 // Check if "mask_pattern" is valid.
216 public static boolean isValidMaskPattern(int maskPattern) {
217 return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
220 // Return true if the all values in the matrix are binary numbers.
222 // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
223 // production. I'm leaving it because it may be useful for testing. It should be removed entirely
224 // if ByteMatrix is changed never to contain a -1.
226 private static boolean EverythingIsBinary(final ByteMatrix matrix) {
227 for (int y = 0; y < matrix.height(); ++y) {
228 for (int x = 0; x < matrix.width(); ++x) {
229 int value = matrix.get(y, x);
230 if (!(value == 0 || value == 1)) {
231 // Found non zero/one value.