如何将我生成的二维码图像保存到图库中?使用Flutter



我正在尝试使用RenderRepaintBoundary生成图像,并希望将图像保存在本地目录中
我可以保存图像,但我得到的是黑色图像而不是QR图像。

将图像写入目录的代码块:

try {
RenderRepaintBoundary boundary =  
globalKey.currentContext.findRenderObject();  
var image = await boundary.toImage();

ByteData byteData = await image.toByteData(format: ImageByteFormat.png);

Uint8List pngBytes = byteData.buffer.asUint8List();

final file =
await new File('/<localpath>/image.png').create();

await file.writeAsBytes(pngBytes);
} catch (e) {
print(e);    
}

生成二维码的代码块:

RepaintBoundary( key: globalKey,child: QrImage(data: _dataString,size: 0.3 * bodyHeight,), );

这是由于背景透明造成的;简单的解决方案是用白色背景的容器包装你的QrImage:

Container(
color: Colors.white,
child: QrImage(....
)

最新更新