MonkeyImage模块功能详解

●MonkeyImage模块

图像处理相关的模块,主要是对截屏生成的图像进行一些操作。主要包括:

●MonkeyImage官方详细介绍

newimage = MonkeyDevice.takeSnapshot()

MonkeyImage是通过MonkeyDevice模块的takeSnapshot()方法来生成屏幕截图,然后生成MonkeyImage对象。

●MonkeyImage的方法

•convertToBytes //将当前图像转换为字节码

•getRawPixel //获取指定坐标位置的像素点,返回值(a,r,g,b)

•getRawPixelInt //把上面的像素数组以整形返回出来

•getSubImage //从MonkeyImage中获取子图像

•sameAs //比较两个MonkeyImage对象的相似度

•writeToFile //将图像写到文件中生成图片文件,一般是png

●代码演示

monkeyrunner

form com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage

device=MonkeyRunner.waitForConnection()//获取MonkeyDevice对象

image=device.takeSnapshot() //通过MonkeyDevice对象的takeSnapshot获取屏幕截图

image.convertToBytes()

image.getRawPixel(100,100)

image.getRawPixelInt(100,100)

接下来打开uiautomatorviewer获取计算器上数字6的坐标

subimage=image.getSubImage((543,895,267,293))                         

subimage.writeToFile('6.png','png')

tools目录下就能看到6.png图片

接下来获取计算器上数字9

subimage2 = image.getSubImage((543,602,267,293))

subimage2.writeToFile('9.png','png')

tools目录下就能看到9.png图片

接下来比较6.png和9.png两个子对象

subimage.sameAs(subimage2, 0.9) //0.9表示相似度

ture就表示两个图片相似

原文地址:https://www.cnblogs.com/ljf-hero/p/6632843.html