资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇

根据前一篇文章《移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇》 所述,因为Appium和MonkeyRunner有一个共同点--代码控制流程都是在客户端实现的。所以要把MonkeyRunner在PC端实现的图 片比对和获取子图功能移植到同样是在PC端运行的Appium是很容易的事情,但是对于在服务器端运行的Robotium和UiAutomator就是另 外一回事了。

在吉安等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站制作 网站设计制作按需网站开发,公司网站建设,企业网站建设,品牌网站设计,营销型网站,外贸营销网站建设,吉安网站建设费用合理。

因为在Android的sdk中,MonkeyRunner获取子图和图片比对需要用到的以下两个类是没有支持的,简单来说就是java.awt这个库是不支持的:

import java.awt.p_w_picpath.BufferedImage;
import javax.p_w_picpathio.ImageIO;

但是在Android的sdk中有Bitmap这个类来帮助我们完成类似的功能,同时这个类还提供了一个sameAs的方法来比对两个Bitmap是否一 致,但是遗憾的是它没有像MonkeyRunner一样提供一个百分比来指明两个图片的差异接受程度,所以为了兼容多种情况,我们需要对sameAs方法 提供多个重载方法。

当然,这只是验证代码,有bug的话自己调吧。

1. 移植代码

注意一下代码只在UiAutomator上面测试通过,但是我相信Robotium是一样的,因为他们都是运行在目标安卓机器上面的,大家可以自行验证下。

package libs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Util {
    
    public static boolean sameAs (String path2, String path3) throws FileNotFoundException {
        boolean res = false;
        FileInputStream fis1 = new FileInputStream(path2);
        Bitmap bitmap1  = BitmapFactory.decodeStream(fis1);
        
        FileInputStream fis2 = new FileInputStream(path3);
        Bitmap bitmap2  = BitmapFactory.decodeStream(fis2);
        
        res = sameAs(bitmap1,bitmap2);
    
        return res;
            
    }
    
    public static boolean sameAs (String path2, String path3,double percent) throws FileNotFoundException {
        FileInputStream fis1 = new FileInputStream(path2);
        Bitmap bitmap1  = BitmapFactory.decodeStream(fis1);
        
        FileInputStream fis2 = new FileInputStream(path3);
        Bitmap bitmap2  = BitmapFactory.decodeStream(fis2);
        
        return sameAs(bitmap1,bitmap2,percent);
            
    }
    
    public static boolean sameAs (Bitmap bitmap1, Bitmap bitmap2, double percent) {
        if(bitmap1.getHeight() != bitmap2.getHeight())
            return false;
        
        if(bitmap1.getWidth() != bitmap2.getWidth())
            return false;
        
        if(bitmap1.getConfig() != bitmap2.getConfig())
            return false;

        int width = bitmap1.getWidth();
        int height = bitmap2.getHeight();

        int numDiffPixels = 0;
         
         for (int y = 0; y < height; y++) {
           for (int x = 0; x < width; x++) {
             if (bitmap1.getPixel(x, y) != bitmap2.getPixel(x, y)) {
               numDiffPixels++;
             }
           }
         }
         double numberPixels = height * width;
         double diffPercent = numDiffPixels / numberPixels;
         return percent <= 1.0D - diffPercent;
    }
    
    public static boolean sameAs (Bitmap bmp1, Bitmap bmp2) throws FileNotFoundException {
        boolean res = false;
        
        res = bmp1.sameAs(bmp2);
        
        return res;        
    }
    
    public static Bitmap getSubImage(String path,int x,int y,int width,int height) throws FileNotFoundException {
        
        FileInputStream fis = new FileInputStream(path);
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
                
        Bitmap res = Bitmap.createBitmap(bitmap, x, y, width, height);
        
        return res;
        
    }
}

2. 调用代码示例

以下是UiAutomator示例,Robotium的示例请大家自行实现.

package sample.demo;

import java.io.File;
import java.io.IOException;
import libs.Util;
import android.graphics.Bitmap;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class CompareScreenshots extends UiAutomatorTestCase   {
    
    public void testCompareScreenshotsNSubScrenshots() throws UiObjectNotFoundException, IOException, InterruptedException {
        UiDevice device = getUiDevice();
        //device.pressHome();
        UiObject appNotes = new UiObject(new UiSelector().text("Notes"));
        appNotes.click();
        Thread.sleep(3000);
        
        String p1 = "/data/local/tmp/1.bmp";
        String p2 = "/data/local/tmp/2.bmp";
        File f1 = new File(p1);
        if(f1.exists())
            f1.delete();
        
        File f2 = new File(p2);
        if(f2.exists())
            f2.delete();
        
        device.takeScreenshot(f1);
        device.takeScreenshot(f2);
        
        Bitmap sub1 = Util.getSubImage(p1, 6, 39, 474, 38);
        Bitmap sub2 = Util.getSubImage(p2, 6, 39, 474, 38);
        
        boolean same = Util.sameAs(sub1, sub2, 1.0);
        assertTrue(same);
        
        same = Util.sameAs(p1, p2, 0.9);
        assertTrue(same); 
        

    
    }
}

网页题目:移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇
网站路径:http://cdkjz.cn/article/jodjhh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220