1.    背景

鉴于网上使用MonkeyImage的实例除了方法sameAs外很难找到,所以本人把实践各个API的过程记录下来然自己有更感性的认识,也为往后的工作打下更好的基础。同时也和上一篇文章《MonkeyDevcie API 实践全记录》起到相互呼应的作用。

因为并没有MonkeyRunner的项目背景,所以这里更多的是描述各个API是怎么一回事,而不是描述在什么场景下需要用到。也就是说是去回答What,而不是How。 首先我们先看下官方给出的MonkeyImage的API描述,对比我现在反编译的最新的源码是一致的:

Return Type

Methods

Comment

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.  

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

integer

getRawPixelInt (integer x, integer y)Returns the single pixel at the image location (x,y), as a 32-bit integer.  

MonkeyImage

getSubImage (tuple rect)Creates a new MonkeyImage object from a rectangular selection of the current image.  

boolean

sameAs (MonkeyImage other, float percent)Compares this MonkeyImage object to another and returns the result of the comparison. Thepercent argument specifies the percentage difference that is allowed for the two images to be "equal".  

void

writeToFile (string path, string format)Writes the current image to the file specified by filename, in the format specified by format.  

2.      String convertToBytes(string format)

2.1  示例

img = device.takeSnapshot()

png1 = img.convertToBytes()

png2 = img.convertToBytes()

bmp = img.convertToBytes('bmp')

jpg = img.convertToBytes('JPG')

gif = img.convertToBytes('gif')

raw = img.convertToBytes('raw')

invalid = img.convertToBytes('xxx')

#is the 2 pngs equal?
print "Two png is equal in bytes:",png1 == png2 #is the png equals to bmp?
print "png and bmp is equal in bytes:", png1 == bmp #is the jpg eqals to the raw?
print "jpg and bmp is equals in bytes:",jpg == bmp #is the jpg eqals to the xxx?
print "jpg is a valid argument:",jpg != invalid #is the gif eqals to the xxx?
print "gif is a valid argument:",gif != invalid #is the bmp eqals to the xxx?
print "bmp is a valid argument:",bmp != invalid #is the raw equas to xxxx? aims at checking whether argument 'raw' is invalid like 'xxx'
print 'raw is a valid argument:',raw != invalid #would invalid argument drop to png by default?
print 'Would invalid argument drop to png by default:',png1 == invalid

输出:

2.2 分析

除了默认的png,常用格式jpg,gif都支持,但bmp格式无效,至于还支持什么其他格式,尝试跟踪了下代码,没有找到想要的结果
 

3. tuple getRawPixel(integer x, integer y)和Integer getRawPixelInt (integer x, integer y)

3.1 示例

viewer = device.getHierarchyViewer()
note = viewer.findViewById('id/title')
text = viewer.getText(note)
print text.encode('utf-8') point = viewer.getAbsoluteCenterOfView(note)
x = point.x
y = point.y img = device.takeSnapshot() pixelTuple = img.getRawPixel(x,y)
pixelInt = img.getRawPixelInt(x,y)
print "Pixel in tuple:",pixelTuple
print "Pixel in int:", pixelInt

输出:

3.2 分析

这里把两个相似的方法放到一起来比较,他们都是获得指定一个坐标的argb值,其中a就是alpha(透明度),rgb就是颜色三元组红绿蓝了。但前者返回的是一个元组,后者返回的是整型。
那么两个类型的值是怎么对应起来的呢?其实就是第一个方法的元组的返回值(a,r,g,b)中的每个值转换成8个bit的二进制值然后按顺序从左到右排列起来再转换成十进制整型就是第二个方法的返回值了。
以示例输出为例,比如b在第一个返回值中是141,换成二进制就是1001101,其他雷同。
再看第二个方法的整型返回值是-7500403,转换成二进制其实就是11111111100011011000110110001101(至于下图calculator转换后为什么前面那么多个1,其实不用管他,因为是负数所以前面要加上FF之类而已),那么最后的8个bit转换成十进制其实就是上面的的141.
 

4. MonkeyImage getSubImage(tuple rect)

