generateImagePngBytes static method

Uint8List generateImagePngBytes({
  1. required String text,
})

Implementation

static Uint8List generateImagePngBytes({
  required String text,
}) {
  final qrcode = Encoder.encode(text, ErrorCorrectionLevel.h);
  final matrix = qrcode.matrix!;
  final scale = 10;
  final width = matrix.width * scale;
  final height = matrix.height * scale;

  final img.Image image = img.Image(
    width: width + 200,
    height: height + 200,
  );
  img.fill(image, color: img.ColorRgba8(255, 255, 255, 0xFF));

  final xs = [];
  for (var x = 0; x < matrix.width; x++) {
    for (var y = 0; y < matrix.height; y++) {
      if (matrix.get(x, y) == 1) {
        xs.add([x + 10, y + 10]);
      }
    }
  }

  for (var i = 0; i < xs.length; i++) {
    var x = xs[i][0];
    var y = xs[i][1];

    img.fillRect(
      image,
      x1: x * scale,
      y1: y * scale,
      x2: x * scale + scale,
      y2: y * scale + scale,
      color: img.ColorRgba8(0, 0, 0, 0xFF),
    );
  }
  return img.encodePng(image);
}