com.google.zxing.common.CharacterSetECI Java Examples

The following examples show how to use com.google.zxing.common.CharacterSetECI. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DecodedBitStreamParser.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #2
Source File: DecodedBitStreamParser.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #3
Source File: DecodedBitStreamParser.java    From reacteu-app with MIT License 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (count << 3 > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException uce) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #4
Source File: DecodedBitStreamParser.java    From weex with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #5
Source File: m.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private static void a(BitSource bitsource, StringBuilder stringbuilder, int i, CharacterSetECI characterseteci, Collection collection, Map map)
{
    if (i << 3 > bitsource.available())
    {
        throw FormatException.getFormatInstance();
    }
    byte abyte0[] = new byte[i];
    for (int j = 0; j < i; j++)
    {
        abyte0[j] = (byte)bitsource.readBits(8);
    }

    String s;
    if (characterseteci == null)
    {
        s = StringUtils.guessEncoding(abyte0, map);
    } else
    {
        s = characterseteci.name();
    }
    try
    {
        stringbuilder.append(new String(abyte0, s));
    }
    catch (UnsupportedEncodingException unsupportedencodingexception)
    {
        throw FormatException.getFormatInstance();
    }
    collection.add(abyte0);
}
 
Example #6
Source File: DecodedBitStreamParser.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #7
Source File: DecodedBitStreamParser.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits, StringBuilder result, int count,
		CharacterSetECI currentCharacterSetECI, Collection<byte[]> byteSegments, Map<DecodeHintType, ?> hints)
		throws FormatException {
	// Don't crash trying to read more bits than we have available.
	if (8 * count > bits.available()) {
		throw FormatException.getFormatInstance();
	}

	byte[] readBytes = new byte[count];
	for (int i = 0; i < count; i++) {
		readBytes[i] = (byte) bits.readBits(8);
	}
	String encoding;
	if (currentCharacterSetECI == null) {
		// The spec isn't clear on this mode; see
		// section 6.4.5: t does not say which encoding to assuming
		// upon decoding. I have seen ISO-8859-1 used as well as
		// Shift_JIS -- without anything like an ECI designator to
		// give a hint.
		encoding = StringUtils.guessEncoding(readBytes, hints);
	} else {
		encoding = currentCharacterSetECI.name();
	}
	try {
		result.append(new String(readBytes, encoding));
	} catch (UnsupportedEncodingException ignored) {
		throw FormatException.getFormatInstance();
	}
	byteSegments.add(readBytes);
}
 
Example #8
Source File: DecodedBitStreamParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #9
Source File: DecodedBitStreamParser.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #10
Source File: DecodedBitStreamParser.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits, StringBuilder result, int count, CharacterSetECI currentCharacterSetECI,
        Collection<byte[]> byteSegments, Map<DecodeHintType, ?> hints) throws FormatException {
    // Don't crash trying to read more bits than we have available.
    if (count << 3 > bits.available()) {
        throw FormatException.getFormatInstance();
    }

    byte[] readBytes = new byte[count];
    for (int i = 0; i < count; i++) {
        readBytes[i] = (byte) bits.readBits(8);
    }
    String encoding;
    if (currentCharacterSetECI == null) {
        // The spec isn't clear on this mode; see
        // section 6.4.5: t does not say which encoding to assuming
        // upon decoding. I have seen ISO-8859-1 used as well as
        // Shift_JIS -- without anything like an ECI designator to
        // give a hint.
        encoding = StringUtils.guessEncoding(readBytes, hints);
    } else {
        encoding = currentCharacterSetECI.name();
    }
    try {
        result.append(new String(readBytes, encoding));
    } catch (UnsupportedEncodingException uce) {
        throw FormatException.getFormatInstance();
    }
    byteSegments.add(readBytes);
}
 
Example #11
Source File: DecodedBitStreamParser.java    From android-quick-response-code with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits, StringBuilder result, int count, CharacterSetECI currentCharacterSetECI,
        Collection<byte[]> byteSegments, Map<DecodeHintType, ?> hints) throws FormatException {
    // Don't crash trying to read more bits than we have available.
    if (count << 3 > bits.available()) {
        throw FormatException.getFormatInstance();
    }

    byte[] readBytes = new byte[count];
    for (int i = 0; i < count; i++) {
        readBytes[i] = (byte) bits.readBits(8);
    }
    String encoding;
    if (currentCharacterSetECI == null) {
        // The spec isn't clear on this mode; see
        // section 6.4.5: t does not say which encoding to assuming
        // upon decoding. I have seen ISO-8859-1 used as well as
        // Shift_JIS -- without anything like an ECI designator to
        // give a hint.
        encoding = StringUtils.guessEncoding(readBytes, hints);
    } else {
        encoding = currentCharacterSetECI.name();
    }
    try {
        result.append(new String(readBytes, encoding));
    } catch (UnsupportedEncodingException uce) {
        throw FormatException.getFormatInstance();
    }
    byteSegments.add(readBytes);
}
 
