MonkeyImage
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
存储设备或模拟器屏幕截图的 monkeyrunner 类。在截图过程中,系统会从屏幕缓冲区复制该图片。此对象的方法可让您将该图片转换为各种存储格式,将该图片写入文件,复制该图片的某些部分,还可以将此对象与其他 MonkeyImage
对象进行比较。
您无需创建 MonkeyImage
的新实例,而是可以使用
MonkeyDevice.takeSnapshot()
根据屏幕截图创建一个新实例。例如,使用以下命令:
newimage = MonkeyDevice.takeSnapshot()
摘要
方法 |
string
|
convertToBytes
(string format)
将当前图片转换为特定格式并以字符串形式返回该图片,然后您可以将其作为二进制字节的可迭代对象来访问。
|
tuple
|
getRawPixel
(integer x,
integer y)
以 (a,r,g,b) 格式的整数元组形式返回图片位置 (x,y) 处的单个像素。
|
integer
|
getRawPixelInt
(integer x,
integer y)
以 32 位整数形式返回图片位置 (x,y) 处的单个像素。
|
MonkeyImage
|
getSubImage
(tuple rect)
根据当前图片的矩形选择区创建一个新的 MonkeyImage 对象。
|
boolean
|
sameAs
(MonkeyImage
other,
float percent)
将此 MonkeyImage 对象与另一个对象进行比较,并返回比较结果。percent 参数指定两张“等同”的图片之间可以存在的百分比差异。
|
void
|
writeToFile
(string path,
string format)
将当前图片以 format 指定的格式写入 filename 指定的文件。
|
公开方法
将当前图片转换为特定格式并以字符串形式返回该图片,然后您可以将其作为二进制字节的可迭代对象来访问。
参数
format |
所需的输出格式。支持所有常用的光栅输出格式。默认值为“png”(Portable Network Graphics)。
|
tuple
getRawPixel
(integer x,
integer y)
以 (a,r,g,b) 格式的整数元组形式返回图片位置 (x,y) 处的单个像素。
参数
x |
像素的水平位置,以屏幕左边的像素 0 开始,与截取屏幕截图时的方向一致。
|
y |
像素的垂直位置,以屏幕顶部的像素 0 开始,与截取屏幕截图时的方向一致。
|
返回
-
一个 (a,r,g,b) 格式的表示像素的整数元组,其中 a 是 Alpha 通道值,r、g 和 b 分别表示红色、绿色和蓝色值。
tuple
getRawPixelInt
(integer x,
integer y)
以整数形式返回图片位置 (x,y) 处的单个像素。使用此方法可以节省内存。
参数
x |
像素的水平位置,以屏幕左边的像素 0 开始,与截取屏幕截图时的方向一致。
|
y |
像素的垂直位置,以屏幕顶部的像素 0 开始,与截取屏幕截图时的方向一致。
|
返回
-
像素的 a、r、g 和 b 值(以 8 位值形式合并为 32 位整数),其中 a 是最左边的 8 位,r 是最右边的 8 位,依此类推。
MonkeyImage
getSubImage
(tuple rect)
根据当前图片的矩形选择区创建一个新的 MonkeyImage
对象。
参数
rect |
指定所选内容的元组 (x, y, w, h)。x 和 y 表示所选内容左上角从 0 开始的像素位置。w 表示区域的宽度,h 表示其高度,均以像素为单位。
图片的方向与截取屏幕截图时的屏幕方向相同。
|
返回
-
一个包含所选内容的新
MonkeyImage
对象。
boolean
sameAs
(
MonkeyImage
otherImage,
float percent
)
将此 MonkeyImage
对象与另一个对象进行比较,并返回比较结果。percent
参数指定两张“等同”的图片之间可以存在的百分比差异。
参数
other |
要与此对象进行比较的另一个 MonkeyImage 对象。
|
percent
|
介于 0.0(含)到 1.0(含)的浮点数,表示要使方法返回 true ,必须相同的像素所占百分比。默认值为 1.0,表示所有像素均必须匹配。
|
返回
-
如果图片匹配,则返回布尔值
true
,否则返回布尔值 false
。
将当前图片以 format
指定的格式写入 filename
指定的文件。
参数
path |
输出文件的完全限定文件名和扩展名。
|
format
|
文件的输出格式。如果没有提供格式,该方法将尝试根据文件扩展名猜出格式。如果未提供扩展名且未指定格式,则使用默认格式“png”(Portable Network Graphics)。
|
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# MonkeyImage\n\nA monkeyrunner class to hold an image of the device or emulator's screen. The image is\ncopied from the screen buffer during a screenshot. This object's methods allow you to\nconvert the image into various storage formats, write the image to a file, copy parts of\nthe image, and compare this object to other `MonkeyImage` objects.\n\n\nYou do not need to create new instances of `MonkeyImage`. Instead, use\n[MonkeyDevice.takeSnapshot()](/tools/help/MonkeyDevice#takeSnapshot) to create a new instance from a screenshot. For example, use: \n\n```\nnewimage = MonkeyDevice.takeSnapshot()\n```\n\nSummary\n-------\n\n| Methods ||||||||||||\n|----------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|---|---|---|---|---|---|---|---|---|\n| *string* | [convertToBytes](#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. |\n| *tuple* | [getRawPixel](#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). |\n| *integer* | [getRawPixelInt](#getRawPixelInt) (*integer* x, *integer* y) Returns the single pixel at the image location (x,y), as a 32-bit *integer*. |\n| ` `[MonkeyImage](/tools/help/MonkeyImage)` ` | [getSubImage](#getSubImage) (*tuple* rect) Creates a new `MonkeyImage` object from a rectangular selection of the current image. |\n| *boolean* | [sameAs](#sameAs) ([MonkeyImage](/tools/help/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\". |\n| *void* | [writeToFile](#writeToFile) (*string* path, *string* format) Writes the current image to the file specified by `filename`, in the format specified by `format`. |\n\nPublic methods\n--------------\n\n#### *string*\nconvertToBytes\n( *string* format)\n\n\nConverts the current image to a particular format and returns it as a *string*\nthat you can then access as an *iterable* of binary bytes. \n\n##### Arguments\n\n| format | The desired output format. All of the common raster output formats are supported. The default value is \"png\" (Portable Network Graphics). |\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------|\n\n#### *tuple*\ngetRawPixel\n(*integer* x, *integer* y)\n\n\nReturns the single pixel at the image location (x,y), as an\na *tuple* of *integer*, in the form (a,r,g,b). \n\n##### Arguments\n\n| x | The horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken. |\n| y | The vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken. |\n|---|------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A tuple of integers representing the pixel, in the form (a,r,g,b) where a is the alpha channel value, and r, g, and b are the red, green, and blue values, respectively. \n\n#### *tuple*\ngetRawPixelInt\n(*integer* x, *integer* y)\n\n\nReturns the single pixel at the image location (x,y), as an\nan *integer*. Use this method to economize on memory. \n\n##### Arguments\n\n| x | The horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken. |\n| y | The vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken. |\n|---|------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- The a,r,g, and b values of the pixel as 8-bit values combined into a 32-bit integer, with a as the leftmost 8 bits, r the next rightmost, and so forth. \n\n#### `\n`[MonkeyImage](/tools/help/MonkeyImage)`\n`\ngetSubImage\n(*tuple* rect)\n\n\nCreates a new `MonkeyImage` object from a rectangular selection of the\ncurrent image. \n\n##### Arguments\n\n| 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. |\n|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A new `MonkeyImage` object containing the selection. \n\n#### *boolean*\nsameAs\n( `\n`[MonkeyImage](/tools/help/MonkeyImage)`\n` otherImage, *float* percent )\n\n\nCompares this `MonkeyImage` object to another and returns the result of\nthe comparison. The `percent` argument specifies the percentage\ndifference that is allowed for the two images to be \"equal\". \n\n##### Arguments\n\n| other | Another `MonkeyImage` object to compare to this one. |\n| percent | A float in the range 0.0 to 1.0, inclusive, indicating the percentage of pixels that need to be the same for the method to return `true`. The default is 1.0, indicating that all the pixels must match. |\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- Boolean `true` if the images match, or boolean `false` otherwise. \n\n#### void\nwriteToFile\n(*string* filename, *string* format)\n\n\nWrites the current image to the file specified by `filename`, in the\nformat specified by `format`. \n\n##### Arguments\n\n| path | The fully-qualified filename and extension of the output file. |\n| 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. |\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]