Flutter qrImage 转换为 Image

GPH*_*GPH 2 qr-code image dart flutter

我正在使用qr_flutter创建 QrImage。没关系,但我想将 QrImage 转换为图像,以便创建一个 PDF 文件以在打印机上打印。请帮助!

QrImage(
  data: qrString,
  size: 300.0,
  version: 10,
  backgroundColor: Colors.white,
),
Run Code Online (Sandbox Code Playgroud)

Zro*_*roq 9

使用RepaintBoundary带键的小部件将小部件导出为 b64 字符串,然后您可以将其导出为图像。

例子:

Future<Uint8List> _getWidgetImage() async {
 try {
   RenderRepaintBoundary boundary =
      _renderObjectKey.currentContext.findRenderObject();
   ui.Image image = await boundary.toImage(pixelRatio: 3.0);
   ByteData byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);
   var pngBytes = byteData.buffer.asUint8List();
   var bs64 = base64Encode(pngBytes);
   debugPrint(bs64.length.toString());
   return pngBytes;
 } catch (exception) {}
Run Code Online (Sandbox Code Playgroud)

}

@override
Widget build(BuildContext context) {
    return Scaffold(
        body: Column(children: [
          RepaintBoundary(
            key: _renderObjectKey,
            child: QrImage(
            data: "some text",
            size: 300.0,
            version: 10,
            backgroundColor: Colors.white,
         ),
       ),
       RaisedButton(onPressed: () {
         _getWidgetImage();
       })
     ]));
Run Code Online (Sandbox Code Playgroud)

}


use*_*484 5

Future<Uint8List> toQrImageData(String text) async {
  try {
    final image = await QrPainter(
      data: text,
      version: QrVersions.auto,
      gapless: false,
      color: hexToColor('#000000'),
      emptyColor: hexToColor('#ffffff'),
    ).toImage(300);
    final a = await image.toByteData(format: ImageByteFormat.png);
    return a.buffer.asUint8List();
  } catch (e) {
    throw e;
  }
}
Run Code Online (Sandbox Code Playgroud)