Example #12
Source File: DecodedBitStreamParser.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #13
Source File: DecodedBitStreamParser.java    From ScreenCapture with MIT License 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #14
Source File: DecodedBitStreamParser.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private static void decodeByteSegment(BitSource bits,
                                      StringBuilder result,
                                      int count,
                                      CharacterSetECI currentCharacterSetECI,
                                      Collection<byte[]> byteSegments,
                                      Map<DecodeHintType,?> hints) throws FormatException {
  // Don't crash trying to read more bits than we have available.
  if (8 * count > bits.available()) {
    throw FormatException.getFormatInstance();
  }

  byte[] readBytes = new byte[count];
  for (int i = 0; i < count; i++) {
    readBytes[i] = (byte) bits.readBits(8);
  }
  String encoding;
  if (currentCharacterSetECI == null) {
    // The spec isn't clear on this mode; see
    // section 6.4.5: t does not say which encoding to assuming
    // upon decoding. I have seen ISO-8859-1 used as well as
    // Shift_JIS -- without anything like an ECI designator to
    // give a hint.
    encoding = StringUtils.guessEncoding(readBytes, hints);
  } else {
    encoding = currentCharacterSetECI.name();
  }
  try {
    result.append(new String(readBytes, encoding));
  } catch (UnsupportedEncodingException ignored) {
    throw FormatException.getFormatInstance();
  }
  byteSegments.add(readBytes);
}
 
Example #15
Source File: Encoder.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints)
		throws WriterException {

	// Determine what character encoding has been specified by the caller,
	// if any
	String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
	if (encoding == null) {
		encoding = DEFAULT_BYTE_MODE_ENCODING;
	}

	// Pick an encoding mode appropriate for the content. Note that this
	// will not attempt to use
	// multiple modes / segments even if that were more efficient. Twould be
	// nice.
	Mode mode = chooseMode(content, encoding);

	// This will store the header information, like mode and
	// length, as well as "header" segments like an ECI segment.
	BitArray headerBits = new BitArray();

	// Append ECI segment if applicable
	if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
		CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
		if (eci != null) {
			appendECI(eci, headerBits);
		}
	}

	// (With ECI in place,) Write the mode marker
	appendModeInfo(mode, headerBits);

	// Collect data within the main segment, separately, to count its size
	// if needed. Don't add it to
	// main payload yet.
	BitArray dataBits = new BitArray();
	appendBytes(content, mode, dataBits, encoding);

	// Hard part: need to know version to know how many bits length takes.
	// But need to know how many
	// bits it takes to know version. First we take a guess at version by
	// assuming version will be
	// the minimum, 1:

	int provisionalBitsNeeded = headerBits.getSize() + mode.getCharacterCountBits(Version.getVersionForNumber(1))
			+ dataBits.getSize();
	Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

	// Use that guess to calculate the right version. I am still not sure
	// this works in 100% of cases.

	int bitsNeeded = headerBits.getSize() + mode.getCharacterCountBits(provisionalVersion) + dataBits.getSize();
	Version version = chooseVersion(bitsNeeded, ecLevel);

	BitArray headerAndDataBits = new BitArray();
	headerAndDataBits.appendBitArray(headerBits);
	// Find "length" of main segment and write it
	int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
	appendLengthInfo(numLetters, version, mode, headerAndDataBits);
	// Put data together into the overall payload
	headerAndDataBits.appendBitArray(dataBits);

	Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
	int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

	// Terminate the bits properly.
	terminateBits(numDataBytes, headerAndDataBits);

	// Interleave data bits with error correction code.
	BitArray finalBits = interleaveWithECBytes(headerAndDataBits, version.getTotalCodewords(), numDataBytes,
			ecBlocks.getNumBlocks());

	QRCode qrCode = new QRCode();

	qrCode.setECLevel(ecLevel);
	qrCode.setMode(mode);
	qrCode.setVersion(version);

	// Choose the mask pattern and set to "qrCode".
	int dimension = version.getDimensionForVersion();
	ByteMatrix matrix = new ByteMatrix(dimension, dimension);
	int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
	qrCode.setMaskPattern(maskPattern);

	// Build the matrix and set it to "qrCode".
	MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
	qrCode.setMatrix(matrix);

	return qrCode;
}
 
