/*\r
- * Copyright 2007 Google Inc.\r
+ * Copyright 2007 ZXing authors\r
*\r
* Licensed under the Apache License, Version 2.0 (the "License");\r
* you may not use this file except in compliance with the License.\r
private FormatInformation parsedFormatInfo;\r
\r
/**\r
- * @throws com.google.zxing.ReaderException\r
- * if dimension is not >= 21 and 1 mod 4\r
+ * @param bitMatrix {@link BitMatrix} to parse\r
+ * @throws ReaderException if dimension is not >= 21 and 1 mod 4\r
*/\r
BitMatrixParser(BitMatrix bitMatrix) throws ReaderException {\r
int dimension = bitMatrix.getDimension();\r
this.bitMatrix = bitMatrix;\r
}\r
\r
+ /**\r
+ * <p>Reads format information from one of its two locations within the QR Code.</p>\r
+ *\r
+ * @return {@link FormatInformation} encapsulating the QR Code's format info\r
+ * @throws ReaderException if both format information locations cannot be parsed as\r
+ * the valid encoding of format information\r
+ */\r
FormatInformation readFormatInformation() throws ReaderException {\r
\r
if (parsedFormatInfo != null) {\r
throw new ReaderException("Could not decode format information");\r
}\r
\r
+ /**\r
+ * <p>Reads version information from one of its two locations within the QR Code.</p>\r
+ *\r
+ * @return {@link Version} encapsulating the QR Code's version\r
+ * @throws ReaderException if both version information locations cannot be parsed as\r
+ * the valid encoding of version information\r
+ */\r
Version readVersion() throws ReaderException {\r
\r
if (parsedVersion != null) {\r
return bitMatrix.get(i, j) ? (versionBits << 1) | 0x1 : versionBits << 1;\r
}\r
\r
+ /**\r
+ * <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the\r
+ * correct order in order to reconstitute the codewords bytes contained within the\r
+ * QR Code.</p>\r
+ *\r
+ * @return bytes encoded within the QR Code\r
+ * @throws ReaderException if the exact number of bytes expected is not read\r
+ */\r
byte[] readCodewords() throws ReaderException {\r
\r
FormatInformation formatInfo = readFormatInformation();\r
Version version = readVersion();\r
\r
+ // Get the data mask for the format used in this QR Code. This will exclude\r
+ // some bits from reading as we wind through the bit matrix.\r
DataMask dataMask = DataMask.forReference((int) formatInfo.getDataMask());\r
int dimension = bitMatrix.getDimension();\r
dataMask.unmaskBitMatrix(bitMatrix.getBits(), dimension);\r
int resultOffset = 0;\r
int currentByte = 0;\r
int bitsRead = 0;\r
+ // Read columns in pairs, from right to left\r
for (int j = dimension - 1; j > 0; j -= 2) {\r
if (j == 6) {\r
// Skip whole column with vertical alignment pattern;\r
// saves time and makes the other code proceed more cleanly\r
j--;\r
}\r
+ // Read alternatingly from bottom to top then top to bottom\r
for (int count = 0; count < dimension; count++) {\r
int i = readingUp ? dimension - 1 - count : count;\r
for (int col = 0; col < 2; col++) {\r
+ // Ignore bits covered by the function pattern\r
if (!functionPattern.get(i, j - col)) {\r
+ // Read a bit\r
bitsRead++;\r
currentByte <<= 1;\r
if (bitMatrix.get(i, j - col)) {\r
currentByte |= 1;\r
}\r
+ // If we've made a whole byte, save it off\r
if (bitsRead == 8) {\r
result[resultOffset++] = (byte) currentByte;\r
bitsRead = 0;\r