在本机 Java 中截屏并在 Flutter 中显示此截屏

3

想要使用本地Java截取移动屏幕的截图。将此截图位图存储在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;
  }

Dart平台通道:

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

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

Flutter用户界面以显示屏幕截图:

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

print(screenshot) 显示一个字节数组:

[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,

从原生通过通道传递给Flutter的完整Byte[]数组未能成功传递。但是它显示为黑色的图像容器enter image description here


print(screenShot) 打印了什么内容? - undefined
@RichardHeap,请查看问题详细信息,我添加了print(screenshot)显示的内容。 - undefined
这是一个PNG头部,看起来很有希望。第三个块似乎有8192字节长。你能打印出字节数组两端的长度来确认没有截断吗?(请注意,直接打印字节数组本身只会打印数组的开头,因为日志会对其进行截断。打印长度将显示数组未被截断。)你能将字节数组保存到文件中,或者上传到HTTP服务器上,以便在笔记本电脑上确认它是否正常显示吗? - undefined
byte[] 数组的长度为 6137。 - undefined
考虑到第三个PNG块似乎有8k,这看起来很短。 - undefined
将byte[]数组保存到文件中,结果显示为黑屏。我该如何获取原始的屏幕截图? - undefined
1个回答

0
一个简单的方法是将屏幕截图保存为磁盘上的文件,然后只需将文件路径传递给Flutter即可实现此目的。 然后,您可以使用Image.file加载它。

请检查我更新的本地代码saveBitmap()函数,你建议遵循这个流程吗? - undefined
@ahmedminhaj 你可以参考https://dev59.com/3nE85IYBdhLWcg3wr1ge#5651242的方法来进行截图并保存到磁盘中。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接