Flutter-如何获取屏幕小部件的图片?

时间:2020-07-19 09:18:20

标签: image flutter dart screenshot

我正在使用不同的面部图像拍打着一张脸,我想在完成脸部创建后将其导出为jpg。我可以用什么来实现它?

enter image description here

您可以在此处看到一张脸已创建,我只想将脸导出为jpeg。

1 个答案:

答案 0 :(得分:0)

在此article中,将GlobalKey与窗口小部件一起使用,并通过以下代码保存图像:

takeScreenShot() async {
  RenderRepaintBoundary boundary =
      previewContainer.currentContext.findRenderObject();
  double pixelRatio = originalSize / MediaQuery.of(context).size.width;
  ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
  ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData.buffer.asUint8List();
  setState(() {
    _image2 = Image.memory(pngBytes.buffer.asUint8List());
  });
  final directory = (await getApplicationDocumentsDirectory()).path;
  File imgFile = new File('$directory/screenshot.png');
  imgFile.writeAsBytes(pngBytes);
  final snackBar = SnackBar(
    content: Text('Saved to ${directory}'),
    action: SnackBarAction(
      label: 'Ok',
      onPressed: () {
        // Some code
      },
    ),
  );

  Scaffold.of(context).showSnackBar(snackBar);
}