溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

移植MonkeyRunner的圖片對比和獲取子圖功能的實現-UiAutomator/Robotium篇

發布時間:2020-07-30 06:37:58 來源:網絡 閱讀:946 作者:zhukev 欄目:移動開發

根據前一篇文章《移植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); 
        

    
    }
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女