Appium appium 自动截图对比功能

周小丽 · 2016年03月21日 · 最后由 yanbin 回复于 2019年11月26日 · 3164 次阅读

最近看了些别人写的代码,进行整合了下,主要是通过 appium 进行自动截图对比。

自动截图,并将图片保存至本地

public static void takeScreenShot1(AndroidDriver driver){  
   try {   
       String StrJ1 = "click";
       String StrJ2 = "swipe";
       if((StrJ1.equals(mstrHI))||(StrJ2.equals(mstrHI))){
           String p1="E:\\AutoScreenCapture1\\" + k  + "_"+ mstrHI + "_" + P1 +".jpg";
           File f1=driver.getScreenshotAs(OutputType.FILE);
           FileUtils.copyFile(f1,new File(p1)); 
           F1=new File(p1);
         //  System.out.println(F1);        
       }           
   }       
   catch (IOException e) {e.printStackTrace();}  
} 

获取本地已保存的文件

public static BufferedImage getImageFromFile(File f){
    BufferedImage img = null;   
    try {
        img = javax.imageio.ImageIO.read(f);
    }   
    catch (IOException e) {
        e.printStackTrace();
    }
    return img;
 }

进行图片对比

public static boolean sameAs(BufferedImage myImage,BufferedImage otherImage, double percent){
    if (otherImage.getWidth() != myImage.getWidth()) {  
       return false;    
    }
    if (otherImage.getHeight() != myImage.getHeight()) {
       return false;
    }
    int width = myImage.getWidth(); 
    int height = myImage.getHeight();   
    int numDiffPixels = 0;  
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
             if (myImage.getRGB(x, y) != otherImage.getRGB(x, y)) {
             numDiffPixels++;
             }
        }
    }   
    double numberPixels = height * width;   
    double diffPercent = numDiffPixels / numberPixels;  
    return percent <= 1.0D - diffPercent;
 }

获取图片对比结果

private static String getResult(AndroidDriver driver){
    Boolean same;
    String result=null;
    String StrJ1 = "click";
    String StrJ2 = "swipe";
    if((StrJ1.equals(mstrHI))||(StrJ2.equals(mstrHI))){
        try{
            BufferedImage img1=getImageFromFile(F1);
            BufferedImage img2=getImageFromFile(F2);
            same=sameAs(img1,img2,0.0);
            result = String.valueOf(same);

        }
        catch (Exception e) {e.printStackTrace();}  
    }
    return result;    
 }
共收到 19 条回复 时间 点赞

我也看过类似的文章,像素对比那块核心方法是从 monkeyrunner 源码抄的,哈哈

最后的结果是个相似度吗?

像素对比靠谱吗?

#1 楼 @cjtcwyk 哈哈,确实是

有用,3Q.

  • 贴个收录于论坛的 python 的图片相似度对比 ``` from PIL import Image import math import operator def pil_image_similarity(filepath1, filepath2):
    image1 = Image.open(filepath1) image2 = Image.open(filepath2) h1 = image1.histogram() h2 = image2.histogram() rms = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) ) return rms

print(pil_image_similarity('/Users/apple/Desktop/git/Vimi_API_Test/Compare_image_test/output.jpg','/Users/apple/Desktop/git/Vimi_API_Test/Compare_image_test/0.jpg')

* 100%相似结果就是0,数字越大说明相差的越大。

建议在截图的时候加入区域截图,对截取后的图片进行比对,因为很多设备最上面时间,WIFI 信号,电量都会变化的

#8 楼 @yzx200712256 ,对,有道理 谢谢

之前做过像素对比感觉不太稳定,后来尝试过感知哈希算法,最后的相似度还是很难把握,不知道楼主做这种图片比对多久了,效果如何。

#10 楼 @spider 还没实际应用,只是目前测试效果还是可以的

推荐用 sikuli,是基于 opencv 的,这种比对局限性太大

Vincent [該主題已被刪除] 中提及了此贴 07月06日 17:49

@hjhjhghghg 我在脚本中用 sikuli 进行图片对比,差异较大的图片可以分辨出来,差异较小就不行了。有解决办法吗?

@xiaoli 我是初学呢,你代码里面的 mstrHI 是什么,k 和 p1 又是什么啊,不太懂,能不能把你的案例分析一下啊,谢谢

#14 楼 @superbaby11 相似度百分比可以调整的

#16 楼 @hjhjhghghg 我用的 robotframework 的一个测试库 sikuliLibrary,没有提供相似度的参数。

僅樓主可見

Appium 里面有个 getImagesSimilarity 利用 openCV 里面 template 比较方法 可以输出两个图片最大匹配数值, 这个就可以直接用来做图像对比了

The internally used OpenCV matchTemplate() feature returns a statistical average about the equality of all pixels in the image compared to the image searched in (capture of Screen/Region in our case).
sikuli 同样是利用 OpenCV matchTemplate() 的方法来实现的 ,具体里面选择的算法是不是相同就需要自己去看了

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册