本文介绍了Flutter qrImage转换为Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 qr_flutter 创建QrImage.可以,但是我想将QrImage转换为图像,以便创建要在打印机上打印的PDF文件.请帮忙!
I'm using qr_flutter to create QrImage. It's ok but I would like to convert QrImage into image in order to create a PDF file to print on the printer. Please kindly help!
QrImage(
data: qrString,
size: 300.0,
version: 10,
backgroundColor: Colors.white,
),
推荐答案
使用 RepaintBoundary
小部件,带有将小部件导出到b64字符串的键,然后可以将其导出为图像.
Use a RepaintBoundary
widget with a key to export the widget to a a b64 string which then you can export as an image.
示例:
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) {}
}
@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();
})
]));
}
这篇关于Flutter qrImage转换为Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!