Android 上的 Monkeyrunner 截图

发布于 2024-11-02 19:39:45 字数 840 浏览 2 评论 0原文

我正在使用以下脚本来截取屏幕截图:

def snap():
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    print "Waiting for device.."
    device = MonkeyRunner.waitForConnection()
    print "Device found.."
    result = device.takeSnapshot()
    print "Clicked.."
    now = datetime.datetime.now()
    file = "C:\\Workspace\\"+now.strftime("%d%m%Y-%H%M%S")+".png"
    result.writeToFile(file,'png')
    print file

我已将其放入 while 循环中,对于一个屏幕截图来说它工作得很好。但在一张屏幕截图之后,输出会像这样无限地挂起:

C:\Program Files\Android\android-sdk\tools>monkeyrunner C:\Workspace\snap.py
Click (y/n)?y
User said: y
Calling function..
Waiting for device..
Device found..
Clicked..
C:\Workspace\19042011-155124.png
Click (y/n)?y
User said: y
Calling function..
Waiting for device..

如何防止代码阻塞并继续截屏?这是设备内存问题吗?

I'm using this following script to take screenshots:

def snap():
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    print "Waiting for device.."
    device = MonkeyRunner.waitForConnection()
    print "Device found.."
    result = device.takeSnapshot()
    print "Clicked.."
    now = datetime.datetime.now()
    file = "C:\\Workspace\\"+now.strftime("%d%m%Y-%H%M%S")+".png"
    result.writeToFile(file,'png')
    print file

I've put this in a while loop and it works fine for one screenshot. But after that one screenshot, the output hangs like this infintely:

C:\Program Files\Android\android-sdk\tools>monkeyrunner C:\Workspace\snap.py
Click (y/n)?y
User said: y
Calling function..
Waiting for device..
Device found..
Clicked..
C:\Workspace\19042011-155124.png
Click (y/n)?y
User said: y
Calling function..
Waiting for device..

How do I keep the code from blocking up and keep taking screenshots? Is this a device-memory issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

污味仙女 2024-11-09 19:39:45

device = MonkeyRunner.waitForConnection() 移出循环并保持与设备的连接打开。

Move device = MonkeyRunner.waitForConnection() out of the loop and keep the connection with your device open.

世界如花海般美丽 2024-11-09 19:39:45

要强制断开连接,您可以终止该进程。不优雅,但无法关闭与monkeyrunner 的连接。

...
pid = int(filter(lambda p: len(p) == 9 and p[8] == 'com.android.commands.monkey', map(lambda l: l.split(), device.shell('ps').splitlines()))[0][1])
print "killing %s" % pid
device.shell("kill %d" % pid)
device = None
...

To force disconnection you may kill the process. Not elegant but there's no way of closing the connection from monkeyrunner.

...
pid = int(filter(lambda p: len(p) == 9 and p[8] == 'com.android.commands.monkey', map(lambda l: l.split(), device.shell('ps').splitlines()))[0][1])
print "killing %s" % pid
device.shell("kill %d" % pid)
device = None
...
画中仙 2024-11-09 19:39:45

我宁愿在 Monkeyrunner 脚本中使用 shell 中的 adb pull 命令,如下所示

 
os.system('adb pull /dev/graphics/fb0 image')
subprocess.call('ffmpeg -vframes 1 -vcodec rawvideo -loglevel quiet -f  rawvideo -pix_fmt rgba -s 480x854 -i image -f image2 -vcodec png image.png')

注意: 它使用 ffmpeg 将原始数据转换为 png 文件。 FFMPEG 应该位于路径
这样做是因为 takesnapshot() API 经常运行异常。
480x854是设备分辨率,根据您的设备更改。
通过这种方法,我可以拍摄一系列快照。

如果您非常执意于 device.takesnapshot() ,请尝试在两者之间添加 time.sleep() ,这可能会有所帮助。

I would rather use adb pull command from shell in monkeyrunner script like this

 
os.system('adb pull /dev/graphics/fb0 image')
subprocess.call('ffmpeg -vframes 1 -vcodec rawvideo -loglevel quiet -f  rawvideo -pix_fmt rgba -s 480x854 -i image -f image2 -vcodec png image.png')

Note: It uses ffmpeg to convert raw data to png file. FFMPEG should be in path
This was done because often takesnapshot() API runs exception.
480x854 is the device resolution, change it according to your device.
By this method I am able to take series of snapshots.

If you are hell bent on device.takesnapshot() try adding time.sleep() in between, it may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文