如何在Flutter中对屏幕上的特定区域进行截图?

3

enter image description here

我想提取屏幕上特定区域的图像,我有x、y、高度和宽度。有人知道如何实现吗?


1
也许你可以使用 RepaintBoundary 进行截图,然后裁剪 PNG? - WSBT
2个回答

1

我不太清楚 X 和 Y,但您可以使用 screenshot 包中的特定小部件屏幕截图进行操作。 链接

这个包可以捕获小部件并将它们作为图像保存,即使它们没有在屏幕上呈现。

步骤 1:创建一个控制器

ScreenshotController screenshotController = ScreenshotController();

第二步:使用Screenshot()包装小部件。
Screenshot(
    controller: screenshotController,
    child: Text("This text will be captured as image"),
),

步骤三:通过调用capture方法来截取屏幕截图。这将返回一个Uint8List。

screenshotController.capture().then((Uint8List image) {
    //Capture Done
    setState(() {
        _imageFile = image;
    });
}).catchError((onError) {
    print(onError);
});

0
通过使用RepaintBoundary小部件,您可以截取您想要的特定区域的屏幕截图。我在这里提供了一个我在项目中执行的示例,请参考。希望对您有所帮助。
首先声明globalkey...
 GlobalKey globalKey = GlobalKey();

然后使用这个globalKey,将您的小部件包装在RepaintBoundary中,如下所示。

 RepaintBoundary(
            key: globalKey,
            child: Obx(() {
              return Container(
                decoration: BoxDecoration(image: DecorationImage(image: NetworkImage(mainController.imageName.value))),
                height: 390,
                width: 390,
                child: Stack(
                  children: generatedNameWidgets,
                ),
              );
            })),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接