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

轉載地址:http://blog.csdn.net/zhubaitian/article/details/41039147

根據前一篇文章《移植MonkeyRunner的圖片對比和獲取子圖功能的實現-Appium篇》所述,因爲Appium和MonkeyRunner有一個共同點--代碼控制流程都是在客戶端實現的。所以要把MonkeyRunner在PC端實現的圖片比對和獲取子圖功能移植到同樣是在PC端運行的Appium是很容易的事情,但是對於在服務器端運行的Robotium和UiAutomator就是另外一回事了。

因爲在Android的sdk中,MonkeyRunner獲取子圖和圖片比對需要用到的以下兩個類是沒有支持的,簡單來說就是java.awt這個庫是不支持的:

[java] view plaincopy
  1. import java.awt.image.BufferedImage;  
  2. import javax.imageio.ImageIO;  
但是在Android的sdk中有Bitmap這個類來幫助我們完成類似的功能,同時這個類還提供了一個sameAs的方法來比對兩個Bitmap是否一致,但是遺憾的是它沒有像MonkeyRunner一樣提供一個百分比來指明兩個圖片的差異接受程度,所以爲了兼容多種情況,我們需要對sameAs方法提供多個重載方法。

當然,這只是驗證代碼,有bug的話自己調吧

1. 移植代碼

注意一下代碼只在UiAutomator上面測試通過,但是我相信Robotium是一樣的,因爲他們都是運行在目標安卓機器上面的,大家可以自行驗證下。

[java] view plaincopy
  1. package libs;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7.   
  8. public class Util {  
  9.       
  10.     public static boolean sameAs (String path1, String path2) throws FileNotFoundException {  
  11.         boolean res = false;  
  12.         FileInputStream fis1 = new FileInputStream(path1);  
  13.         Bitmap bitmap1  = BitmapFactory.decodeStream(fis1);  
  14.           
  15.         FileInputStream fis2 = new FileInputStream(path2);  
  16.         Bitmap bitmap2  = BitmapFactory.decodeStream(fis2);  
  17.           
  18.         res = sameAs(bitmap1,bitmap2);  
  19.       
  20.         return res;  
  21.               
  22.     }  
  23.       
  24.     public static boolean sameAs (String path1, String path2,double percent) throws FileNotFoundException {  
  25.         FileInputStream fis1 = new FileInputStream(path1);  
  26.         Bitmap bitmap1  = BitmapFactory.decodeStream(fis1);  
  27.           
  28.         FileInputStream fis2 = new FileInputStream(path2);  
  29.         Bitmap bitmap2  = BitmapFactory.decodeStream(fis2);  
  30.           
  31.         return sameAs(bitmap1,bitmap2,percent);  
  32.               
  33.     }  
  34.       
  35.     public static boolean sameAs (Bitmap bitmap1, Bitmap bitmap2, double percent) {  
  36.         if(bitmap1.getHeight() != bitmap2.getHeight())  
  37.             return false;  
  38.           
  39.         if(bitmap1.getWidth() != bitmap2.getWidth())  
  40.             return false;  
  41.           
  42.         if(bitmap1.getConfig() != bitmap2.getConfig())  
  43.             return false;  
  44.   
  45.         int width = bitmap1.getWidth();  
  46.         int height = bitmap2.getHeight();  
  47.   
  48.         int numDiffPixels = 0;  
  49.            
  50.          for (int y = 0; y < height; y++) {  
  51.            for (int x = 0; x < width; x++) {  
  52.              if (bitmap1.getPixel(x, y) != bitmap2.getPixel(x, y)) {  
  53.                numDiffPixels++;  
  54.              }  
  55.            }  
  56.          }  
  57.          double numberPixels = height * width;  
  58.          double diffPercent = numDiffPixels / numberPixels;  
  59.          return percent <= 1.0D - diffPercent;  
  60.     }  
  61.       
  62.     public static boolean sameAs (Bitmap bmp1, Bitmap bmp2) throws FileNotFoundException {  
  63.         boolean res = false;  
  64.           
  65.         res = bmp1.sameAs(bmp2);  
  66.           
  67.         return res;       
  68.     }  
  69.       
  70.     public static Bitmap getSubImage(String path,int x,int y,int width,int height) throws FileNotFoundException {  
  71.           
  72.         FileInputStream fis = new FileInputStream(path);  
  73.         Bitmap bitmap  = BitmapFactory.decodeStream(fis);  
  74.                   
  75.         Bitmap res = Bitmap.createBitmap(bitmap, x, y, width, height);  
  76.           
  77.         return res;  
  78.           
  79.     }  
  80. }  

2. 調用代碼示例

以下是UiAutomator示例,Robotium的示例請大家自行實現.

[java] view plaincopy
  1. package sample.demo;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import libs.Util;  
  6. import android.graphics.Bitmap;  
  7.   
  8. import com.android.uiautomator.core.UiDevice;  
  9. import com.android.uiautomator.core.UiObject;  
  10. import com.android.uiautomator.core.UiObjectNotFoundException;  
  11. import com.android.uiautomator.core.UiSelector;  
  12. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  13.   
  14. public class CompareScreenshots extends UiAutomatorTestCase   {  
  15.       
  16.     public void testCompareScreenshotsNSubScrenshots() throws UiObjectNotFoundException, IOException, InterruptedException {  
  17.         UiDevice device = getUiDevice();  
  18.         //device.pressHome();  
  19.         UiObject appNotes = new UiObject(new UiSelector().text("Notes"));  
  20.         appNotes.click();  
  21.         Thread.sleep(3000);  
  22.           
  23.         String p1 = "/data/local/tmp/1.bmp";  
  24.         String p2 = "/data/local/tmp/2.bmp";  
  25.         File f1 = new File(p1);  
  26.         if(f1.exists())  
  27.             f1.delete();  
  28.           
  29.         File f2 = new File(p2);  
  30.         if(f2.exists())  
  31.             f2.delete();  
  32.           
  33.         device.takeScreenshot(f1);  
  34.         device.takeScreenshot(f2);  
  35.           
  36.         Bitmap sub1 = Util.getSubImage(p1, 63947438);  
  37.         Bitmap sub2 = Util.getSubImage(p2, 63947438);  
  38.           
  39.         boolean same = Util.sameAs(sub1, sub2, 1.0);  
  40.         assertTrue(same);  
  41.           
  42.         same = Util.sameAs(p1, p2, 0.9);  
  43.         assertTrue(same);   
  44.           
  45.   
  46.       
  47.     }  
  48. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章