snapshot method

Future<Image?> snapshot([
  1. Barcode? barcode
])

barcode (default this.barcode) cut out of the camera frame or the photo it was found in and straightened: a flat grayscale image with a little margin, its contrast stretched. Each call makes a new image, which the caller disposes.

onDetect: (capture) async {
  final ui.Image? image = await capture.snapshot();
  history.add(capture.barcode, image);
}

null for captures not made by a ScannerController, and for a barcode not in barcodes.

Implementation

Future<ui.Image?> snapshot([Barcode? barcode]) async {
  final int index = barcode == null ? 0 : barcodes.indexOf(barcode);
  final List<BarcodePosition>? positions = _positions;
  if (index < 0 || positions == null) {
    return null;
  }
  FrameCut? cut = _cut;
  BarcodePosition position = positions[index];
  final Uint8List? encoded = _encoded;
  if (cut == null && encoded != null) {
    // The reader sees the file's pixels, the decoder here turns them.
    final int orientation = exifOrientation(encoded);
    final List<BarcodePosition> shown = <BarcodePosition>[
      for (final BarcodePosition p in positions)
        orientedPosition(p, imageSize, orientation),
    ];
    final (FrameCut, double)? decoded = await cutEncoded(
      encoded,
      orientedSize(imageSize, orientation),
      shown,
      linear: <bool>[for (final Barcode b in barcodes) b.format.isLinear],
    );
    if (decoded == null) {
      return null;
    }
    cut = decoded.$1;
    position = scalePosition(shown[index], decoded.$2);
  }
  if (cut == null) {
    return null;
  }
  final bool linear = barcodes[index].format.isLinear;
  List<Offset> corners = snapshotCorners(
    position,
    linear: linear,
    mirrored: _mirrored,
  );
  if (linear) {
    corners = fitBarHeight(
      cut,
      alignToBars(cut, corners, mirrored: _mirrored),
    );
  }
  final GrayImage? image = straighten(cut, corners);
  return image?.toImage();
}