Example #16
Source File: Encoder.java    From android-quick-response-code with Apache License 2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
    bits.appendBits(Mode.ECI.getBits(), 4);
    // This is correct for values up to 127, which is all we need now.
    bits.appendBits(eci.getValue(), 8);
}
 
Example #17
Source File: Encoder.java    From reacteu-app with MIT License 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
  if (encoding == null) {
    encoding = DEFAULT_BYTE_MODE_ENCODING;
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  // Hard part: need to know version to know how many bits length takes. But need to know how many
  // bits it takes to know version. First we take a guess at version by assuming version will be
  // the minimum, 1:

  int provisionalBitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(Version.getVersionForNumber(1))
      + dataBits.getSize();
  Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

  // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

  int bitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(provisionalVersion)
      + dataBits.getSize();
  Version version = chooseVersion(bitsNeeded, ecLevel);

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example #18
Source File: Encoder.java    From reacteu-app with MIT License 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
  bits.appendBits(Mode.ECI.getBits(), 4);
  // This is correct for values up to 127, which is all we need now.
  bits.appendBits(eci.getValue(), 8);
}
 
Example #19
Source File: DecodedBitStreamParser.java    From reacteu-app with MIT License 4 votes vote down vote up
static DecoderResult decode(byte[] bytes,
                            Version version,
                            ErrorCorrectionLevel ecLevel,
                            Map<DecodeHintType,?> hints) throws FormatException {
  BitSource bits = new BitSource(bytes);
  StringBuilder result = new StringBuilder(50);
  CharacterSetECI currentCharacterSetECI = null;
  boolean fc1InEffect = false;
  List<byte[]> byteSegments = new ArrayList<byte[]>(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 (IllegalArgumentException iae) {
        throw FormatException.getFormatInstance();
      }
    }
    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();
        }
        // 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.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);

  return new DecoderResult(bytes,
                           result.toString(),
                           byteSegments.isEmpty() ? null : byteSegments,
                           ecLevel == null ? null : ecLevel.toString());
}
 
Example #20
Source File: DecodedBitStreamParser.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
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 = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
      }
      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);
}
 
Example #21
Source File: DecodedBitStreamParser.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(50);
    CharacterSetECI currentCharacterSetECI = null;
    boolean fc1InEffect = false;
    List<byte[]> byteSegments = new ArrayList<byte[]>(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 (IllegalArgumentException iae) {
                throw FormatException.getFormatInstance();
            }
        }
        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) {
                // 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.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);

    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel == null ? null : ecLevel.toString());
}
 
Example #22
Source File: Encoder.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
private static void a(CharacterSetECI characterseteci, BitArray bitarray)
{
    bitarray.appendBits(Mode.ECI.getBits(), 4);
    bitarray.appendBits(characterseteci.getValue(), 8);
}
 
Example #23
Source File: Encoder.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
  bits.appendBits(Mode.ECI.getBits(), 4);
  // This is correct for values up to 127, which is all we need now.
  bits.appendBits(eci.getValue(), 8);
}
 
Example #24
Source File: Encoder.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
public static void encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints, QRCode qrCode) throws WriterException {

        String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
        if (encoding == null) {
            encoding = DEFAULT_BYTE_MODE_ENCODING;
        }

        // Step 1: Choose the mode (encoding).
        Mode mode = chooseMode(content, encoding);

        // Step 2: Append "bytes" into "dataBits" in appropriate encoding.
        BitArray dataBits = new BitArray();
        appendBytes(content, mode, dataBits, encoding);
        // Step 3: Initialize QR code that can contain "dataBits".
        int numInputBits = dataBits.getSize();
        initQRCode(numInputBits, ecLevel, mode, qrCode);

        // Step 4: Build another bit vector that contains header and data.
        BitArray headerAndDataBits = new BitArray();

        // Step 4.5: Append ECI message if applicable
        if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
            CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
            if (eci != null) {
                appendECI(eci, headerAndDataBits);
            }
        }

        appendModeInfo(mode, headerAndDataBits);

        int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
        appendLengthInfo(numLetters, qrCode.getVersion(), mode, headerAndDataBits);
        headerAndDataBits.appendBitArray(dataBits);

        // Step 5: Terminate the bits properly.
        terminateBits(qrCode.getNumDataBytes(), headerAndDataBits);

        // Step 6: Interleave data bits with error correction code.
        BitArray finalBits = new BitArray();
        interleaveWithECBytes(headerAndDataBits, qrCode.getNumTotalBytes(), qrCode.getNumDataBytes(), qrCode.getNumRSBlocks(), finalBits);

        // Step 7: Choose the mask pattern and set to "qrCode".
        ByteMatrix matrix = new ByteMatrix(qrCode.getMatrixWidth(), qrCode.getMatrixWidth());
        qrCode.setMaskPattern(chooseMaskPattern(finalBits, ecLevel, qrCode.getVersion(), matrix));

        // Step 8. Build the matrix and set it to "qrCode".
        MatrixUtil.buildMatrix(finalBits, ecLevel, qrCode.getVersion(), qrCode.getMaskPattern(), matrix);
        qrCode.setMatrix(matrix);
        // Step 9. Make sure we have a valid QR Code.
        if (!qrCode.isValid()) {
            throw new WriterException("Invalid QR code: " + qrCode.toString());
        }
    }
 
