示例#1
0
        internal static System.String decode(sbyte[] bytes, Version version)
        {
            BitSource bits = new BitSource(bytes);

            System.Text.StringBuilder result = new System.Text.StringBuilder();
            Mode mode;

            do
            {
                // While still another segment to read...
                mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
                if (!mode.Equals(Mode.TERMINATOR))
                {
                    // How many characters will follow, encoded in this mode?
                    int count = bits.readBits(mode.getCharacterCountBits(version));
                    if (mode.Equals(Mode.NUMERIC))
                    {
                        decodeNumericSegment(bits, result, count);
                    }
                    else if (mode.Equals(Mode.ALPHANUMERIC))
                    {
                        decodeAlphanumericSegment(bits, result, count);
                    }
                    else if (mode.Equals(Mode.BYTE))
                    {
                        decodeByteSegment(bits, result, count);
                    }
                    else if (mode.Equals(Mode.KANJI))
                    {
                        decodeKanjiSegment(bits, result, count);
                    }
                    else
                    {
                        throw new ReaderException("Unsupported mode indicator");
                    }
                }
            }while (!mode.Equals(Mode.TERMINATOR));

            // I thought it wasn't allowed to leave extra bytes after the terminator but it happens

            /*
             * int bitsLeft = bits.available();
             * if (bitsLeft > 0) {
             * if (bitsLeft > 6 || bits.readBits(bitsLeft) != 0) {
             * throw new ReaderException("Excess bits or non-zero bits after terminator mode indicator");
             * }
             * }
             */
            return(result.ToString());
        }
        internal static DecoderResult decode(sbyte[] bytes, Version version, ErrorCorrectionLevel ecLevel)
        {
            BitSource bits = new BitSource(bytes);

            System.Text.StringBuilder result = new System.Text.StringBuilder(50);
            CharacterSetECI           currentCharacterSetECI = null;
            bool fc1InEffect = false;

            System.Collections.ArrayList byteSegments = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(1));
            Mode mode;

            do
            {
                // While still another segment to read...
                if (bits.available() < 4)
                {
                    // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
                    mode = Mode.TERMINATOR;
                }
                else
                {
                    try
                    {
                        mode = Mode.forBits(bits.readBits(4));                         // mode is encoded by 4 bits
                    }
                    catch (System.ArgumentException iae)
                    {
                        throw ReaderException.Instance;
                    }
                }
                if (!mode.Equals(Mode.TERMINATOR))
                {
                    if (mode.Equals(Mode.FNC1_FIRST_POSITION) || mode.Equals(Mode.FNC1_SECOND_POSITION))
                    {
                        // We do little with FNC1 except alter the parsed result a bit according to the spec
                        fc1InEffect = true;
                    }
                    else if (mode.Equals(Mode.STRUCTURED_APPEND))
                    {
                        // not really supported; all we do is ignore it
                        // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
                        bits.readBits(16);
                    }
                    else if (mode.Equals(Mode.ECI))
                    {
                        // Count doesn't apply to ECI
                        int value_Renamed = parseECIValue(bits);
                        currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value_Renamed);
                        if (currentCharacterSetECI == null)
                        {
                            throw ReaderException.Instance;
                        }
                    }
                    else
                    {
                        // How many characters will follow, encoded in this mode?
                        int count = bits.readBits(mode.getCharacterCountBits(version));
                        if (mode.Equals(Mode.NUMERIC))
                        {
                            decodeNumericSegment(bits, result, count);
                        }
                        else if (mode.Equals(Mode.ALPHANUMERIC))
                        {
                            decodeAlphanumericSegment(bits, result, count, fc1InEffect);
                        }
                        else if (mode.Equals(Mode.BYTE))
                        {
                            decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments);
                        }
                        else if (mode.Equals(Mode.KANJI))
                        {
                            decodeKanjiSegment(bits, result, count);
                        }
                        else
                        {
                            throw ReaderException.Instance;
                        }
                    }
                }
            }while (!mode.Equals(Mode.TERMINATOR));

            return(new DecoderResult(bytes, result.ToString(), (byteSegments.Count == 0)?null:byteSegments, ecLevel));
        }
示例#3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: static com.google.zxing.common.DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, java.util.Map<com.google.zxing.DecodeHintType,?> hints) throws com.google.zxing.FormatException
        internal static DecoderResult decode(sbyte[] bytes, Version version, ErrorCorrectionLevel ecLevel, IDictionary <DecodeHintType, object> hints)
        {
            BitSource       bits         = new BitSource(bytes);
            StringBuilder   result       = new StringBuilder(50);
            IList <sbyte[]> byteSegments = new List <sbyte[]>(1);

            try
            {
                CharacterSetECI currentCharacterSetECI = null;
                bool            fc1InEffect            = false;
                Mode            mode;
                do
                {
                    // While still another segment to read...
                    if (bits.available() < 4)
                    {
                        // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
                        mode = Mode.TERMINATOR;
                    }
                    else
                    {
                        mode = Mode.forBits(bits.readBits(4));   // mode is encoded by 4 bits
                    }
                    if (mode != Mode.TERMINATOR)
                    {
                        if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION)
                        {
                            // We do little with FNC1 except alter the parsed result a bit according to the spec
                            fc1InEffect = true;
                        }
                        else if (mode == Mode.STRUCTURED_APPEND)
                        {
                            if (bits.available() < 16)
                            {
                                throw FormatException.FormatInstance;
                            }
                            // not really supported; all we do is ignore it
                            // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
                            bits.readBits(16);
                        }
                        else if (mode == Mode.ECI)
                        {
                            // Count doesn't apply to ECI
                            int value = parseECIValue(bits);
                            currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
                            if (currentCharacterSetECI == null)
                            {
                                throw FormatException.FormatInstance;
                            }
                        }
                        else
                        {
                            // First handle Hanzi mode which does not start with character count
                            if (mode == Mode.HANZI)
                            {
                                //chinese mode contains a sub set indicator right after mode indicator
                                int subset     = bits.readBits(4);
                                int countHanzi = bits.readBits(mode.getCharacterCountBits(version));
                                if (subset == GB2312_SUBSET)
                                {
                                    decodeHanziSegment(bits, result, countHanzi);
                                }
                            }
                            else
                            {
                                // "Normal" QR code modes:
                                // How many characters will follow, encoded in this mode?
                                int count = bits.readBits(mode.getCharacterCountBits(version));
                                if (mode == Mode.NUMERIC)
                                {
                                    decodeNumericSegment(bits, result, count);
                                }
                                else if (mode == Mode.ALPHANUMERIC)
                                {
                                    decodeAlphanumericSegment(bits, result, count, fc1InEffect);
                                }
                                else if (mode == Mode.BYTE)
                                {
                                    decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
                                }
                                else if (mode == Mode.KANJI)
                                {
                                    decodeKanjiSegment(bits, result, count);
                                }
                                else
                                {
                                    throw FormatException.FormatInstance;
                                }
                            }
                        }
                    }
                } while (mode != Mode.TERMINATOR);
            }
            catch (System.ArgumentException iae)
            {
                // from readBits() calls
                throw FormatException.FormatInstance;
            }

            return(new DecoderResult(bytes, result.ToString(), byteSegments.Count == 0 ? null : byteSegments, ecLevel == null ? null : ecLevel.ToString()));
        }