internal static Version decodeVersionInformation(int versionBits) { int bestDifference = System.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 4 bits. if (bestDifference <= 3) { return(getVersionForNumber(bestVersion)); } // If we didn't find a close enough match, fail return(null); }
public override bool Equals(System.Object o) { if (!(o is FormatInformation)) { return(false); } FormatInformation other = (FormatInformation)o; return(this.errorCorrectionLevel == other.errorCorrectionLevel && this._dataMask == other._dataMask); }
/// <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 formatInfoBits = 0; for (int i = 0; i < 6; i++) { formatInfoBits = copyBit(i, 8, formatInfoBits); } // .. and skip a bit in the timing pattern ... formatInfoBits = copyBit(7, 8, formatInfoBits); formatInfoBits = copyBit(8, 8, formatInfoBits); formatInfoBits = copyBit(8, 7, formatInfoBits); // .. and skip a bit in the timing pattern ... for (int j = 5; j >= 0; j--) { formatInfoBits = copyBit(8, j, formatInfoBits); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits); if (parsedFormatInfo != null) { return(parsedFormatInfo); } // Hmm, failed. Try the top-right/bottom-left pattern int dimension = bitMatrix.Dimension; formatInfoBits = 0; int iMin = dimension - 8; for (int i = dimension - 1; i >= iMin; i--) { formatInfoBits = copyBit(i, 8, formatInfoBits); } for (int j = dimension - 7; j < dimension; j++) { formatInfoBits = copyBit(8, j, formatInfoBits); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits); if (parsedFormatInfo != null) { return(parsedFormatInfo); } throw ReaderException.Instance; }
/// <param name="maskedFormatInfo">format info indicator, with mask still applied /// </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 maskedFormatInfo) { FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo); 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(maskedFormatInfo ^ 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 formatInfoBits = 0; for (int i = 0; i < 6; i++) { formatInfoBits = copyBit(i, 8, formatInfoBits); } // .. and skip a bit in the timing pattern ... formatInfoBits = copyBit(7, 8, formatInfoBits); formatInfoBits = copyBit(8, 8, formatInfoBits); formatInfoBits = copyBit(8, 7, formatInfoBits); // .. and skip a bit in the timing pattern ... for (int j = 5; j >= 0; j--) { formatInfoBits = copyBit(8, j, formatInfoBits); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits); if (parsedFormatInfo != null) { return parsedFormatInfo; } // Hmm, failed. Try the top-right/bottom-left pattern int dimension = bitMatrix.Dimension; formatInfoBits = 0; int iMin = dimension - 8; for (int i = dimension - 1; i >= iMin; i--) { formatInfoBits = copyBit(i, 8, formatInfoBits); } for (int j = dimension - 7; j < dimension; j++) { formatInfoBits = copyBit(8, j, formatInfoBits); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits); if (parsedFormatInfo != null) { return parsedFormatInfo; } throw ReaderException.Instance; }
/// <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 sbyte[] readCodewords() { FormatInformation formatInfo = readFormatInformation(); Version version = readVersion(); // 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((int)formatInfo.DataMask); int dimension = bitMatrix.Dimension; dataMask.unmaskBitMatrix(bitMatrix, dimension); BitMatrix functionPattern = version.buildFunctionPattern(); bool readingUp = true; sbyte[] result = new sbyte[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.get_Renamed(j - col, i)) { // Read a bit bitsRead++; currentByte <<= 1; if (bitMatrix.get_Renamed(j - col, i)) { currentByte |= 1; } // If we've made a whole byte, save it off if (bitsRead == 8) { result[resultOffset++] = (sbyte)currentByte; bitsRead = 0; currentByte = 0; } } } } readingUp ^= true; // readingUp = !readingUp; // switch directions } if (resultOffset != version.TotalCodewords) { throw ReaderException.Instance; } return(result); }