【问题标题】:Take screenshot in native java and display this screenshot in flutter在原生 java 中截取屏幕截图并在颤动中显示此屏幕截图
【发布时间】:2020-06-23 13:14:25
【问题描述】:

想用原生 Java 截取手机屏幕截图。将此截图Bitmap存储在一个byte[]数组中,通过平台通道将这个byte[]数组传入flutter,最后想在flutter应用中显示这个截图。

本机 Java 代码:

  public Bitmap takeScreenshot(){
        View rootView = findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
  }

  public byte[] bitmapToByte(){
      Bitmap bitmap = takeScreenshot();

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] array = stream.toByteArray();
      Log.i("MyAndroidClass", Arrays.toString(array));

      return array;
  }

  public File saveBitmap(Bitmap bitmap){
      Bitmap bitmap = takeScreenshot();
      File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshoot.png");
      FileOutputStream fos;
      try{
          fos = new FileOutputStream(imagePath);
          bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
          fos.flush();
          fos.close();
      } catch (FileNotFoundException e) {
          Log.e("GREC", e.getMessage(), e);
      } catch (IOException e) {
          Log.e("GREC", e.getMessage(), e);
      }
      return imagePath;
  }

飞镖平台频道:

Future<Null> _takeScreenShot() async {
    Uint8List screenShot;

    Uint8List ssView = await platform2.invokeMethod("takeScreenshot");
    screenShot = ssView;
    print(screenShot);
    setState(() {
      byteView = screenShot;
    });
  }

Flutter UI 显示截图:

Container(
  child: byteView != null
     ? Image.memory(
        byteView,
        width: 100,
        height: 150,
        )
     : Container(),
 ),

print(screenshot) 显示一个 byte[] 数组:

[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 4, 56, 0, 0, 7, 128, 8, 2, 0, 0, 0, 164, 3, 112, 93, 0, 0, 0, 3, 115, 66, 73, 84, 8, 8, 8, 219, 225, 79, 224, 0, 0, 32, 0, 73, 68, 65, 84, 120, 156, 236, 221, 187, 138, 20, 81, 20, 64, 209, 115, 155, 198, 119, 160, 129, 162, 160, 32, 38, 130, 255, 228, 159, 248, 165, 134, 26, 136, 10, 130, 207, 81, 25, 186, 12, 156, 25, 17, 140, 237, 13, 179, 86, 80, 20, 197, 13, 78, 186, 57, 69, 213, 122, 254, 98, 46, 172, 53, 107, 247, 251, 110, 0, 0, 0, 254, 135, 109, 102, 102, 14, 115, 216, 254, 60, 219, 255, 227, 208, 58, 63, 10, 0, 0, 112, 12, 103, 161, 178, 109, 51, 219, 28, 182, 57, 28, 102, 89, 167, 0, 0, 0, 255, 215, 90, 179, 214, 204, 58, 235, 145, 243, 141, 202, 54, 223, 79, 230, 219, 167, 249, 246, 249, 252, 220, 113, 198, 3, 0, 0, 46, 163, 171, 55, 230, 230, 237, 185, 118, 99, 214, 110, 102, 155, 253, 110, 205, 54, 115, 216, 230, 235, 199, 121, 251, 106, 222, 191, 58, 246, 128, 0, 0, 192, 229, 115, 231, 254, 60, 120, 50, 87, 174, 206, 126, 63,

完整的 Byte[] 数组没有通过通道从本地传递到颤动。 但它显示一个黑色的图像容器

【问题讨论】:

  • print(screenShot) 打印了什么?
  • @RichardHeap,请查看问题详情,我添加了print(screenshot) 显示的内容。
  • 这是一个 PNG 标头,很有前途。看起来第 3 个块的长度为 8192 字节。您能否在每一端打印字节数组的长度以确认没有截断。 (请注意,打印字节数组本身只会打印数组的开头,因为它被日志截断。打印长度将显示数组没有被截断。)你可以将字节数组保存到文件中,还是上传到一个 http 服务器,以便您可以确认它在笔记本电脑上显示正常?
  • byte[] 数组的长度为 6137
  • 考虑到第三个 PNG 块似乎是 8k,这似乎很短。

标签: java android flutter dart java-native-interface


【解决方案1】:

实现此目的的一种简单方法是将屏幕截图保存为磁盘上的文件,然后将文件路径简单地传递给 Flutter。 然后你使用Image.file加载它

【讨论】:

  • 请检查我更新的原生代码saveBitmap()函数,你建议按照这个过程吗?
  • @ahmedminhaj 您可以查看stackoverflow.com/a/5651242/1630255 获取屏幕截图并将其保存到磁盘。
猜你喜欢
  • 2019-12-29
  • 1970-01-01
  • 2019-05-07
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
相关资源
最近更新 更多