internal static Version decodeVersionInformation(int versionBits) { int bestDifference = Int32.MaxValue; int bestVersion = 0; for (int i = 0; i < VERSION_DECODE_INFO.Length; i++) { int targetVersion = VERSION_DECODE_INFO[i]; // Do the version info bits match exactly? done. if (targetVersion == versionBits) { return(getVersionForNumber(i + 7)); } // Otherwise see if this is the closest to a real version info bit string // we have seen so far int bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion); if (bitsDifference < bestDifference) { bestVersion = i + 7; bestDifference = bitsDifference; } } // We can tolerate up to 3 bits of error since no two version info codewords will // differ in less than 8 bits. if (bestDifference <= 3) { return(getVersionForNumber(bestVersion)); } // If we didn't find a close enough match, fail return(null); }
/// <summary> /// Decodes the format information. /// </summary> /// <param name="maskedFormatInfo1">format info indicator, with mask still applied</param> /// <param name="maskedFormatInfo2">The masked format info2.</param> /// <returns> /// information about the format it specifies, or <code>null</code> /// if doesn't seem to match any known pattern /// </returns> internal static FormatInformation decodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) { FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2); if (formatInfo != null) { return(formatInfo); } // Should return null, but, some QR codes apparently // do not mask this info. Try again by actually masking the pattern // first return(doDecodeFormatInformation(maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR, maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR)); }
/// <summary> <p>Reads format information from one of its two locations within the QR Code.</p> /// /// </summary> /// <returns> {@link FormatInformation} encapsulating the QR Code's format info /// </returns> /// <throws> ReaderException if both format information locations cannot be parsed as </throws> /// <summary> the valid encoding of format information /// </summary> internal FormatInformation readFormatInformation() { if (parsedFormatInfo != null) { return(parsedFormatInfo); } // Read top-left format info bits int formatInfoBits1 = 0; for (int i = 0; i < 6; i++) { formatInfoBits1 = copyBit(i, 8, formatInfoBits1); } // .. and skip a bit in the timing pattern ... formatInfoBits1 = copyBit(7, 8, formatInfoBits1); formatInfoBits1 = copyBit(8, 8, formatInfoBits1); formatInfoBits1 = copyBit(8, 7, formatInfoBits1); // .. and skip a bit in the timing pattern ... for (int j = 5; j >= 0; j--) { formatInfoBits1 = copyBit(8, j, formatInfoBits1); } // Read the top-right/bottom-left pattern too int dimension = bitMatrix.Height; int formatInfoBits2 = 0; int jMin = dimension - 7; for (int j = dimension - 1; j >= jMin; j--) { formatInfoBits2 = copyBit(8, j, formatInfoBits2); } for (int i = dimension - 8; i < dimension; i++) { formatInfoBits2 = copyBit(i, 8, formatInfoBits2); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2); if (parsedFormatInfo != null) { return(parsedFormatInfo); } return(null); }
/// <summary> <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the /// correct order in order to reconstitute the codewords bytes contained within the /// QR Code.</p> /// /// </summary> /// <returns> bytes encoded within the QR Code /// </returns> /// <throws> ReaderException if the exact number of bytes expected is not read </throws> internal byte[] readCodewords() { FormatInformation formatInfo = readFormatInformation(); if (formatInfo == null) { return(null); } Version version = readVersion(); if (version == null) { return(null); } // Get the data mask for the format used in this QR Code. This will exclude // some bits from reading as we wind through the bit matrix. DataMask dataMask = DataMask.forReference(formatInfo.DataMask); int dimension = bitMatrix.Height; dataMask.unmaskBitMatrix(bitMatrix, dimension); BitMatrix functionPattern = version.buildFunctionPattern(); bool readingUp = true; byte[] result = new byte[version.TotalCodewords]; int resultOffset = 0; int currentByte = 0; int bitsRead = 0; // Read columns in pairs, from right to left for (int j = dimension - 1; j > 0; j -= 2) { if (j == 6) { // Skip whole column with vertical alignment pattern; // saves time and makes the other code proceed more cleanly j--; } // Read alternatingly from bottom to top then top to bottom for (int count = 0; count < dimension; count++) { int i = readingUp ? dimension - 1 - count : count; for (int col = 0; col < 2; col++) { // Ignore bits covered by the function pattern if (!functionPattern[j - col, i]) { // Read a bit bitsRead++; currentByte <<= 1; if (bitMatrix[j - col, i]) { currentByte |= 1; } // If we've made a whole byte, save it off if (bitsRead == 8) { result[resultOffset++] = (byte)currentByte; bitsRead = 0; currentByte = 0; } } } } readingUp ^= true; // readingUp = !readingUp; // switch directions } if (resultOffset != version.TotalCodewords) { return(null); } return(result); }