Android自动化工具Monkeyrunner使用(三)

     本章开始介绍下MonkeyrunnerAPI, 以便了解其具体的使用。

MonkeyImage  takeSnapshot()截取当前设备的屏幕, 并获得当前图片, 返回一个monkryimage对象。

Captures the entire screen buffer of this device, yielding a screen capture of the current display.

Returns
  • A MonkeyImage object containing the image of the current display.

E.g.  截取当前界面,返回一个对象

short_begin = device.takeSnapshot()

这个对象有5中方法可以调用:

Methods
string convertToBytes ( string format)
Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes.
tuple getRawPixel ( integer x, integer y)
Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b).
integer getRawPixelInt ( integer x, integer y)
Returns the single pixel at the image location (x,y), as a 32-bit integer.
MonkeyImage getSubImage ( tuple rect)
Creates a new MonkeyImage object from a rectangular selection of the current image.
boolean sameAs ( MonkeyImage other, float percent)
Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal".
void writeToFile ( string path, string format)
Writes the current image to the file specified by filename, in the format specified by format.

其中经常使用的为 sameAs(MonkeyImage other, float percent) , writeToFile(string path, string format)

sameAs(MonkeyImage other, float percent): 是对截取的image对象的比较, 不足之处是不能把手机界面和电脑上的图片进行比较

Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal".

short_begin = device.takeSnapshot()

short_end = device.takeSnapshot()

print short_end.sameAs(short_begin, 1.0)


writeToFile (string filename, string format): 保存当前截取的图片, 需要选择路径和格式。格式可以选择png, bmp, jpg等。

short_begin.writeToFile('d:\shotbegin.png','png')


你可能感兴趣的:(android,String,object,image,Integer,化工)