Navaron Bracke

compute the size for web barcodes

@@ -152,7 +152,7 @@ class Barcode { @@ -152,7 +152,7 @@ class Barcode {
152 /// This is null if the raw value is not available. 152 /// This is null if the raw value is not available.
153 final String? rawValue; 153 final String? rawValue;
154 154
155 - /// The size of the barcode bounding box. 155 + /// The normalized size of the barcode bounding box.
156 /// 156 ///
157 /// If the bounding box is unavailable, this will be [Size.zero]. 157 /// If the bounding box is unavailable, this will be [Size.zero].
158 final Size size; 158 final Size size;
@@ -75,13 +75,33 @@ extension type Result(JSObject _) implements JSObject { @@ -75,13 +75,33 @@ extension type Result(JSObject _) implements JSObject {
75 75
76 /// Convert this result to a [Barcode]. 76 /// Convert this result to a [Barcode].
77 Barcode get toBarcode { 77 Barcode get toBarcode {
  78 + final List<Offset> corners = resultPoints;
  79 +
78 return Barcode( 80 return Barcode(
79 - corners: resultPoints, 81 + corners: corners,
80 format: barcodeFormat, 82 format: barcodeFormat,
81 displayValue: text, 83 displayValue: text,
82 rawBytes: rawBytes, 84 rawBytes: rawBytes,
83 rawValue: text, 85 rawValue: text,
  86 + size: _computeSize(corners),
84 type: BarcodeType.text, 87 type: BarcodeType.text,
85 ); 88 );
86 } 89 }
  90 +
  91 + Size _computeSize(List<Offset> points) {
  92 + if (points.length != 4) {
  93 + return Size.zero;
  94 + }
  95 +
  96 + final Iterable<double> xCoords = points.map((p) => p.dx);
  97 + final Iterable<double> yCoords = points.map((p) => p.dy);
  98 +
  99 + // Find the minimum and maximum x and y coordinates.
  100 + final double xMin = xCoords.reduce((a, b) => a < b ? a : b);
  101 + final double xMax = xCoords.reduce((a, b) => a > b ? a : b);
  102 + final double yMin = yCoords.reduce((a, b) => a < b ? a : b);
  103 + final double yMax = yCoords.reduce((a, b) => a > b ? a : b);
  104 +
  105 + return Size(xMax - xMin, yMax - yMin);
  106 + }
87 } 107 }