MonkeyImage
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คลาส Monkeyrunner เพื่อเก็บรูปภาพหน้าจอของอุปกรณ์หรือโปรแกรมจำลอง รูปภาพคือ
คัดลอกจากบัฟเฟอร์หน้าจอในระหว่างที่แสดงภาพหน้าจอ เมธอดของออบเจ็กต์นี้ช่วยให้คุณทำสิ่งต่อไปนี้ได้
แปลงรูปภาพเป็นที่เก็บข้อมูลรูปแบบต่างๆ เขียนภาพเป็นไฟล์ คัดลอกส่วนต่างๆ
รูปภาพและเปรียบเทียบออบเจ็กต์นี้กับออบเจ็กต์MonkeyImage
อื่นๆ
คุณไม่จำเป็นต้องสร้างอินสแตนซ์ใหม่ของ MonkeyImage
ให้ใช้
MonkeyDevice.takeSnapshot()
เพื่อสร้างอินสแตนซ์ใหม่จากภาพหน้าจอ ตัวอย่างเช่น ให้ใช้ข้อความต่อไปนี้
newimage = MonkeyDevice.takeSnapshot()
สรุป
วิธีการ |
สตริง
|
convertToBytes
(รูปแบบ สตริง)
แปลงรูปภาพปัจจุบันเป็นรูปแบบเฉพาะและแสดงผลเป็น
string ที่คุณเข้าถึงได้ในรูปแบบ iterable ของไบต์ไบนารี
|
tuple
|
รับพิกเซล
(จำนวนเต็ม x,
จำนวนเต็ม y)
แสดงพิกเซลเดี่ยวที่ตำแหน่งภาพ (x,y) เป็น
tuple ของ จำนวนเต็ม ในรูปแบบ (a,r,g,b)
|
จำนวนเต็ม
|
ดึงพิกเซลInt
(จำนวนเต็ม x,
จำนวนเต็ม y)
แสดงพิกเซลเดี่ยวที่ตำแหน่งภาพ (x,y) เป็น
จำนวนเต็ม 32 บิต
|
MonkeyImage
|
getSubImage
(สี่เหลี่ยมผืนผ้า tuple)
สร้างออบเจ็กต์ MonkeyImage ใหม่จากรายการที่เลือกเป็นรูปสี่เหลี่ยมผืนผ้า
ภาพปัจจุบัน
|
บูลีน
|
sameAs
(MonkeyImage )
อื่นๆ
เปอร์เซ็นต์แบบลอย)
เปรียบเทียบออบเจ็กต์ MonkeyImage นี้กับออบเจ็กต์อื่น และแสดงผลลัพธ์ของ
การเปรียบเทียบ อาร์กิวเมนต์ percent จะระบุเปอร์เซ็นต์
ความแตกต่างที่อนุญาตให้ใช้ระหว่างรูปภาพทั้ง 2 รูปเป็น "เท่ากัน"
|
เป็นโมฆะ
|
writeToFile
(เส้นทาง สตริง,
รูปแบบ สตริง)
เขียนรูปภาพปัจจุบันไปยังไฟล์ที่ระบุโดย filename ใน
รูปแบบที่ระบุโดย format
|
วิธีการสาธารณะ
แปลงรูปภาพปัจจุบันเป็นรูปแบบเฉพาะและแสดงผลเป็นสตริง
ที่คุณจะเข้าถึงในฐานะย่อยของไบต์ไบนารีได้
อาร์กิวเมนต์
รูปแบบ |
รูปแบบเอาต์พุตที่ต้องการ ระบบรองรับรูปแบบเอาต์พุตแรสเตอร์ทั่วไปทั้งหมด
ค่าเริ่มต้นคือ "png" (Portable Network Graphics)
|
tuple
รับพิกเซล
(จำนวนเต็ม x
จำนวนเต็ม ป)
แสดงพิกเซลเดี่ยวที่ตำแหน่งภาพ (x,y) เป็น
tuple ของ จำนวนเต็ม ในรูปแบบ (a,r,g,b)
อาร์กิวเมนต์
x |
ตำแหน่งแนวนอนของพิกเซล ซึ่งเริ่มต้นด้วย 0 ทางด้านซ้ายของหน้าจอใน
การวางแนวที่มีอยู่เมื่อถ่ายภาพหน้าจอ
|
y |
ตำแหน่งแนวตั้งของพิกเซล ซึ่งเริ่มต้นด้วย 0 ที่ด้านบนของหน้าจอใน
การวางแนวที่มีอยู่เมื่อถ่ายภาพหน้าจอ
|
คิกรีเทิร์น
-
ทูเปิลของจำนวนเต็มที่แทนพิกเซลในรูปแบบ (a,r,g,b) โดยที่
a คือค่าช่องอัลฟ่า ส่วน r, g และ b คือค่าสีแดง เขียว และน้ำเงิน
ตามลำดับ
tuple
รับพิกเซลInt
(จำนวนเต็ม x
จำนวนเต็ม ป)
แสดงพิกเซลเดี่ยวที่ตำแหน่งภาพ (x,y) เป็น
จำนวนเต็ม ใช้วิธีนี้ประหยัดหน่วยความจำ
อาร์กิวเมนต์
x |
ตำแหน่งแนวนอนของพิกเซล ซึ่งเริ่มต้นด้วย 0 ทางด้านซ้ายของหน้าจอใน
การวางแนวที่มีอยู่เมื่อถ่ายภาพหน้าจอ
|
y |
ตำแหน่งแนวตั้งของพิกเซล ซึ่งเริ่มต้นด้วย 0 ที่ด้านบนของหน้าจอใน
การวางแนวที่มีอยู่เมื่อถ่ายภาพหน้าจอ
|
คิกรีเทิร์น
-
ค่า a,r,g และ b ของพิกเซลเป็นค่า 8 บิตที่รวมกันเป็น 32 บิต
จำนวนเต็มที่มี 8 บิตทางซ้ายสุด r ทางขวาสุด ถัดไปเป็น 8 บิต เป็นต้น
วันที่
MonkeyImage
getSubImage
(สี่เหลี่ยมผืนผ้า tuple)
สร้างออบเจ็กต์ MonkeyImage
ใหม่จากรายการที่เลือกเป็นรูปสี่เหลี่ยมผืนผ้า
ภาพปัจจุบัน
อาร์กิวเมนต์
rect |
ทูเปิล (x, y, w, h) ที่ระบุการเลือก x และ y ระบุพิกเซลฐาน 0
ของมุมซ้ายบนของสิ่งที่เลือก w ระบุความกว้างของฟิลด์
ภูมิภาค และ h จะระบุความสูงของพื้นที่เป็นหน่วยพิกเซล
การวางแนวของรูปภาพจะตรงกับการวางแนวหน้าจอในช่วงเวลาที่
สร้างภาพหน้าจอแล้ว
|
คิกรีเทิร์น
-
ออบเจ็กต์
MonkeyImage
ใหม่ที่มีการเลือกนี้
บูลีน
sameAs
(
รูปภาพอื่นๆ
MonkeyImage
เปอร์เซ็นต์จำนวนลอยตัว
)
เปรียบเทียบออบเจ็กต์ MonkeyImage
นี้กับออบเจ็กต์อื่น และแสดงผลลัพธ์ของ
การเปรียบเทียบ อาร์กิวเมนต์ percent
จะระบุเปอร์เซ็นต์
ความแตกต่างที่อนุญาตให้ใช้ระหว่างรูปภาพทั้ง 2 รูปเป็น "เท่ากัน"
อาร์กิวเมนต์
อื่นๆ |
ออบเจ็กต์ MonkeyImage อีกรายการที่จะเปรียบเทียบกับออบเจ็กต์นี้
|
เปอร์เซ็นต์
|
จำนวนลอยตัวในช่วง 0.0 ถึง 1.0 (รวมค่าทศนิยม) ซึ่งระบุ
เปอร์เซ็นต์ของพิกเซลที่ต้องเหมือนกันเพื่อให้เมธอดแสดงผล
true ค่าเริ่มต้นคือ 1.0 ซึ่งหมายความว่าพิกเซลทั้งหมด
ต้องตรงกัน
|
คิกรีเทิร์น
-
บูลีน
true
หากรูปภาพตรงกัน หรือบูลีน false
เป็นอย่างอื่น
เขียนรูปภาพปัจจุบันไปยังไฟล์ที่ระบุโดย filename
ใน
รูปแบบที่ระบุโดย format
อาร์กิวเมนต์
เส้นทาง |
ชื่อไฟล์และนามสกุลเต็มของไฟล์เอาต์พุต
|
รูปแบบ
|
รูปแบบเอาต์พุตที่ใช้สำหรับไฟล์ หากไม่มีรูปแบบให้
จะพยายามเดารูปแบบจากนามสกุลของชื่อไฟล์ หากไม่ใช่
มีส่วนขยายและไม่มีการระบุรูปแบบ จะใช้รูปแบบเริ่มต้นของ
"PNG" (รูปแบบกราฟิกเครือข่ายแบบพกพา)
|
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],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|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]