android - 无法使用原生代码在 V2 Flutter 中截屏,它在 V1 Flutter 中运行良好

标签 android ios flutter sdk screenshot

我们正在构建一个 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,我们也没有找到任何解决方案来捕获它。

我们尝试了以下方法,但没有成功。有人可以帮我吗?

  • 试图从我们在 SDK 中传递的上下文中获取 Activity
    初始化,我们从 Activity 中获取 View 并尝试绘制它,
    正在捕获的空白屏幕截图
  • 尝试将 SDK Flutter 插件迁移到 V2 使用
    https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration
  • 试图遍历 View 层次结构并使用 Activity 绘制 View
    上下文,正在捕获空白屏幕截图
  • 试图绘制从
    FlutterActivity.getWindow().getDecorView().getRootView(),空白
    正在捕获的屏幕截图。
  • 最佳答案

    你可以看看Screenshoot package其代码 GitHub正在做。该软件包是 Flutter v2 的最新版本。
    该软件包使用:

  • 获取 render object归功于 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/

    相关文章:

    google-maps - 如何在 Flutter 中的 Google map 上制作 float 搜索栏?

    flutter - 有什么方法可以在应用程序中打开 URL 而不是在浏览器中打开 URL?

    firebase - 如何在 Flutter/Firestore 中执行先进先出系统?

    android - 如何用 RecyclerView 实现无限列表?

    android - 整个应用程序的 float View

    android - Android导航组件Deeplink Backstack

    安卓 : AlarmManager triggers anytime

    ios - AvAudioRecorder 录音文件随机损坏

    ios - 在选择另一个UITEXTVIEW时,如何阻止UitextView辞职急救/关闭键盘?

    ios - iOS:混合Core Graphics绘制路径和UIKit绘制文本功能