MonkeyImage Class

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.

monkeyimage是截取设备或者模拟器屏幕截图的类. 图片是从屏幕的缓存中复制的.这个类的方法允许你把图片转换成各种格式,把图片写进文件,复制图片的一部分,比较这个对象和其他monkeyimage对象.

You do not need to create new instances of MonkeyImage. Instead, use MonkeyDevice.takeSnapshot() to create a new instance from a screenshot. For example, use:

你不需要创建一个monkeyimage的实例, 使用MonkeyDevice.takeSnapshot()从screenshot创建一个新的实例.例如,用:

newimage = MonkeyDevice.takeSnapshot()

Public 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.

  把当前的图片转换到指定的格式 并且 返回为一个可迭代的二进制字符串.

  Arguments

  format      The desired output format. All of the common raster output formats are supported. The default value is "png" (Portable Network Graphics).

  参数: format , 希望的输出格式. 所有个常见光栅格式都是可支持的. 默认的格式是"PNG"(便携式网络图像格式)

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).

    返回指定(x,y)坐标的像素, 是(a,r,g,b)格式的整数元组.

   Arguments

           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.

    X  像素的横坐标,屏幕左边是0点.

          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.

    Y  像素的纵坐标, 屏幕上边是它的0点.

  Returns

    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.

    一个表示像素的整数元组, 在表格(a,r,g,b)中, 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.

   返回指定坐标(x,y)的像素. 使用这个方法节省内存.  

  Arguments

    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.

    X  像素的横坐标,屏幕左边是0点.

    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.

    Y  像素的纵坐标, 屏幕上边是它的0点.

  Returns

    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.

    a,r,g,b组成一个32位的整数.前8位是a,下面8位是r一次类推.

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.

    一个元组指定了所选位置. X,Y指定矩形的左上角(作为原点),W指定区域宽度,H指定高度.所有的单位都是像素.

    The image's orientation is the same as the screen orientation at the time the screenshot was made.

    图片的朝向是和屏幕当时截屏朝向相同.

  Returns

    A new MonkeyImage object containing the selection.

    返回一个包含所选区域的对象.

boolean sameAs ( MonkeyImage otherImage, 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".

    把当前monkeyimage对象和另一个做对比,并且返回对比结果.百分数参数指定的是允许两个image"相同"的百分数差.

  Arguments

    other        Another MonkeyImage object to compare to this one.

          要和这个monkeyimage对比的另一个对象.

    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.

          一个0到1之间的浮点数,   表示百分制多少必须相等的像素才能返回true. 默认值是1,表示所有像素都必须相等.

    Returns

      Boolean true if the images match, or boolean false otherwise.

      如果相等返回布尔值真,反之返回布尔值假.

void writeToFile (string filename, string format)

  Writes the current image to the file specified by filename, in the format specified by format.

  把当前图片写入到指定文件名的文件. 在format 参数中指定格式.

  Arguments

    filename  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.

          文件的格式, 如果没有提供格式,这个方法会从文件名上的扩展名去猜测, 如果扩展名和格式都没有指定,会使用默认的"png"格式.

原文地址:https://www.cnblogs.com/hanxiaocai/p/3753454.html