我需要从webview_flutter截取WebView的屏幕快照,但这不起作用。
我有一个应用,该应用必须对WebView进行截图,然后对其进行处理。我试图用Screenshot包来做。我发现此信息https://github.com/fluttercommunity/flutter_webview_plugin/issues/181#issuecomment-497625384
从链接中,我了解到无法通过Screenshot插件来做到这一点。
Screenshot(
controller: _screenshotController,
child: WebView(
initialUrl: widget._webUrl,
onWebViewCreated:
(WebViewController webViewController) {
if (_controller.isCompleted == false)
_controller.complete(webViewController);
},
),
);
void takeScreenshot() {
_screenshotController.capture().then(
(File image) async {
_screenshot = image;
}
);
当我截取屏幕截图时,我得到了透明的png图像,而我想捕获WebView内容
答案 0 :(得分:0)
正如我向类似的issue报告的那样:
您可以使用我的插件flutter_inappwebview,它是Flutter插件,允许您添加内联WebView或打开应用程序内浏览器窗口,并且具有许多事件,方法和选项来控制WebView。 / p>
要获取屏幕截图,可以使用InAppWebViewController.takeScreenshot
方法,该方法获取WebView可见视口的屏幕截图(PNG格式)并返回Uint8List
。
下面是一个示例,该示例在页面停止加载时获取WebView的屏幕快照,并显示带有相应屏幕快照图像的警报对话框:
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(initialRoute: '/', routes: {
'/': (context) => InAppWebViewExampleScreen(),
});
}
}
class InAppWebViewExampleScreen extends StatefulWidget {
@override
_InAppWebViewExampleScreenState createState() =>
new _InAppWebViewExampleScreenState();
}
class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
InAppWebViewController webView;
Uint8List screenshotBytes;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("InAppWebView")),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: "https://github.com/flutter",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) async {
screenshotBytes = await controller.takeScreenshot();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Image.memory(screenshotBytes),
);
},
);
},
))
])));
}
}