Search in sources :

Example 1 with BitSource

use of com.google.zxing.common.BitSource in project weex-example by KalicyZhou.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(100);
    StringBuilder resultTrailer = new StringBuilder(0);
    List<byte[]> byteSegments = new ArrayList<>(1);
    Mode mode = Mode.ASCII_ENCODE;
    do {
        if (mode == Mode.ASCII_ENCODE) {
            mode = decodeAsciiSegment(bits, result, resultTrailer);
        } else {
            switch(mode) {
                case C40_ENCODE:
                    decodeC40Segment(bits, result);
                    break;
                case TEXT_ENCODE:
                    decodeTextSegment(bits, result);
                    break;
                case ANSIX12_ENCODE:
                    decodeAnsiX12Segment(bits, result);
                    break;
                case EDIFACT_ENCODE:
                    decodeEdifactSegment(bits, result);
                    break;
                case BASE256_ENCODE:
                    decodeBase256Segment(bits, result, byteSegments);
                    break;
                default:
                    throw FormatException.getFormatInstance();
            }
            mode = Mode.ASCII_ENCODE;
        }
    } while (mode != Mode.PAD_ENCODE && bits.available() > 0);
    if (resultTrailer.length() > 0) {
        result.append(resultTrailer);
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, null);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult)

Example 2 with BitSource

use of com.google.zxing.common.BitSource in project weex-example by KalicyZhou.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(50);
    List<byte[]> byteSegments = new ArrayList<>(1);
    int symbolSequence = -1;
    int parityData = -1;
    try {
        CharacterSetECI currentCharacterSetECI = null;
        boolean 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 is encoded by 4 bits
                mode = Mode.forBits(bits.readBits(4));
            }
            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.getFormatInstance();
                    }
                    // sequence number and parity is added later to the result metadata
                    // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
                    symbolSequence = bits.readBits(8);
                    parityData = bits.readBits(8);
                } else if (mode == Mode.ECI) {
                    // Count doesn't apply to ECI
                    int value = parseECIValue(bits);
                    currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
                    if (currentCharacterSetECI == null) {
                        throw FormatException.getFormatInstance();
                    }
                } 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.getFormatInstance();
                        }
                    }
                }
            }
        } while (mode != Mode.TERMINATOR);
    } catch (IllegalArgumentException iae) {
        // from readBits() calls
        throw FormatException.getFormatInstance();
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel == null ? null : ecLevel.toString(), symbolSequence, parityData);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult) CharacterSetECI(com.google.zxing.common.CharacterSetECI)

Example 3 with BitSource

use of com.google.zxing.common.BitSource in project zxing by zxing.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(50);
    List<byte[]> byteSegments = new ArrayList<>(1);
    int symbolSequence = -1;
    int parityData = -1;
    try {
        CharacterSetECI currentCharacterSetECI = null;
        boolean 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 is encoded by 4 bits
                mode = Mode.forBits(bits.readBits(4));
            }
            switch(mode) {
                case TERMINATOR:
                    break;
                case FNC1_FIRST_POSITION:
                case FNC1_SECOND_POSITION:
                    // We do little with FNC1 except alter the parsed result a bit according to the spec
                    fc1InEffect = true;
                    break;
                case STRUCTURED_APPEND:
                    if (bits.available() < 16) {
                        throw FormatException.getFormatInstance();
                    }
                    // sequence number and parity is added later to the result metadata
                    // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
                    symbolSequence = bits.readBits(8);
                    parityData = bits.readBits(8);
                    break;
                case ECI:
                    // Count doesn't apply to ECI
                    int value = parseECIValue(bits);
                    currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
                    if (currentCharacterSetECI == null) {
                        throw FormatException.getFormatInstance();
                    }
                    break;
                case HANZI:
                    // First handle Hanzi mode which does not start with character count
                    // 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);
                    }
                    break;
                default:
                    // "Normal" QR code modes:
                    // How many characters will follow, encoded in this mode?
                    int count = bits.readBits(mode.getCharacterCountBits(version));
                    switch(mode) {
                        case NUMERIC:
                            decodeNumericSegment(bits, result, count);
                            break;
                        case ALPHANUMERIC:
                            decodeAlphanumericSegment(bits, result, count, fc1InEffect);
                            break;
                        case BYTE:
                            decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
                            break;
                        case KANJI:
                            decodeKanjiSegment(bits, result, count);
                            break;
                        default:
                            throw FormatException.getFormatInstance();
                    }
                    break;
            }
        } while (mode != Mode.TERMINATOR);
    } catch (IllegalArgumentException iae) {
        // from readBits() calls
        throw FormatException.getFormatInstance();
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel == null ? null : ecLevel.toString(), symbolSequence, parityData);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult) CharacterSetECI(com.google.zxing.common.CharacterSetECI)

Example 4 with BitSource

use of com.google.zxing.common.BitSource in project zxing by zxing.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(100);
    StringBuilder resultTrailer = new StringBuilder(0);
    List<byte[]> byteSegments = new ArrayList<>(1);
    Mode mode = Mode.ASCII_ENCODE;
    do {
        if (mode == Mode.ASCII_ENCODE) {
            mode = decodeAsciiSegment(bits, result, resultTrailer);
        } else {
            switch(mode) {
                case C40_ENCODE:
                    decodeC40Segment(bits, result);
                    break;
                case TEXT_ENCODE:
                    decodeTextSegment(bits, result);
                    break;
                case ANSIX12_ENCODE:
                    decodeAnsiX12Segment(bits, result);
                    break;
                case EDIFACT_ENCODE:
                    decodeEdifactSegment(bits, result);
                    break;
                case BASE256_ENCODE:
                    decodeBase256Segment(bits, result, byteSegments);
                    break;
                default:
                    throw FormatException.getFormatInstance();
            }
            mode = Mode.ASCII_ENCODE;
        }
    } while (mode != Mode.PAD_ENCODE && bits.available() > 0);
    if (resultTrailer.length() > 0) {
        result.append(resultTrailer);
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, null);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult)

Aggregations

BitSource (com.google.zxing.common.BitSource)4 DecoderResult (com.google.zxing.common.DecoderResult)4 ArrayList (java.util.ArrayList)4 CharacterSetECI (com.google.zxing.common.CharacterSetECI)2