decode method

  1. @override
Result decode(
  1. BinaryBitmap image, [
  2. DecodeHint? hints
])
override

Locates and decodes a barcode in some format within an image. This method also accepts hints, each possibly associated to some data, which may help the implementation decode.

@param image image of barcode to decode @param hints passed as a Map from DecodeHintType to arbitrary data. The meaning of the data depends upon the hint type. The implementation may or may not do anything with these hints. @return String which the barcode encodes @throws NotFoundException if no potential barcode is found @throws ChecksumException if a potential barcode is found but does not pass its checksum @throws FormatException if a potential barcode is found but format is invalid

Implementation

@override
Result decode(BinaryBitmap image, [DecodeHint? hints]) {
  NotFoundException? notFoundException;
  FormatsException? formatException;
  final detector = Detector(image.blackMatrix);
  List<ResultPoint>? points;
  DecoderResult? decoderResult;
  int errorsCorrected = 0;
  try {
    final detectorResult = detector.detect(false);
    points = detectorResult.points;
    errorsCorrected = detectorResult.errorsCorrected;
    decoderResult = Decoder().decode(detectorResult);
  } on NotFoundException catch (e) {
    notFoundException = e;
  } on FormatsException catch (e) {
    formatException = e;
  }
  if (decoderResult == null) {
    try {
      final detectorResult = detector.detect(true);
      points = detectorResult.points;
      errorsCorrected = detectorResult.errorsCorrected;
      decoderResult = Decoder().decode(detectorResult);
    } on NotFoundException catch (_) {
      if (notFoundException != null) {
        throw notFoundException;
      }
      rethrow;
    } on FormatsException catch (_) {
      if (formatException != null) {
        throw formatException;
      }
      rethrow;
    }
  }

  if (hints != null) {
    final rpcb = hints.needResultPointCallback;
    if (rpcb != null) {
      for (ResultPoint point in points!) {
        rpcb.foundPossibleResultPoint(point);
      }
    }
  }

  final result = Result.full(
    decoderResult.text,
    decoderResult.rawBytes,
    decoderResult.numBits,
    points!,
    BarcodeFormat.aztec,
    DateTime.now().millisecondsSinceEpoch,
  );

  final byteSegments = decoderResult.byteSegments;
  if (byteSegments != null) {
    result.putMetadata(ResultMetadataType.byteSegments, byteSegments);
  }
  final ecLevel = decoderResult.ecLevel;
  if (ecLevel != null) {
    result.putMetadata(ResultMetadataType.errorCorrectionLevel, ecLevel);
  }
  errorsCorrected += decoderResult.errorsCorrected ?? 0;
  result.putMetadata(ResultMetadataType.errorsCorrected, errorsCorrected);
  result.putMetadata(
    ResultMetadataType.symbologyIdentifier,
    ']z${decoderResult.symbologyModifier}',
  );

  return result;
}