我们正在构建一个 SDK,它会在应用集成后对其进行截图。我们能够为 Native Android、Native iOS、Cordova、IONIC、React Native、Xamarin 平台截屏。但是无法在 V2 Flutter 中拍摄快照。代码在 V1 Flutter 之前运行良好。但是,当在 V2 Flutter 上执行相同的 native 代码时,会返回应用程序的黑色屏幕截图。
这里的问题是,V1 的 FlutterView 使用名为 registrar.view() 的方法对第三方插件公开可用,在 V2 中,此 View 现在不会暴露给任何插件!
https://api.flutter.dev/javadoc/io/flutter/plugin/common/PluginRegistry.Registrar.html使用 V1 的 FlutterView,我们能够使用 PixelCopy 捕获 View 的屏幕截图,但我们无法获得 V2 的 FlutterView,我们也没有找到任何解决方案来捕获它。
我们尝试了以下方法,但没有成功。有人可以帮我吗?
初始化,我们从 Activity 中获取 View 并尝试绘制它,
正在捕获的空白屏幕截图
https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration
上下文,正在捕获空白屏幕截图
FlutterActivity.getWindow().getDecorView().getRootView(),空白
正在捕获的屏幕截图。
最佳答案
你可以看看Screenshoot package其代码 GitHub正在做。该软件包是 Flutter v2 的最新版本。
该软件包使用:
GlobalKey
的当前小部件. GlobalKey _containerKey = GlobalKey().currentContext?.findRenderObject();
RenderRepaintBoundary
. RenderRepaintBoundary boundary = findRenderObject as RenderRepaintBoundary;
pixelRatio = pixelRatio ?? MediaQuery.of(context).devicePixelRatio;
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
请参阅下面的整个代码:
Future<ui.Image?> captureAsUiImage(
{double? pixelRatio: 1,
Duration delay: const Duration(milliseconds: 20)}) {
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return new Future.delayed(delay, () async {
try {
var findRenderObject =
this._containerKey.currentContext?.findRenderObject();
if (findRenderObject == null) {
return null;
}
RenderRepaintBoundary boundary =
findRenderObject as RenderRepaintBoundary;
BuildContext? context = _containerKey.currentContext;
if (pixelRatio == null) {
if (context != null)
pixelRatio = pixelRatio ?? MediaQuery.of(context).devicePixelRatio;
}
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
return image;
} catch (Exception) {
throw (Exception);
}
});
}
关于android - 无法使用原生代码在 V2 Flutter 中截屏,它在 V1 Flutter 中运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62021821/