Example #25
Source File: Encoder.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
	bits.appendBits(Mode.ECI.getBits(), 4);
	// This is correct for values up to 127, which is all we need now.
	bits.appendBits(eci.getValue(), 8);
}
 
Example #26
Source File: Encoder.java    From android-quick-response-code with Apache License 2.0 4 votes vote down vote up
public static void encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints, QRCode qrCode) throws WriterException {

        String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
        if (encoding == null) {
            encoding = DEFAULT_BYTE_MODE_ENCODING;
        }

        // Step 1: Choose the mode (encoding).
        Mode mode = chooseMode(content, encoding);

        // Step 2: Append "bytes" into "dataBits" in appropriate encoding.
        BitArray dataBits = new BitArray();
        appendBytes(content, mode, dataBits, encoding);
        // Step 3: Initialize QR code that can contain "dataBits".
        int numInputBits = dataBits.getSize();
        initQRCode(numInputBits, ecLevel, mode, qrCode);

        // Step 4: Build another bit vector that contains header and data.
        BitArray headerAndDataBits = new BitArray();

        // Step 4.5: Append ECI message if applicable
        if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
            CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
            if (eci != null) {
                appendECI(eci, headerAndDataBits);
            }
        }

        appendModeInfo(mode, headerAndDataBits);

        int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
        appendLengthInfo(numLetters, qrCode.getVersion(), mode, headerAndDataBits);
        headerAndDataBits.appendBitArray(dataBits);

        // Step 5: Terminate the bits properly.
        terminateBits(qrCode.getNumDataBytes(), headerAndDataBits);

        // Step 6: Interleave data bits with error correction code.
        BitArray finalBits = new BitArray();
        interleaveWithECBytes(headerAndDataBits, qrCode.getNumTotalBytes(), qrCode.getNumDataBytes(), qrCode.getNumRSBlocks(), finalBits);

        // Step 7: Choose the mask pattern and set to "qrCode".
        ByteMatrix matrix = new ByteMatrix(qrCode.getMatrixWidth(), qrCode.getMatrixWidth());
        qrCode.setMaskPattern(chooseMaskPattern(finalBits, ecLevel, qrCode.getVersion(), matrix));

        // Step 8. Build the matrix and set it to "qrCode".
        MatrixUtil.buildMatrix(finalBits, ecLevel, qrCode.getVersion(), qrCode.getMaskPattern(), matrix);
        qrCode.setMatrix(matrix);
        // Step 9. Make sure we have a valid QR Code.
        if (!qrCode.isValid()) {
            throw new WriterException("Invalid QR code: " + qrCode.toString());
        }
    }
 
Example #27
Source File: DecodedBitStreamParser.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
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 = 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.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);
}
 
Example #28
Source File: DecodedBitStreamParser.java    From android-quick-response-code with Apache License 2.0 4 votes vote down vote up
static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(50);
    CharacterSetECI currentCharacterSetECI = null;
    boolean fc1InEffect = false;
    List<byte[]> byteSegments = new ArrayList<byte[]>(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 (IllegalArgumentException iae) {
                throw FormatException.getFormatInstance();
            }
        }
        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) {
                // 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.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);

    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel == null ? null : ecLevel.toString());
}
 
Example #29
Source File: Encoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
  bits.appendBits(Mode.ECI.getBits(), 4);
  // This is correct for values up to 127, which is all we need now.
  bits.appendBits(eci.getValue(), 8);
}
 
Example #30
Source File: DecodedBitStreamParser.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
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 = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
      }
      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);
}