4.1 示例

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode, Window
from java.awt import Point #from com.android.hierarchyviewerlib.device import #Connect to the target targetDevice
targetDevice = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need this
targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList") #invoke the menu options
MonkeyRunner.sleep(6)
#targetDevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP); '''
public ViewNode findViewById(String id)
* @param id id for the view.
* @return view with the specified ID, or {@code null} if no view found.
'''
#MonkeyRunner.alert("Continue?", "help", "Ok?") pic = targetDevice.takeSnapshot()
pic = pic.getSubImage((0,38,480,762)) newPic = targetDevice.takeSnapshot()
newPic = newPic.getSubImage((0,38,480,762)) print (newPic.sameAs(pic,1.0))
newPic.writeToFile('./shot1.png','png')

4.2 分析

以上示例流程是
  • 打开NotePad的NotesList Activity
  • 按下Menu Options按钮弹出“Add note”这个Menu Entry
  • 截取一个屏幕
  • 调用getSubImage来取得去掉屏幕最上面的状态栏(因为有时间不断变化,所以每截屏一次可能都会有所改变)和最下面的Menu Options的一个Image
  • 再重复以上两个步骤取得另外一个Image
  • 比较以上两个image是否相同
  • 把第二个image写到本地。
 

5 boolean sameAs(MonkeyImage other, float percent)

5.1 示例

见4.1

5.2 分析

流程见4.1,这里要注意第二个浮点型的参数是从0.0到1.0, 1.0代表必须100%相同,0.5代表可以有50%的Error Tolerance.
 

6. void writeToFile (string path, string format)

6.1 示例

请参见第4章节

6.2 分析

参数很明了,这里需要提一下的是第一个参数路径,如果你填写的是相对路径的话,base用得是MonkeyRunner。也就是说示例中的图片最终是保存在我的monkeyrunner可执行程序的上一层目录。
 
作者 自主博客 微信服务号及扫描码 CSDN
天地会珠海分舵 http://techgogogo.com 服务号:TechGoGoGo扫描码: http://blog.csdn.net/zhubaitian
 

MonkeyImage API 实践全记录的更多相关文章

  1. MonkeyDevcie API 实践全记录

    1.    背景 使用SDK自带的NotePad应用作为实践目标应用,目的是对MonkeyDevice拥有的成员方法做一个初步的了解. 以下是官方列出的方法的Overview. Return Type ...

  2. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(二)

    用mogoose搭建restful测试接口 接着上一篇(Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一))记录,今天单独搭建一个restful测试接口,和项目前 ...

  3. .Net Core Web Api实践之中间件的使用(一)

    前言:从2019年年中入坑.net core已半年有余,总体上来说虽然感觉坑多,但是用起来还是比较香的.本来我是不怎么喜欢写这类实践分享或填坑记录的博客的,因为初步实践坑多,文章肯定也会有各种错误,跟 ...

  4. 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法

    若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...

  5. ASP.NET Web API实践系列04,通过Route等特性设置路由

    ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...

  6. 老李推荐:第3章3节《MonkeyRunner源码剖析》脚本编写示例: MonkeyImage API使用示例 1

    老李推荐:第3章3节<MonkeyRunner源码剖析>脚本编写示例: MonkeyImage API使用示例   在上一节的第一个“增加日记”的示例中,我们并没有看到日记是否真的增加成功 ...

  7. 在CentOS6上配置MHA过程全记录

    在CentOS6上配置MHA过程全记录 MHA(Master High Availability)是一款开源的MariaDB or MySQL高可用程序,为MariaDB or MySQL主从复制架构 ...

  8. 在CentOS7上通过RPM安装实现LAMP+phpMyAdmin过程全记录

    在CentOS7上通过RPM安装实现LAMP+phpMyAdmin过程全记录 时间:2017年9月20日 一.软件环境: IP:192.168.1.71 Hostname:centos73-2.sur ...

  9. Dynamics CRM2016 Web API之创建记录

    前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...

随机推荐

  1. Struts1.X与Spring集成——第一种方案

    spring+struts(第一种方案) 集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,调用业务逻辑方法. 一,新建一个项目Spring_Strut ...

  2. executeBatch()相关操作汇总

    环境:oracle使用PreparedStatement的executeBatch方法,如果DML操作成功,返回值[-2,-2,...]an array of update counts contai ...

  3. div+js 弹出层

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  4. Android组件系列----ContentProvider内容提供商【5】

    2.执行query()方法,查询全部记录(眼下一共两条记录).后台输出效果例如以下: 经測试,其它方法也都是能够运行的. 事实证明,新建的另外一个project文件ContentResolverTes ...

  5. Html5响应式设计与实现广场

    由于提出的想法响应式设计,越来越多的网站使用这样的思想.各类大型网站如雨后春笋般涌了出来.例如:小米商城.天猫等. 至于响应式设计的概念等大家能够去百度百度,我这里就不相信解说了.直接为大家带来源代码 ...

  6. C# 闭包问题

    C# 闭包问题-你被”坑“过吗? 引言 闭包是什么?以前看面试题的时候才发现这个名词. 闭包在实际项目中会有什么问题?现在就让我们一起来看下这个不太熟悉的名词. 如果在实际工作中用到了匿名函数和lam ...

  7. JAVA缓存技术之EhCache(转)

    最近再ITEYE上看到关于讨论JAVA缓存技术的帖子比较多,自己不懂,所以上网大概搜了下,找到一篇,暂作保存,后面如果有用到可以参考.此为转贴,帖子来处:http://cogipard.info/ar ...

  8. Cocos2d-x Auto-batching 浅浅的”深入分析”

    Auto-batching是Cocos2d-x3.0新增的特性,目的是为了代替SpriteBatchNode,完毕渲染的批处理,提高绘制效率. 至于它有什么特点,能够看看官方文档,这里主要想探讨Aut ...

  9. 网络广告公司的恐慌 XSS广告终结者(html5新特性)

    人们在平时网络冲浪的时候,有时候会发现当前网站有很多莫名其妙的广告,这些广告会被导向到站外. 这些广告可能是你安装了一些免费网络软件后强制安装的浏览器插件导致的. 譬如chrome浏览器:

  10. DBA工具——DMV——如何知道TSQL语句已运行了多久

    原文:DBA工具--DMV--如何知道TSQL语句已运行了多久 DBA通常想知道正在运行的语句已经执行了多久了?可以使用Sqlserver profiler来捕获语句的开始时间,和现有时间比较,但是在 ...