1 page.title=MonkeyImage 2 @jd:body 3 <style> 4 h4.jd-details-title {background-color: #DEE8F1;} 5 </style> 6 7 <p> 8 A monkeyrunner class to hold an image of the device or emulator's screen. The image is 9 copied from the screen buffer during a screenshot. This object's methods allow you to 10 convert the image into various storage formats, write the image to a file, copy parts of 11 the image, and compare this object to other <code>MonkeyImage</code> objects. 12 </p> 13 <p> 14 You do not need to create new instances of <code>MonkeyImage</code>. Instead, use 15 <code><a href="{@docRoot}guide/developing/tools/MonkeyDevice.html#takeSnapshot"> 16 MonkeyDevice.takeSnapshot()</a></code> to create a new instance from a screenshot. For example, use: 17 </p> 18 <pre> 19 newimage = MonkeyDevice.takeSnapshot() 20 </pre> 21 <h2>Summary</h2> 22 <table id="pubmethods" class="jd-sumtable"> 23 <tr> 24 <th colspan="12" style="background-color: #E2E2E2">Methods</th> 25 </tr> 26 <tr class="api" > 27 <td class="jd-typecol"> 28 <nobr> 29 <em>string</em> 30 </nobr> 31 </td> 32 <td class="jd-linkcol" width="100%"> 33 <nobr> 34 <span class="sympad"> 35 <a href="#convertToBytes">convertToBytes</a> 36 </span> 37 (<em>string</em> format) 38 </nobr> 39 <div class="jd-descrdiv"> 40 Converts the current image to a particular format and returns it as a 41 <em>string</em> that you can then access as an <em>iterable</em> of binary bytes. 42 </div> 43 </td> 44 </tr> 45 <tr class="api" > 46 <td class="jd-typecol"> 47 <nobr> 48 <em>tuple</em> 49 </nobr> 50 </td> 51 <td class="jd-linkcol" width="100%"> 52 <nobr> 53 <span class="sympad"> 54 <a href="#getRawPixel">getRawPixel</a> 55 </span> 56 (<em>integer</em> x, 57 <em>integer</em> y) 58 </nobr> 59 <div class="jd-descrdiv"> 60 Returns the single pixel at the image location (x,y), as an 61 a <em>tuple</em> of <em>integer</em>, in the form (a,r,g,b). 62 </div> 63 </td> 64 </tr> 65 <tr class="api" > 66 <td class="jd-typecol"> 67 <nobr> 68 <em>integer</em> 69 </nobr> 70 </td> 71 <td class="jd-linkcol" width="100%"> 72 <nobr> 73 <span class="sympad"> 74 <a href="#getRawPixelInt">getRawPixelInt</a> 75 </span> 76 (<em>integer</em> x, 77 <em>integer</em> y) 78 </nobr> 79 <div class="jd-descrdiv"> 80 Returns the single pixel at the image location (x,y), as 81 a 32-bit <em>integer</em>. 82 </div> 83 </td> 84 </tr> 85 <tr class="api" > 86 <td class="jd-typecol"> 87 <nobr> 88 <code> 89 <a href="{@docRoot}guide/developing/tools/MonkeyImage.html">MonkeyImage</a> 90 </code> 91 </nobr> 92 </td> 93 <td class="jd-linkcol" width="100%"> 94 <nobr> 95 <span class="sympad"> 96 <a href="#getSubImage">getSubImage</a> 97 </span> 98 (<em>tuple</em> rect) 99 </nobr> 100 <div class="jd-descrdiv"> 101 Creates a new <code>MonkeyImage</code> object from a rectangular selection of the 102 current image. 103 </div> 104 </td> 105 </tr> 106 <tr class="api" > 107 <td class="jd-typecol"> 108 <nobr> 109 <em>boolean</em> 110 </nobr> 111 </td> 112 <td class="jd-linkcol" width="100%"> 113 <nobr> 114 <span class="sympad"> 115 <a href="#sameAs">sameAs</a> 116 </span> 117 (<code><a href="{@docRoot}guide/developing/tools/MonkeyImage.html">MonkeyImage</a></code> 118 other, 119 <em>float</em> percent) 120 </nobr> 121 <div class="jd-descrdiv"> 122 Compares this <code>MonkeyImage</code> object to another and returns the result of 123 the comparison. The <code>percent</code> argument specifies the percentage 124 difference that is allowed for the two images to be "equal". 125 </div> 126 </td> 127 </tr> 128 <tr class="api" > 129 <td class="jd-typecol"> 130 <nobr> 131 <em>void</em> 132 </nobr> 133 </td> 134 <td class="jd-linkcol" width="100%"> 135 <nobr> 136 <span class="sympad"> 137 <a href="#writeToFile">writeToFile</a> 138 </span> 139 (<em>string</em> path, 140 <em>string</em> format) 141 </nobr> 142 <div class="jd-descrdiv"> 143 Writes the current image to the file specified by <code>filename</code>, in the 144 format specified by <code>format</code>. 145 </div> 146 </td> 147 </tr> 148 </table> 149 <!-- ========= METHOD DETAIL ======== --> 150 <!-- Public methods --> 151 <h2>Public Methods</h2> 152 <A NAME="convertToBytes"></A> 153 <div class="jd-details api "> 154 <h4 class="jd-details-title"> 155 <span class="normal"> 156 <em>string</em> 157 </span> 158 <span class="sympad">convertToBytes</span> 159 <span class="normal"> 160 ( 161 <em>string</em> format) 162 </span> 163 </h4> 164 <div class="jd-details-descr"> 165 166 <div class="jd-tagdata jd-tagdescr"> 167 <p> 168 Converts the current image to a particular format and returns it as a <em>string</em> 169 that you can then access as an <em>iterable</em> of binary bytes. 170 </p> 171 </div> 172 <div class="jd-tagdata"> 173 <h5 class="jd-tagtitle">Arguments</h5> 174 <table class="jd-tagtable"> 175 <tr> 176 <th>format</th> 177 <td> 178 The desired output format. All of the common raster output formats are supported. 179 The default value is "png" (Portable Network Graphics). 180 </td> 181 </tr> 182 </table> 183 </div> 184 </div> 185 </div> 186 <A NAME="getRawPixel"></A> 187 <div class="jd-details api "> 188 <h4 class="jd-details-title"> 189 <span class="normal"> 190 <em>tuple</em> 191 </span> 192 <span class="sympad">getRawPixel</span> 193 <span class="normal"> 194 (<em>integer</em> x, 195 <em>integer</em> y) 196 </span> 197 </h4> 198 <div class="jd-details-descr"> 199 200 <div class="jd-tagdata jd-tagdescr"> 201 <p> 202 Returns the single pixel at the image location (x,y), as an 203 a <em>tuple</em> of <em>integer</em>, in the form (a,r,g,b). 204 </p> 205 </div> 206 <div class="jd-tagdata"> 207 <h5 class="jd-tagtitle">Arguments</h5> 208 <table class="jd-tagtable"> 209 <tr> 210 <th>x</th> 211 <td> 212 The horizontal position of the pixel, starting with 0 at the left of the screen in the 213 orientation it had when the screenshot was taken. 214 </td> 215 </tr> 216 <tr> 217 <th>y</th> 218 <td> 219 The vertical position of the pixel, starting with 0 at the top of the screen in the 220 orientation it had when the screenshot was taken. 221 </td> 222 </tr> 223 </table> 224 </div> 225 <div class="jd-tagdata"> 226 <h5 class="jd-tagtitle">Returns</h5> 227 <ul class="nolist"> 228 <li> 229 A tuple of integers representing the pixel, in the form (a,r,g,b) where 230 a is the alpha channel value, and r, g, and b are the red, green, and blue values, 231 respectively. 232 </li> 233 </ul> 234 </div> 235 </div> 236 </div> 237 <A NAME="getRawPixelInt"></A> 238 <div class="jd-details api "> 239 <h4 class="jd-details-title"> 240 <span class="normal"> 241 <em>tuple</em> 242 </span> 243 <span class="sympad">getRawPixelInt</span> 244 <span class="normal"> 245 (<em>integer</em> x, 246 <em>integer</em> y) 247 </span> 248 </h4> 249 <div class="jd-details-descr"> 250 251 <div class="jd-tagdata jd-tagdescr"> 252 <p> 253 Returns the single pixel at the image location (x,y), as an 254 an <em>integer</em>. Use this method to economize on memory. 255 </p> 256 </div> 257 <div class="jd-tagdata"> 258 <h5 class="jd-tagtitle">Arguments</h5> 259 <table class="jd-tagtable"> 260 <tr> 261 <th>x</th> 262 <td> 263 The horizontal position of the pixel, starting with 0 at the left of the screen in the 264 orientation it had when the screenshot was taken. 265 </td> 266 </tr> 267 <tr> 268 <th>y</th> 269 <td> 270 The vertical position of the pixel, starting with 0 at the top of the screen in the 271 orientation it had when the screenshot was taken. 272 </td> 273 </tr> 274 </table> 275 </div> 276 <div class="jd-tagdata"> 277 <h5 class="jd-tagtitle">Returns</h5> 278 <ul class="nolist"> 279 <li> 280 The a,r,g, and b values of the pixel as 8-bit values combined into a 32-bit 281 integer, with a as the leftmost 8 bits, r the next rightmost, and so forth. 282 </li> 283 </ul> 284 </div> 285 </div> 286 </div> 287 <A NAME="getSubImage"></A> 288 <div class="jd-details api "> 289 <h4 class="jd-details-title"> 290 <span class="normal"> 291 <code> 292 <a href="{@docRoot}guide/developing/tools/MonkeyImage.html">MonkeyImage</a> 293 </code> 294 </span> 295 <span class="sympad">getSubImage</span> 296 <span class="normal"> 297 (<em>tuple</em> rect) 298 </span> 299 </h4> 300 <div class="jd-details-descr"> 301 302 <div class="jd-tagdata jd-tagdescr"> 303 <p> 304 Creates a new <code>MonkeyImage</code> object from a rectangular selection of the 305 current image. 306 </p> 307 </div> 308 <div class="jd-tagdata"> 309 <h5 class="jd-tagtitle">Arguments</h5> 310 <table class="jd-tagtable"> 311 <tr> 312 <th>rect</th> 313 <td> 314 A tuple (x, y, w, h) specifying the selection. x and y specify the 0-based pixel 315 position of the upper left-hand corner of the selection. w specifies the width of the 316 region, and h specifies its height, both in units of pixels. 317 <p> 318 The image's orientation is the same as the screen orientation at the time the 319 screenshot was made. 320 </p> 321 </td> 322 </tr> 323 </table> 324 </div> 325 <div class="jd-tagdata"> 326 <h5 class="jd-tagtitle">Returns</h5> 327 <ul class="nolist"> 328 <li> 329 A new <code>MonkeyImage</code> object containing the selection. 330 </li> 331 </ul> 332 </div> 333 </div> 334 </div> 335 <A NAME="sameAs"></A> 336 <div class="jd-details api "> 337 <h4 class="jd-details-title"> 338 <span class="normal"> 339 <em>boolean</em> 340 </span> 341 <span class="sympad">sameAs</span> 342 <span class="normal"> 343 ( 344 <code> 345 <a href="{@docRoot}guide/developing/tools/MonkeyImage.html">MonkeyImage</a> 346 </code> otherImage, 347 <em>float</em> percent 348 ) 349 </span> 350 </h4> 351 <div class="jd-details-descr"> 352 353 <div class="jd-tagdata jd-tagdescr"> 354 <p> 355 Compares this <code>MonkeyImage</code> object to another and returns the result of 356 the comparison. The <code>percent</code> argument specifies the percentage 357 difference that is allowed for the two images to be "equal". 358 </p> 359 </div> 360 <div class="jd-tagdata"> 361 <h5 class="jd-tagtitle">Arguments</h5> 362 <table class="jd-tagtable"> 363 <tr> 364 <th>other</th> 365 <td> 366 Another <code>MonkeyImage</code> object to compare to this one. 367 </td> 368 </tr> 369 <tr> 370 <th> 371 percent 372 </th> 373 <td> 374 A float in the range 0.0 to 1.0, inclusive, indicating 375 the percentage of pixels that need to be the same for the method to return 376 <code>true</code>. The default is 1.0, indicating that all the pixels 377 must match. 378 </td> 379 </tr> 380 </table> 381 </div> 382 <div class="jd-tagdata"> 383 <h5 class="jd-tagtitle">Returns</h5> 384 <ul class="nolist"> 385 <li> 386 Boolean <code>true</code> if the images match, or boolean <code>false</code> otherwise. 387 </li> 388 </ul> 389 </div> 390 </div> 391 </div> 392 <A NAME="writeToFile"></A> 393 <div class="jd-details api "> 394 <h4 class="jd-details-title"> 395 <span class="normal"> 396 void 397 </span> 398 <span class="sympad">writeToFile</span> 399 <span class="normal"> 400 (<em>string</em> filename, 401 <em>string</em> format) 402 </span> 403 </h4> 404 <div class="jd-details-descr"> 405 406 <div class="jd-tagdata jd-tagdescr"> 407 <p> 408 Writes the current image to the file specified by <code>filename</code>, in the 409 format specified by <code>format</code>. 410 </p> 411 </div> 412 <div class="jd-tagdata"> 413 <h5 class="jd-tagtitle">Arguments</h5> 414 <table class="jd-tagtable"> 415 <tr> 416 <th>path</th> 417 <td> 418 The fully-qualified filename and extension of the output file. 419 </td> 420 </tr> 421 <tr> 422 <th> 423 format 424 </th> 425 <td> 426 The output format to use for the file. If no format is provided, then the 427 method tries to guess the format from the filename's extension. If no 428 extension is provided and no format is specified, then the default format of 429 "png" (Portable Network Graphics) is used. 430 </td> 431 </tr> 432 </table> 433 </div> 434 </div> 435 </div> 436