简体   繁体   English

飞镖:捕获Flutter Webview的图像

[英]Dart : capture an image of a Flutter webview

I wonder if it's possible to capture an image of a webview? 我想知道是否可以捕获Webview的图像?

Here is what I tried : 这是我尝试的:

eg with webview_flutter 0.1.0+1 : 例如,使用webview_flutter 0.1.0 + 1

   static GlobalKey previewContainer = new GlobalKey();
   ...            
                new RaisedButton(
                  onPressed: takeScreenShot,
                  child: const Text('Take a Screenshot'),
                ),
  ...
   takeScreenShot() async{
        RenderRepaintBoundary boundary = previewContainer.currentContext.findRenderObject();
        ui.Image image = await boundary.toImage();
        final directory = (await getApplicationDocumentsDirectory()).path;
        ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
        Uint8List pngBytes = byteData.buffer.asUint8List();
        File imgFile = new File(directory+'/screenshot.png');
        imgFile.writeAsBytes(pngBytes);

        //then, save the png image file in Firebase storage : OKAY
  }

First I tried to make a printscreen of a simple material button: OKAY, it works 首先,我尝试制作一个简单的材质按钮的打印屏幕: 好的,它可以工作

    RepaintBoundary(
            key: previewContainer,
            child: 
                MaterialButton(
                     child: const Text('Test'),
                     textColor: Colors.white,
                    color: Colors.blue,

                ),
            ),

But when I try to make a printscreen of a webview, it doesn't work, the image saved is empty (just a few bytes) : 但是,当我尝试制作Webview的打印屏幕时,它不起作用,保存的图像为空(仅几个字节):

     RepaintBoundary(
            key: previewContainer,
            child: 
              new SizedBox(
                    height: 280.0,
                    width: 280.0,
                    child: WebView(
                      initialUrl: 'https://flutter.io',
                      javaScriptMode: JavaScriptMode.unrestricted,
                      onWebViewCreated: (WebViewController webViewController)                    {
                        _controller.complete(webViewController);
                      },
                    )
               ),
             )

Any idea? 任何想法?

or is there another way than RepaintBoundary to capture a png image? 还是除了RepaintBoundary以外还有其他方法来捕获png图像?

It happen to me that I had the same problem. 我碰巧遇到了同样的问题。

The short answer is that RepaintBoundary cannot capture WebView's content because it was displayed by native elements under the hood (ie WKWebView on iOS and WebView on Android) 简短的答案是RepaintBoundary 无法捕获WebView的内容,因为它是由引擎盖下的本机元素(即iOS上的WKWebView和Android上的WebView )显示的

So, the only solution I've found was to capture content directly from native element. 因此,我发现的唯一解决方案是直接从本机元素捕获内容。 Unfortunately, that's not a trivial task to do with webview_flutter 0.1.0+1 , but there is another package that do the thing - flutter_inappbrowser https://github.com/pichillilorenzo/flutter_inappbrowser 不幸的是,对于webview_flutter 0.1.0+1来说,这并不是一件容易的事,但是还有另一个软件包可以完成此flutter_inappbrowser https://github.com/pichillilorenzo/flutter_inappbrowser

Once you connected this library you can get the image this way: 连接此库后,您可以通过以下方式获取图像:

_controller.takeScreenshot().then((bytes) async {
  var len = bytes.lengthInBytes;
  debugPrint("screenshot taken bytes $len");

  final directory = (await getApplicationDocumentsDirectory()).path;
  String fileName = DateTime.now().toIso8601String();
  var path = '$directory/$fileName.png';

  File imgFile = new File(path);
  await imgFile.writeAsBytes(bytes).then((onValue) {
    ShareExtend.share(imgFile.path, "Screenshot taken");
  });
});

PS It works on iOS 11+ and on Android it uses deprecated method, so might be not supported in a time. PS它可以在iOS 11+和Android上使用不推荐使用的方法,因此可能暂时不支持。

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

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