makeBytes method

  1. @override
Iterable<BarcodeElement> makeBytes(
  1. Uint8List data, {
  2. required double width,
  3. required double height,
  4. bool drawText = false,
  5. double? fontHeight,
  6. double? textPadding,
})
override

Generate the barcode graphic description like make but takes a Uint8List data.

Implementation

@override
Iterable<BarcodeElement> makeBytes(
  Uint8List data, {
  required double width,
  required double height,
  bool drawText = false,
  double? fontHeight,
  double? textPadding,
}) sync* {
  assert(width > 0);
  assert(height > 0);

  final matrix = convert(data);

  // Center the barcode
  final mh = matrix.height * matrix.ratio;
  double w;
  double h;
  if (width / height > matrix.width / mh) {
    w = matrix.width * height / mh;
    h = height;
  } else {
    w = width;
    h = mh * width / matrix.width;
  }

  final pixelW = w / matrix.width;
  final pixelH = h / matrix.height;
  final offsetX = (width - w) / 2;
  final offsetY = (height - h) / 2;

  var start = 0;
  bool? color;
  var x = 0;
  var y = 0;

  for (final pixel in matrix.pixels) {
    color ??= pixel;

    if (pixel != color) {
      yield BarcodeBar(
        left: offsetX + start * pixelW,
        top: offsetY + y * pixelH,
        width: (x - start) * pixelW,
        height: pixelH,
        black: color,
      );

      color = pixel;
      start = x;
    }

    x++;
    if (x >= matrix.width) {
      yield BarcodeBar(
        left: offsetX + start * pixelW,
        top: offsetY + y * pixelH,
        width: (matrix.width - start) * pixelW,
        height: pixelH,
        black: color,
      );
      color = null;
      start = 0;
      x = 0;
      y++;
    }
  }
}