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  示例

  1. img = device.takeSnapshot()
  2.  
  3. png1 = img.convertToBytes()
  4.  
  5. png2 = img.convertToBytes()
  6.  
  7. bmp = img.convertToBytes('bmp')
  8.  
  9. jpg = img.convertToBytes('JPG')
  10.  
  11. gif = img.convertToBytes('gif')
  12.  
  13. raw = img.convertToBytes('raw')
  14.  
  15. invalid = img.convertToBytes('xxx')
  16.  
  17. #is the 2 pngs equal?
  18. print "Two png is equal in bytes:",png1 == png2
  19.  
  20. #is the png equals to bmp?
  21. print "png and bmp is equal in bytes:", png1 == bmp
  22.  
  23. #is the jpg eqals to the raw?
  24. print "jpg and bmp is equals in bytes:",jpg == bmp
  25.  
  26. #is the jpg eqals to the xxx?
  27. print "jpg is a valid argument:",jpg != invalid
  28.  
  29. #is the gif eqals to the xxx?
  30. print "gif is a valid argument:",gif != invalid
  31.  
  32. #is the bmp eqals to the xxx?
  33. print "bmp is a valid argument:",bmp != invalid
  34.  
  35. #is the raw equas to xxxx? aims at checking whether argument 'raw' is invalid like 'xxx'
  36. print 'raw is a valid argument:',raw != invalid
  37.  
  38. #would invalid argument drop to png by default?
  39. 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 示例

  1. viewer = device.getHierarchyViewer()
  2. note = viewer.findViewById('id/title')
  3. text = viewer.getText(note)
  4. print text.encode('utf-8')
  5.  
  6. point = viewer.getAbsoluteCenterOfView(note)
  7. x = point.x
  8. y = point.y
  9.  
  10. img = device.takeSnapshot()
  11.  
  12. pixelTuple = img.getRawPixel(x,y)
  13. pixelInt = img.getRawPixelInt(x,y)
  14. print "Pixel in tuple:",pixelTuple
  15. 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 示例

  1. from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
  2. from com.android.monkeyrunner.easy import EasyMonkeyDevice,By
  3. from com.android.chimpchat.hierarchyviewer import HierarchyViewer
  4. from com.android.hierarchyviewerlib.models import ViewNode, Window
  5. from java.awt import Point
  6.  
  7. #from com.android.hierarchyviewerlib.device import
  8.  
  9. #Connect to the target targetDevice
  10. targetDevice = MonkeyRunner.waitForConnection()
  11.  
  12. easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need this
  13. targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList")
  14.  
  15. #invoke the menu options
  16. MonkeyRunner.sleep(6)
  17. #targetDevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP);
  18.  
  19. '''
  20. public ViewNode findViewById(String id)
  21. * @param id id for the view.
  22. * @return view with the specified ID, or {@code null} if no view found.
  23. '''
  24. #MonkeyRunner.alert("Continue?", "help", "Ok?")
  25.  
  26. pic = targetDevice.takeSnapshot()
  27. pic = pic.getSubImage((0,38,480,762))
  28.  
  29. newPic = targetDevice.takeSnapshot()
  30. newPic = newPic.getSubImage((0,38,480,762))
  31.  
  32. print (newPic.sameAs(pic,1.0))
  33. 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. 有关Struts2a的ction直接使用response异步问题

    假设我们在项目中使用struts2,正在使用ajax而通信时后端程序.为简单起见,我们经常使用下面的方法:         ActionContext ac = ActionContext.getCo ...

  2. Android供TextView添加多个点击文字

    我们使用社会性软件的过程中会或多或少像别人的帖子点,图. : 能够看到用户页面显示出来的仅仅是点了赞的用户的名称,点击这些名称能够进入到该用户的主页.我们就来实现相似的效果.直接上代码吧. @Over ...

  3. FPGA合成编码

    1 决策树 于FPGA推断使用if else报表及case达到. a) if else 是有特权的,类似于优先编码(当两个条件同一时候成立,仅推断条件靠前的成立),所以当有特权条件时应该採用if el ...

  4. Android虚拟机器学习总结Dalvik虚拟机创建进程和线程分析

    Dalvik调用一个成员函数时,虚拟机,假设发现,该成员函数是一个JNI办法,然后,它会直接跳转到其地址来运行.也就是说.JNI方法是直接在本地操作系统上运行的.而不是由Dalvik虚拟机解释器运行. ...

  5. [Unity3D]Unity3D游戏开发3D选择场景中的对象,并显示轮廓效果强化版

    大家好,我是秦培,欢迎关注我的博客,我的博客地址blog.csdn.net/qinyuanpei. 在上一篇文章中,我们通过自己定义着色器实现了一个简单的在3D游戏中选取.显示物体轮廓的实例. 在文章 ...

  6. 用于主题检测的临时日志(18506589-369d-4505-a204-3678db17eae5 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(252f1b1e-5ce3-42a8-95da-bc0acbd4f637 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  7. SQL开发中容易忽视的一些小地方(一)

    原文:SQL开发中容易忽视的一些小地方(一) 写此系列文章缘由: 做开发三年来(B/S),发现基于web 架构的项目技术主要分两大方面: 第一:C#,它是程序的基础,也可是其它开发语言,没有开发语言也 ...

  8. Log4NET 数据库

    阅读目录 Log4NET简介 前提 详细步骤 回到顶部 Log4NET简介 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种 ...

  9. Doug Lea

    如果IT的历史,是以人为主体串接起来的话,那么肯定少不了Doug Lea.这个鼻梁挂着眼镜,留着德王威廉二世的胡子,脸上永远挂着谦逊腼腆笑容,服务于纽约州立大学Oswego分校计算机科学系的老大爷. ...

  10. springmvc集成Ueditor插件实现图片上传2、

    一.下载Ueditor插件. 地址:http://ueditor.baidu.com/website/download.html 二.环境搭建. 具体可以参看http://fex.baidu.com/ ...