如何在 Flutter 中导出屏幕的高分辨率图像?

时间:2021-02-16 19:57:52

标签: image flutter export

如何在 Flutter 中以高分辨率将屏幕导出为图像或 PDF?是否可以使用用户输入作为图像导出带有背景图像和文本字段的容器?我试过这个解决方案 ("react-native paper / radio-button-group" lib),但分辨率太差了。我需要不同尺寸的屏幕,例如40x60cm、20x30cm 和 300Ddpi。

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用 pixelRatio

ui.Image image = await boundary.toImage(pixelRatio: 10);

答案 1 :(得分:0)

尝试了 pixelRatio 并将其与更大的响应结合起来。没有 cacheWidth - 它确实有效,至少对于 PNG 导出而言是这样。

但是请注意,pixelRatio 为 10 需要相当长的时间来捕获...<​​/p>

图像代码片段:

child: ClipRRect(
  borderRadius: BorderRadius.circular(15),
  child: Image.file(
    File(widget.myGroup.picture),
    width: MediaQuery.of(context).size.width,
    // NO cacheWidth:
    // cacheWidth: MediaQuery.of(context).size.width,
    fit: BoxFit.fitWidth,
  ),
),

_capturePng() 的代码片段:

Future<Uint8List> _capturePng() async {
  RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();

  if (boundary.debugNeedsPaint) {
    print("Waiting for boundary to be painted.");
    await Future.delayed(const Duration(milliseconds: 20));
    return _capturePng();
  }

  var image = await boundary.toImage(pixelRatio: 10);
  var byteData = await image.toByteData(format: ImageByteFormat.png);
  return byteData.buffer.asUint8List();
}

(指this question here about screenshot with flutter on Stack Overflow