Java 使用 ADB 截屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27766712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:58:57  来源:igfitidea点击:

Using ADB to capture the screen

javaandroidadbscreenshot

提问by user2513924

I'm trying to get a screenshot of the phone screen as fast as possible. Currently, I am doing:

我正在尝试尽快获取电话屏幕的屏幕截图。目前,我正在做:

adb shell screencap -p /sdcard/screencap.png && adb pull /sdcard/screencap.png         

However it is too slow and takes up to 3 seconds. Is there any better way to do this? I intend to use this function with an unrooted phone.

但是它太慢了,最多需要 3 秒。有没有更好的方法来做到这一点?我打算在无根电话上使用此功能。

Also what are the different arguments I can use for screencap?

另外,我可以用于 screencap 的不同参数是什么?

Thanks.

谢谢。

EDIT (extra information): I intend to use this method to be able to get a live feed of the screen onto my pc. The current method works however it is too slow. I can't use adb shell screenrecordbecause I won't be able to access the video file while it is being recorded.

编辑(额外信息):我打算使用这种方法来将屏幕的实时信息传送到我的电脑上。目前的方法有效,但它太慢了。我无法使用,adb shell screenrecord因为在录制视频文件时我将无法访问它。

回答by alijandro

Sorry to tell you screencapjust a simple command, only accept few arguments, but none of them can save time for you, here is the -hhelp output.

很抱歉告诉你screencap一个简单的命令,只接受很少的参数,但它们都不能为你节省时间,这里是-h帮助输出。

$ adb shell screencap -h
usage: screencap [-hp] [-d display-id] [FILENAME]
-h: this message
-p: save the file as a png.
-d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

Besides the command screencap, there is another command screenshot, I don't know why screenshotwas removed from Android 5.0, but it's avaiable below Android 4.4, you can check the source from here. I didn't make my comparison which is faster between these two commands, but you can give your try in your real environment and make the final decision.

除了命令之外screencap,还有另一个命令screenshot,我不知道为什么screenshot从 中删除Android 5.0,但它在下面可用Android 4.4,您可以从这里查看源。我没有比较这两个命令之间哪个更快,但是您可以在实际环境中尝试并做出最终决定。

回答by Prem

To start recording your device's screen, run the following command:

要开始录制设备的屏幕,请运行以下命令:

adb shell screenrecord /sdcard/example.mp4

This command will start recording your device's screen using the default settings and save the resulting video to a file at /sdcard/example.mp4 file on your device.

此命令将开始使用默认设置录制设备屏幕,并将生成的视频保存到设备上的 /sdcard/example.mp4 文件中。

When you're done recording, press Ctrl+C in the Command Prompt window to stop the screen recording. You can then find the screen recording file at the location you specified. Note that the screen recording is saved to your device's internal storage, not to your computer.

完成录制后,在命令提示符窗口中按 Ctrl+C 停止屏幕录制。然后您可以在您指定的位置找到屏幕录制文件。请注意,屏幕录制将保存到您设备的内部存储中,而不是您的计算机中。

The default settings are to use your device's standard screen resolution, encode the video at a bitrate of 4Mbps, and set the maximum screen recording time to 180 seconds. For more information about the command-line options you can use, run the following command:

默认设置是使用您设备的标准屏幕分辨率,以 4Mbps 的比特率对视频进行编码,并将最大屏幕录制时间设置为 180 秒。有关您可以使用的命令行选项的更多信息,请运行以下命令:

adb shell screenrecord --help

This works without rooting the device. Hope this helps.

这无需植根设备即可工作。希望这可以帮助。

回答by Jared Rummler

You can read the binary from stdout instead of saving the png to the sdcard and then pulling it:

您可以从 stdout 读取二进制文件,而不是将 png 保存到 SD 卡,然后将其拉出:

adb shell screencap -p | sed 's|\r$||' > screenshot.png

This should save a little time, but not much.

这应该会节省一点时间,但不会太多。

source: Read binary stdout data from adb shell?

来源:从 adb shell 读取二进制标准输出数据?

回答by Diego Plentz

To make it work @ OSX and Linux

让它在 OSX 和 Linux 上工作

adb exec-out screencap -p > screen.png

回答by I'm a frog dragon

https://stackoverflow.com/a/37191719/75579answer stopped working for me in Android 7 somehow. So I have to do it the manual way, so I want to share it.

https://stackoverflow.com/a/37191719/75579答案在 Android 7 中以某种方式停止为我工作。所以我必须以手动方式来做,所以我想分享它。



How to install

如何安装

  1. Put this snippet of code in your ~/.bash_profileor ~/.profilefile:

    snap_screen() {
      if [ $# -eq 0 ]
      then
        name="screenshot.png"
      else
        name=".png"
      fi
      adb shell screencap -p /sdcard/$name
      adb pull /sdcard/$name
      adb shell rm /sdcard/$name
      curr_dir=pwd
      echo "save to `pwd`/$name"
    }
    
  2. Run source ~/.bash_profileor source ~/.profilecommand,

  1. 将此代码片段放在您的~/.bash_profile~/.profile文件中:

    snap_screen() {
      if [ $# -eq 0 ]
      then
        name="screenshot.png"
      else
        name=".png"
      fi
      adb shell screencap -p /sdcard/$name
      adb pull /sdcard/$name
      adb shell rm /sdcard/$name
      curr_dir=pwd
      echo "save to `pwd`/$name"
    }
    
  2. 运行source ~/.bash_profilesource ~/.profile命令,



How to use

如何使用

Usage without specifying filename:

不指定文件名的用法:

$ snap_screen
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/screenshot.png

Usage with a filename:

使用文件名:

$ snap_screen mega_screen_capture
11272 KB/s (256237 bytes in 0.022s)
Saved to /Users/worker8/desktop/mega_screen_capture.png

Hope it helps!

希望能帮助到你!

** This will not work if multiple devices are plugged in

** 如果插入多个设备,这将不起作用

回答by baitisj

Using some of the knowledge from this and a couple of other posts, I found the method that worked the best for me was to:

使用本文和其他几篇文章中的一些知识,我发现最适合我的方法是:

adb shell 'stty raw; screencap -p'

adb shell 'stty raw; screencap -p'

I have posted a very simple Python script on GitHub that essentially mirrors the screen of a device connected over ADB:

我在 GitHub 上发布了一个非常简单的 Python 脚本,它基本上反映了通过 ADB 连接的设备的屏幕:

https://github.com/baitisj/android_screen_mirror

https://github.com/baitisj/android_screen_mirror