2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 using com.google.zxing;
\r
19 using com.google.zxing.common;
\r
20 using com.google.zxing.qrcode.decoder;
\r
21 using com.google.zxing.qrcode;
\r
23 namespace com.google.zxing.qrcode.encoder
\r
26 public sealed class QRCode
\r
29 public static int NUM_MASK_PATTERNS = 8;
\r
32 private ErrorCorrectionLevel ecLevel;
\r
33 private int version;
\r
34 private int matrixWidth;
\r
35 private int maskPattern;
\r
36 private int numTotalBytes;
\r
37 private int numDataBytes;
\r
38 private int numECBytes;
\r
39 private int numRSBlocks;
\r
40 private ByteMatrix matrix;
\r
55 // Mode of the QR Code.
\r
56 public Mode getMode() {
\r
60 // Error correction level of the QR Code.
\r
61 public ErrorCorrectionLevel getECLevel() {
\r
65 // Version of the QR Code. The bigger size, the bigger version.
\r
66 public int getVersion() {
\r
70 // ByteMatrix width of the QR Code.
\r
71 public int getMatrixWidth() {
\r
75 // Mask pattern of the QR Code.
\r
76 public int getMaskPattern() {
\r
80 // Number of total bytes in the QR Code.
\r
81 public int getNumTotalBytes() {
\r
82 return numTotalBytes;
\r
85 // Number of data bytes in the QR Code.
\r
86 public int getNumDataBytes() {
\r
87 return numDataBytes;
\r
90 // Number of error correction bytes in the QR Code.
\r
91 public int getNumECBytes() {
\r
95 // Number of Reedsolomon blocks in the QR Code.
\r
96 public int getNumRSBlocks() {
\r
100 // ByteMatrix data of the QR Code.
\r
101 public ByteMatrix getMatrix() {
\r
106 // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They
\r
107 // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.
\r
108 public int at(int x, int y) {
\r
109 // The value must be zero or one.
\r
110 int value = matrix.get(y, x);
\r
111 if (!(value == 0 || value == 1)) {
\r
112 // this is really like an assert... not sure what better exception to use?
\r
113 throw new Exception("Bad value");
\r
118 // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
\r
120 public bool isValid() {
\r
122 // First check if all version are not uninitialized.
\r
126 matrixWidth != -1 &&
\r
127 maskPattern != -1 &&
\r
128 numTotalBytes != -1 &&
\r
129 numDataBytes != -1 &&
\r
130 numECBytes != -1 &&
\r
131 numRSBlocks != -1 &&
\r
132 // Then check them in other ways..
\r
133 isValidMaskPattern(maskPattern) &&
\r
134 numTotalBytes == numDataBytes + numECBytes &&
\r
135 // ByteMatrix stuff.
\r
137 matrixWidth == matrix.width() &&
\r
138 // See 7.3.1 of JISX0510:2004 (p.5).
\r
139 matrix.width() == matrix.height(); // Must be square.
\r
142 // Return debug String.
\r
143 public String toString() {
\r
144 StringBuilder result = new StringBuilder(200);
\r
145 result.Append("<<\n");
\r
146 result.Append(" mode: ");
\r
147 result.Append(mode);
\r
148 result.Append("\n ecLevel: ");
\r
149 result.Append(ecLevel);
\r
150 result.Append("\n version: ");
\r
151 result.Append(version);
\r
152 result.Append("\n matrixWidth: ");
\r
153 result.Append(matrixWidth);
\r
154 result.Append("\n maskPattern: ");
\r
155 result.Append(maskPattern);
\r
156 result.Append("\n numTotalBytes: ");
\r
157 result.Append(numTotalBytes);
\r
158 result.Append("\n numDataBytes: ");
\r
159 result.Append(numDataBytes);
\r
160 result.Append("\n numECBytes: ");
\r
161 result.Append(numECBytes);
\r
162 result.Append("\n numRSBlocks: ");
\r
163 result.Append(numRSBlocks);
\r
164 if (matrix == null) {
\r
165 result.Append("\n matrix: null\n");
\r
167 result.Append("\n matrix:\n");
\r
168 result.Append(matrix.toString());
\r
170 result.Append(">>\n");
\r
171 return result.ToString();
\r
174 public void setMode(Mode value) {
\r
178 public void setECLevel(ErrorCorrectionLevel value) {
\r
182 public void setVersion(int value) {
\r
186 public void setMatrixWidth(int value) {
\r
187 matrixWidth = value;
\r
190 public void setMaskPattern(int value) {
\r
191 maskPattern = value;
\r
194 public void setNumTotalBytes(int value) {
\r
195 numTotalBytes = value;
\r
198 public void setNumDataBytes(int value) {
\r
199 numDataBytes = value;
\r
202 public void setNumECBytes(int value) {
\r
203 numECBytes = value;
\r
206 public void setNumRSBlocks(int value) {
\r
207 numRSBlocks = value;
\r
210 // This takes ownership of the 2D array.
\r
211 public void setMatrix(ByteMatrix value) {
\r
215 // Check if "mask_pattern" is valid.
\r
216 public static bool isValidMaskPattern(int maskPattern) {
\r
217 return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
\r
220 // Return true if the all values in the matrix are binary numbers.
\r
222 // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
\r
223 // production. I'm leaving it because it may be useful for testing. It should be removed entirely
\r
224 // if ByteMatrix is changed never to contain a -1.
\r
226 private static boolean EverythingIsBinary(final ByteMatrix matrix) {
\r
227 for (int y = 0; y < matrix.height(); ++y) {
\r
228 for (int x = 0; x < matrix.width(); ++x) {
\r
229 int value = matrix.get(y, x);
\r
230 if (!(value == 0 || value == 1)) {
\r
231 // Found non zero/one value.
\r