Android自动化测试之MonkeyRunner之MonkeyImage
A monkeyrunner class to hold an image of the device or emulator's screen. The image is copied from the screen buffer during a screenshot. This object's methods allow you to convert the image into various storage formats, write the image to a file, copy parts of the image, and compare this object to other MonkeyImage
objects.
You do not need to create new instances of MonkeyImage
. Instead, useMonkeyDevice.takeSnapshot()
to create a new instance from a screenshot. For example, use:
<span class="pln">newimage </span><span class="pun">=</span><span class="pln"> </span><span class="typ">MonkeyDevice</span><span class="pun">.</span><span class="pln">takeSnapshot</span><span class="pun">()</span>
Summary
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 .
|
Public Methods
tuple getRawPixel(integer x,integer y)
Returns the single pixel at the image location (x,y), as an a tuple ofinteger, in the form (a,r,g,b).
tuple getRawPixelInt(integer x,integer y)
Returns the single pixel at the image location (x,y), as an an integer. Use this method to economize on memory.
MonkeyImage
getSubImage(tuple rect)
Creates a new MonkeyImage
object from a rectangular selection of the current image.
Arguments
rect | A tuple (x, y, w, h) specifying the selection. x and y specify the 0-based pixel position of the upper left-hand corner of the selection. w specifies the width of the region, and h specifies its height, both in units of pixels. The image's orientation is the same as the screen orientation at the time the screenshot was made. |
---|
boolean sameAs(MonkeyImage
otherImage,float percent )
Compares this MonkeyImage
object to another and returns the result of the comparison. Thepercent
argument specifies the percentage difference that is allowed for the two images to be "equal".
void writeToFile(string filename,string format)
Writes the current image to the file specified by filename
, in the format specified byformat
.
Arguments
path | The fully-qualified filename and extension of the output file. |
---|---|
format | The output format to use for the file. If no format is provided, then the method tries to guess the format from the filename's extension. If no extension is provided and no format is specified, then the default format of "png" (Portable Network Graphics) is used. |