Encapsulates a QR Code's format information, including the data mask used and error correction level.

Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        /// <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));
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
      /// <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;
      }
Esempio n. 5
0
 /**
  * Prepare the parser for a mirrored operation.
  * This flag has effect only on the {@link #readFormatInformation()} and the
  * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
  * {@link #mirror()} method should be called.
  * 
  * @param mirror Whether to read version and format information mirrored.
  */
 internal void setMirror(bool mirror)
 {
    parsedVersion = null;
    parsedFormatInfo = null;
    mirrored = mirror;
 }
Esempio n. 6
0
 /**
  * Prepare the parser for a mirrored operation.
  * This flag has effect only on the {@link #readFormatInformation()} and the
  * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
  * {@link #mirror()} method should be called.
  *
  * @param mirror Whether to read version and format information mirrored.
  */
 internal void setMirror(bool mirror)
 {
     parsedVersion    = null;
     parsedFormatInfo = null;
     mirrored         = mirror;
 }
Esempio n. 7
0
        /// <summary> <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
        /// correct order in order to reconstruct 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.
            int dimension = bitMatrix.Height;

            DataMask.unmaskBitMatrix(formatInfo.DataMask, 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);
        }