2 * Copyright 2008 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
17 using ByteMatrix = com.google.zxing.common.ByteMatrix;
\r
18 using ErrorCorrectionLevel = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
\r
19 using Mode = com.google.zxing.qrcode.decoder.Mode;
\r
20 namespace com.google.zxing.qrcode.encoder
\r
23 /// <author> satorux@google.com (Satoru Takabayashi) - creator
\r
25 /// <author> dswitkin@google.com (Daniel Switkin) - ported from C++
\r
27 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
29 public sealed class QRCode
\r
33 // Mode of the QR Code.
\r
46 public ErrorCorrectionLevel ECLevel
\r
48 // Error correction level of the QR Code.
\r
63 // Version of the QR Code. The bigger size, the bigger version.
\r
76 public int MatrixWidth
\r
78 // ByteMatrix width of the QR Code.
\r
87 matrixWidth = value;
\r
91 public int MaskPattern
\r
93 // Mask pattern of the QR Code.
\r
102 maskPattern = value;
\r
106 public int NumTotalBytes
\r
108 // Number of total bytes in the QR Code.
\r
112 return numTotalBytes;
\r
117 numTotalBytes = value;
\r
121 public int NumDataBytes
\r
123 // Number of data bytes in the QR Code.
\r
127 return numDataBytes;
\r
132 numDataBytes = value;
\r
136 public int NumECBytes
\r
138 // Number of error correction bytes in the QR Code.
\r
147 numECBytes = value;
\r
151 public int NumRSBlocks
\r
153 // Number of Reedsolomon blocks in the QR Code.
\r
157 return numRSBlocks;
\r
162 numRSBlocks = value;
\r
166 public ByteMatrix Matrix
\r
168 // ByteMatrix data of the QR Code.
\r
175 // This takes ownership of the 2D array.
\r
185 // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
\r
190 return mode != null && ecLevel != null && version != - 1 && matrixWidth != - 1 && maskPattern != - 1 && numTotalBytes != - 1 && numDataBytes != - 1 && numECBytes != - 1 && numRSBlocks != - 1 && isValidMaskPattern(maskPattern) && numTotalBytes == numDataBytes + numECBytes && matrix != null && matrixWidth == matrix.Width && matrix.Width == matrix.Height; // Must be square.
\r
195 public const int NUM_MASK_PATTERNS = 8;
\r
198 private ErrorCorrectionLevel ecLevel;
\r
199 private int version;
\r
200 private int matrixWidth;
\r
201 private int maskPattern;
\r
202 private int numTotalBytes;
\r
203 private int numDataBytes;
\r
204 private int numECBytes;
\r
205 private int numRSBlocks;
\r
206 private ByteMatrix matrix;
\r
215 numTotalBytes = - 1;
\r
216 numDataBytes = - 1;
\r
223 // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They
\r
224 // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.
\r
225 public int at(int x, int y)
\r
227 // The value must be zero or one.
\r
228 int value_Renamed = matrix.get_Renamed(x, y);
\r
229 if (!(value_Renamed == 0 || value_Renamed == 1))
\r
231 // this is really like an assert... not sure what better exception to use?
\r
232 throw new System.SystemException("Bad value");
\r
234 return value_Renamed;
\r
237 // Return debug String.
\r
238 public override System.String ToString()
\r
240 System.Text.StringBuilder result = new System.Text.StringBuilder(200);
\r
241 result.Append("<<\n");
\r
242 result.Append(" mode: ");
\r
243 result.Append(mode);
\r
244 result.Append("\n ecLevel: ");
\r
245 result.Append(ecLevel);
\r
246 result.Append("\n version: ");
\r
247 result.Append(version);
\r
248 result.Append("\n matrixWidth: ");
\r
249 result.Append(matrixWidth);
\r
250 result.Append("\n maskPattern: ");
\r
251 result.Append(maskPattern);
\r
252 result.Append("\n numTotalBytes: ");
\r
253 result.Append(numTotalBytes);
\r
254 result.Append("\n numDataBytes: ");
\r
255 result.Append(numDataBytes);
\r
256 result.Append("\n numECBytes: ");
\r
257 result.Append(numECBytes);
\r
258 result.Append("\n numRSBlocks: ");
\r
259 result.Append(numRSBlocks);
\r
260 if (matrix == null)
\r
262 result.Append("\n matrix: null\n");
\r
266 result.Append("\n matrix:\n");
\r
267 result.Append(matrix.ToString());
\r
269 result.Append(">>\n");
\r
270 return result.ToString();
\r
273 // Check if "mask_pattern" is valid.
\r
274 public static bool isValidMaskPattern(int maskPattern)
\r
276 return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
\r
279 // Return true if the all values in the matrix are binary numbers.
\r
281 // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
\r
282 // production. I'm leaving it because it may be useful for testing. It should be removed entirely
\r
283 // if ByteMatrix is changed never to contain a -1.
\r
285 private static boolean EverythingIsBinary(final ByteMatrix matrix) {
\r
286 for (int y = 0; y < matrix.height(); ++y) {
\r
287 for (int x = 0; x < matrix.width(); ++x) {
\r
288 int value = matrix.get(y, x);
\r
289 if (!(value == 0 || value == 1)) {
\r
290 // Found non zero/one value.
\r