decode static method

DecoderResult decode(
  1. Uint8List bytes
)

Implementation

static DecoderResult decode(Uint8List bytes) {
  final bits = BitSource(bytes);
  final result = ECIStringBuilder();
  final resultTrailer = StringBuilder();
  final byteSegments = <Uint8List>[];
  _Mode mode = _Mode.asciiEncode;
  // Could be replaceable by looking directly at 'bytes', if we're sure of not having to account for multi byte values.
  final fnc1Positions = <int>{};
  int symbologyModifier = 0;
  bool isECIencoded = false;
  do {
    if (mode == _Mode.asciiEncode) {
      mode = _decodeAsciiSegment(bits, result, resultTrailer, fnc1Positions);
    } else {
      switch (mode) {
        case _Mode.c40Encode:
          _decodeC40Segment(bits, result, fnc1Positions);
          break;
        case _Mode.textEncode:
          _decodeTextSegment(bits, result, fnc1Positions);
          break;
        case _Mode.ansix12Encode:
          _decodeAnsiX12Segment(bits, result);
          break;
        case _Mode.edifactEncode:
          _decodeEdifactSegment(bits, result);
          break;
        case _Mode.base256Encode:
          _decodeBase256Segment(bits, result, byteSegments);
          break;
        case _Mode.eciEncode:
          _decodeECISegment(bits, result);
          // ECI detection only, atm continue decoding as ASCII
          isECIencoded = true;
          break;
        default:
          throw FormatsException.instance;
      }
      mode = _Mode.asciiEncode;
    }
  } while (mode != _Mode.padEncode && bits.available() > 0);
  if (resultTrailer.length > 0) {
    result.write(resultTrailer);
  }
  if (isECIencoded) {
    // Examples for this numbers can be found in this documentation of a hardware barcode scanner:
    // https://honeywellaidc.force.com/supportppr/s/article/List-of-barcode-symbology-AIM-Identifiers
    if (fnc1Positions.contains(0) || fnc1Positions.contains(4)) {
      symbologyModifier = 5;
    } else if (fnc1Positions.contains(1) || fnc1Positions.contains(5)) {
      symbologyModifier = 6;
    } else {
      symbologyModifier = 4;
    }
  } else {
    if (fnc1Positions.contains(0) || fnc1Positions.contains(4)) {
      symbologyModifier = 2;
    } else if (fnc1Positions.contains(1) || fnc1Positions.contains(5)) {
      symbologyModifier = 3;
    } else {
      symbologyModifier = 1;
    }
  }

  return DecoderResult(
    bytes,
    result.toString(),
    byteSegments.isEmpty ? null : byteSegments,
    null,
    symbologyModifier: symbologyModifier,
  );
}