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

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

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

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

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

1. 移植代码

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

[java]  view plain copy
  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 plain copy
  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值