toPng static method

Future<Uint8List?> toPng({
  1. required String value,
  2. UBarcodeType type = UBarcodeType.qrCode,
  3. double width = 512,
  4. double height = 512,
  5. double pixelRatio = 1,
  6. Color barColor = const Color(0xFF000000),
  7. Color background = const Color(0xFFFFFFFF),
  8. List<Color>? gradientColors,
  9. AlignmentGeometry gradientBegin = Alignment.centerLeft,
  10. AlignmentGeometry gradientEnd = Alignment.centerRight,
  11. UBarcodeModuleShape moduleShape = UBarcodeModuleShape.square,
  12. double cornerRadiusRatio = 0.3,
  13. bool showValue = false,
  14. double textSpacing = 8,
  15. double quietZone = 0,
  16. UErrorCorrectionLevel errorCorrectionLevel = UErrorCorrectionLevel.low,
  17. int? qrCodeVersion,
})

Implementation

static Future<Uint8List?> toPng({
  required String value,
  UBarcodeType type = UBarcodeType.qrCode,
  double width = 512,
  double height = 512,
  double pixelRatio = 1,
  Color barColor = const Color(0xFF000000),
  Color background = const Color(0xFFFFFFFF),
  List<Color>? gradientColors,
  AlignmentGeometry gradientBegin = Alignment.centerLeft,
  AlignmentGeometry gradientEnd = Alignment.centerRight,
  UBarcodeModuleShape moduleShape = UBarcodeModuleShape.square,
  double cornerRadiusRatio = 0.3,
  bool showValue = false,
  double textSpacing = 8,
  double quietZone = 0,
  UErrorCorrectionLevel errorCorrectionLevel = UErrorCorrectionLevel.low,
  int? qrCodeVersion,
}) async {
  final Barcode barcode = _barcodeFor(type, qrVersion: qrCodeVersion, ecc: errorCorrectionLevel);
  final _UBarcodePainter painter = _UBarcodePainter(
    barcode: barcode,
    data: value,
    drawText: showValue,
    foreground: barColor,
    background: background,
    gradient: gradientColors != null && gradientColors.length >= 2 ? LinearGradient(colors: gradientColors, begin: gradientBegin, end: gradientEnd) : null,
    moduleShape: moduleShape,
    cornerRadiusRatio: cornerRadiusRatio,
    quietZone: quietZone,
    textStyle: TextStyle(color: barColor, fontSize: 12),
    textPadding: textSpacing,
    is2D: barcode is Barcode2D,
    logo: null,
    logoRatio: 0,
  );
  final ui.PictureRecorder recorder = ui.PictureRecorder();
  final Canvas canvas = Canvas(recorder);
  canvas.scale(pixelRatio);
  painter.paint(canvas, Size(width, height));
  final ui.Image image = await recorder.endRecording().toImage((width * pixelRatio).round(), (height * pixelRatio).round());
  final ByteData? data = await image.toByteData(format: ui.ImageByteFormat.png);
  image.dispose();
  return data?.buffer.asUint8List();
}