MonkeyImage
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
一個 monkeyrunner 類別,用於儲存裝置或模擬器螢幕的圖像。系統會從螢幕截圖緩衝區中複製圖像。這個物件的方法可將圖像轉換為各種儲存格式、將圖像寫入檔案、複製圖像的特定部分,以及將此物件與其他 MonkeyImage
物件進行比較。
您不需要建立新的 MonkeyImage
執行個體。請改為使用
MonkeyDevice.takeSnapshot()
從螢幕截圖中建立新的執行個體。例如,請使用:
newimage = MonkeyDevice.takeSnapshot()
摘要
方法 |
string
|
convertToBytes
(string format)
將目前的圖像轉換為特定格式,並以字串的形式傳回,接著就能以二進位元組的形式存取可疊代檔案。
|
tuple
|
getRawPixel
(integer x,
integer y)
傳回圖像位置 (x,y) 的單一像素,作為整數元組,格式為 (a,r,g,b)。
|
integer
|
getRawPixelInt
(integer x,
integer y)
傳回圖像位置 (x,y) 中的單一像素,做為 32 位元的整數。
|
MonkeyImage
|
getSubImage
(tuplerect)
從目前圖像的矩形中建立新的 MonkeyImage 物件。
|
boolean
|
sameAs
(MonkeyImage
other,
float percent)
將此 MonkeyImage 物件與其他物件比較,然後傳回比較結果。percent 引數會指定這兩種圖像可「相等」的百分比差。
|
void
|
writeToFile
(string path,
string format)
以 format 指定的格式將目前的圖像寫入 filename 指定的檔案。
|
公用方法
將目前的圖像轉換為特定格式,並以字串的形式傳回,接著就能以二進位元組的形式存取可疊代檔案。
引數
format |
所需的輸出格式。支援所有常見的光柵輸出格式。預設值為「png」(可攜式網路圖形)。 |
tuple
getRawPixel
(integer x,
integer y)
傳回圖像位置 (x,y) 的單一像素,作為整數元組,格式為 (a,r,g,b)。
引數
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 開始)。 |
傳回
-
做為 8 位元值像素的 a、r、g 和 b 值,合併為 32 位元整數,其中 a 是最左端的 8 位元,r 是最右端,依此類推。
MonkeyImage
getSubImage
(tuple rect)
從目前圖像的矩形中建立新的 MonkeyImage
物件。
引數
rect |
用於指定選項的元組 (x、y、w、h)。x 和 y 則指定選項左上角的「0」像素位置。w 可指定區域寬度,h 會指定高度 (以像素為單位)。
圖像的方向與擷取畫面的螢幕方向相同。
|
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」(可攜式網路圖形) 的預設格式。 |
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]