processStructuredAppend static method

List<Result> processStructuredAppend(
  1. List<Result> results
)

Implementation

static List<Result> processStructuredAppend(List<Result> results) {
  final newResults = <Result>[];
  final saResults = <Result>[];
  for (Result result in results) {
    if (result.resultMetadata!
        .containsKey(ResultMetadataType.structuredAppendSequence)) {
      saResults.add(result);
    } else {
      newResults.add(result);
    }
  }
  if (saResults.isEmpty) {
    return results;
  }

  // sort and concatenate the SA list items
  saResults.sort(_compareResult);
  final newText = StringBuffer();
  final newRawBytes = BytesBuilder();
  final newByteSegment = BytesBuilder();
  //ByteArrayOutputStream newRawBytes = ByteArrayOutputStream();
  //ByteArrayOutputStream newByteSegment = ByteArrayOutputStream();
  for (Result saResult in saResults) {
    newText.write(saResult.text);
    final saBytes = saResult.rawBytes;
    if (saBytes != null) newRawBytes.add(saBytes);
    // @SuppressWarnings("unchecked")
    final byteSegments =
        saResult.resultMetadata?[ResultMetadataType.byteSegments]
            as Iterable<Uint8List>?;
    if (byteSegments != null) {
      for (Uint8List segment in byteSegments) {
        newByteSegment.add(segment);
      }
    }
  }

  final newResult = Result(
    newText.toString(),
    Uint8List.fromList(newRawBytes.takeBytes()),
    _noPoints,
    BarcodeFormat.qrCode,
  );
  if (newByteSegment.length > 0) {
    newResult.putMetadata(
      ResultMetadataType.byteSegments,
      [newByteSegment.toBytes()],
    );
  }
  newResults.add(newResult);
  return newResults;
}