简体   繁体   English

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

[英]Flutter- how to get pictures of screen widgets?

I am making a face in flutter using different facial images and i want to export it as jpg when the face creation is done.我正在使用不同的面部图像在 flutter 中制作一张脸,我想在完成面部创建后将其导出为 jpg。 what could i use to achieve it?我可以用什么来实现它?

在此处输入图像描述

You can see here a face is created and i want to export only face as a jpeg.您可以在这里看到创建了一个人脸,我只想将人脸导出为 jpeg。

In this article , use GlobalKey with your widget and save image by following code:本文中,将 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM