toPng static method
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,
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();
}