移植MonkeyRunner的图片对照和获取子图功能的实现-UiAutomator/Robotium篇
实现 获取 功能 图片 移植 对照
2023-09-11 14:20:45 时间
依据前一篇文章《移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇》所述,由于Appium和MonkeyRunner有一个共同点--代码控制流程都是在client实现的。所以要把MonkeyRunner在PC端实现的图片比对和获取子图功能移植到相同是在PC端执行的Appium是非常easy的事情,可是对于在server端执行的Robotium和UiAutomator就是另外一回事了。
由于在Android的sdk中。MonkeyRunner获取子图和图片比对须要用到的下面两个类是没有支持的。简单来说就是java.awt这个库是不支持的:
import java.awt.image.BufferedImage; import javax.imageio.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 path1, String path2) throws FileNotFoundException { boolean res = false; FileInputStream fis1 = new FileInputStream(path1); Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path2); Bitmap bitmap2 = BitmapFactory.decodeStream(fis2); res = sameAs(bitmap1,bitmap2); return res; } public static boolean sameAs (String path1, String path2,double percent) throws FileNotFoundException { FileInputStream fis1 = new FileInputStream(path1); Bitmap bitmap1 = BitmapFactory.decodeStream(fis1); FileInputStream fis2 = new FileInputStream(path2); 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); } }
作者 |
自主博客 |
微信 |
CSDN |
天地会珠海分舵 |
|
服务号:TechGoGoGo 扫描码:
|
http://blog.csdn.net/zhubaitian |
相关文章
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- c++ template函数的声明和实现需要在同一个文件中
- Node+TS+Koa+vue 商城全栈(八)创建分类控制器 并 实现获取分类信息列表
- 【MATLAB教程案例53】CNN卷积神经网络的MATLAB编程学习和实现,以手势识别为例进行仿真分析
- Vue - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- 用Python发送通知到企业微信,实现消息推送
- Kilim实现浅析(一)
- shell脚本获取进程ID并杀死的实现及问题解析
- 基于Java实现(PC)民航订票管理系统【100010185】
- 基于Java(SSM)+MySQL实现个人博客网站【100010083】
- 基于python实现的DDoS
- SpringSecurity实现用户名密码登录(Token)
- macOS SwiftUI 教程之 实现点击获取具体位置并监测单机双击(教程含源码)
- 微信小程序云开发轻松实现用户openid的获取
- XE 画矩形实现拖拉改变大小(属性)
- 《计算机系统:核心概念及软硬件实现(原书第4版)》——2.7 练习
- 几种获取时间戳与获取自定义时间戳的实现方法
- 高德地图实现地址检索获取结果列表和坐标
- 浅析Vue3相关基础知识:Vue3应用配置、重写的v-model、emits 选项、getCurrentInstance()获取实例、采用mitt实现全局通讯、vue-router的新特性
- 小技巧随手记:一行代码实现星级评分、同步阻塞实现sleep函数、一行代码生成随机字符串(唯一ID/Number.prototype.toString([radix])用法)、获取浏览器Cookie的值、颜色RGB转十六进制、使用数字分隔符、最快获取dom方法(ID同名变量获取dom元素)、带命名空间的事件绑定
- Unity 屏幕特效 之 简单地使用 Shader 获取深度,实现景深效果
- pyautogui模块获取鼠标位置、移动鼠标并实现双击效果
- C#-IO-Path类-path.GetFullPath 获取上级目录实现方法
- SpringBoot获取全局配置文件的属性以及@ConfigurationProperties实现类型安全的配置