doEncode method

ResultList doEncode(
  1. Version? version
)

Implementation

ResultList doEncode(Version? version) {
  if (version == null) {
    // compute minimal encoding trying the three version sizes.
    final versions = [
      getVersion(VersionSize.small),
      getVersion(VersionSize.medium),
      getVersion(VersionSize.large)
    ];
    final results = [
      encodeSpecificVersion(versions[0]),
      encodeSpecificVersion(versions[1]),
      encodeSpecificVersion(versions[2])
    ];
    int smallestSize = MathUtils.maxValue;
    int smallestResult = -1;
    for (int i = 0; i < 3; i++) {
      final size = results[i].getSize();
      if (Encoder.willFit(size, versions[i], ecLevel) &&
          size < smallestSize) {
        smallestSize = size;
        smallestResult = i;
      }
    }
    if (smallestResult < 0) {
      throw WriterException('Data too big for any version');
    }
    return results[smallestResult];
  } else {
    // compute minimal encoding for a given version
    final result = encodeSpecificVersion(version);
    if (!Encoder.willFit(
      result.getSize(),
      getVersion(getVersionSize(result.getVersion())),
      ecLevel,
    )) {
      throw WriterException('Data too big for version $version');
    }
    return result;
  }
}