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]) {
  try {
    return _doDecode(image, hints);
  } on NotFoundException catch (_) {
    if (hints?.tryHarder == true && image.isRotateSupported) {
      final rotatedImage = image.rotateCounterClockwise();
      final result = _doDecode(rotatedImage, hints);
      // Record that we found it rotated 90 degrees CCW / 270 degrees CW
      final metadata = result.resultMetadata;
      int orientation = 270;
      if (metadata != null &&
          metadata.containsKey(ResultMetadataType.orientation)) {
        // But if we found it reversed in doDecode(), add in that result here:
        orientation = (orientation +
                (metadata[ResultMetadataType.orientation] as int)) %
            360;
      }
      result.putMetadata(ResultMetadataType.orientation, orientation);
      // Update result points
      final points = result.resultPoints;
      if (points != null) {
        final height = rotatedImage.height;
        for (int i = 0; i < points.length; i++) {
          points[i] = ResultPoint(height - points[i]!.y - 1, points[i]!.x);
        }
      }
      return result;
    } else {
      rethrow;
    }
  }
}