scaleCorners method

List<Offset> scaleCorners(
  1. Size targetSize
)

Scale the corners of this Barcode to the given targetSize.

Returns the list of scaled offsets, or an empty list, if the corners is empty.

This method can be used to scale the corners of a Barcode from the original camera coordinate space, into widget coordinate space.

For example, given the BuildContext of a widget:

final BuildContext context;

final Barcode barcode = Barcode(
  size: Size(60, 60),
  corners: [
    Offset(10, 10),
    Offset(50, 10),
    Offset(50, 50),
    Offset(10, 50),
  ],
);

final List<Offset> scaledCorners = barcode.scaleCorners(
  context.size ?? Size.zero
);

Implementation

List<Offset> scaleCorners(Size targetSize) {
  // The size and corners are in the same coordinate space,
  // which is the camera input.
  // If the barcode size is unknown, scale to 0,0.
  final scaleX = size.width > 0 ? targetSize.width / size.width : 0;
  final scaleY = size.height > 0 ? targetSize.height / size.height : 0;

  return [
    for (final Offset offset in corners)
      Offset(offset.dx * scaleX, offset.dy * scaleY),
  ];
}