這個功能在網上看了好多人的代碼,但是總是在image.writeToFile('D:/tmp/images/black.png','png')這一句出錯.查了google的API也感覺沒錯呀。
后來自己動腦,改成了image.writeToFile('D:/tmp/images/black.png'),竟然就成功了。后面的圖片后綴竟然是可以不用加的。而且
然后loadimage后面的后綴同樣可選。我是這樣寫的 result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')
code:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
import sys,time
device = MonkeyRunner.waitForConnection()
device.wake()
device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
MonkeyRunner.sleep(3.0)
image = device.takeSnapshot()
MonkeyRunner.sleep(1.0)
if (image != None):
image.writeToFile('D:/tmp/images/black.png')
cout = 1
while(cout < 1000):
device.startActivity(action = "android.media.action.IMAGE_CAPTURE")
MonkeyRunner.sleep(3.0)
image = device.takeSnapshot()
MonkeyRunner.sleep(1.0)
result = MonkeyRunner.loadImageFromFile('D:/tmp/images/black.png','png')
MonkeyRunner.sleep(3.0)
if(image.sameAs(result,0.8)):
print "success!"
else:
print "fail!"
break;
cout += 1
這個腳本是用來測試系統調用攝像頭是否成功的,但是由於攝像頭取到的圖片是一張照片,sameas的算法可能針對於手機屏幕的顯示而對攝像頭取到的圖算不來,所以實際測試的時候失敗了。
