Android上monkeyrunner的屏幕截图
我使用以下脚本截图:Android上monkeyrunner的屏幕截图,android,screenshot,monkeyrunner,Android,Screenshot,Monkeyrunner,我使用以下脚本截图: def snap(): from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice print "Waiting for device.." device = MonkeyRunner.waitForConnection() print "Device found.." result = device.takeSnapshot() print "Clicked
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
我把它放在一个循环中,一个屏幕截图就可以了。但在那一个屏幕截图之后,输出完全如下所示:
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..
如何防止代码阻塞并继续截图?这是设备内存问题吗 将
device=MonkeyRunner.waitForConnection()
移出循环,并保持与设备的连接处于打开状态。若要强制断开连接,可能会终止进程。不优雅,但无法关闭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
...
我宁愿在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') 操作系统('adb pull/dev/graphics/fb0 image') 子进程调用('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是设备分辨率,请根据您的设备进行更改。
通过这种方法,我可以拍摄一系列快照
如果您非常喜欢设备.takesnapshot(),请尝试在两者之间添加time.sleep(),这可能会有所帮助。dtmilano-有没有办法在连接成功后断开连接?