zoukankan      html  css  js  c++  java
  • MonkeyImage API 实践全记录

    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 Optionsbutton弹出“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




  • 相关阅读:
    算法25-----位运算(2)-----案例
    算法24----重复子字符串
    算法23-------岛屿的最大面积 LeetCode 695
    算法22-----托普利茨矩阵leetcode766
    算法21----重塑矩阵 LeetCode566
    Python笔记25-----------创建二维列表【浅copy】和转置
    TensorFlow+实战Google深度学习框架学习笔记(7)-----队列与多线程
    TensorFlow+实战Google深度学习框架学习笔记(6)----神经网络经典损失函数
    Testing
    Spark简介
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5065599.html
Copyright © 2011-2022 走看看