本人在用UiAutomator做测试的时候,经常会遇到一些控件因为不同的条件显示不同的颜色,在学习了UiAutomator图像处理之后,自己尝试写了一个方法来处理不同颜色控件的区分。分享代码供大家参考。
//根据颜色判断状态
public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException {
screenShot("test");//截图
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
Rect rect = uiObject.getVisibleBounds();
int x = rect.left;
int xx = rect.right;
int y = rect.top;
int yy = rect.bottom;
List<Integer> blueColor = new ArrayList<Integer>();
for (int i = x; i < xx; i++) {
for (int k = y;k < yy;k++) {
int color = bitmap.getPixel(i, k);//获取坐标点像素颜色
int red = Color.blue(color);
blueColor.add(red);
}
}
int sum = 0;
for (int i = 0;i<blueColor.size();i++) {
sum += blueColor.get(i);
}
// output(sum/blueColor.size());
return sum/blueColor.size() > 200?true:false;
}
下面是在选择判定值的过程中快速获取某点颜色值的方法:
public int getRedPixel(int x, int y) {
screenShot("test");//截图
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
// output(color);//输出颜色值
int red = Color.red(color);
return red;
}
public int getGreenPixel(int x, int y) {
screenShot("test");//截图
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
// output(color);//输出颜色值
int green = Color.green(color);
return green;
}
public int getBluePixel(int x, int y) {
screenShot("test");//截图
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象
int color = bitmap.getPixel(x, y);//获取坐标点像素颜色
// output(color);//输出颜色值
int blue = Color.blue(color);
return blue;
}
public int[] getRGBcolorPixel(int x, int y) {
screenShot("testDemo");
String path = "/mnt/sdcard/123/testDemo.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
int color = bitmap.getPixel(x, y);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int[] rgb = {red, green, blue};
return rgb;
}
技术类文章精选
- java一行代码打印心形
- Linux性能监控软件netdata中文汉化版
- 接口测试代码覆盖率(jacoco)方案分享
- 性能测试框架
- 如何在Linux命令行界面愉快进行性能测试
- 图解HTTP脑图
- 如何测试概率型业务接口
- httpclient处理多用户同时在线
- 将swagger文档自动变成测试代码
- 五行代码构建静态博客
- httpclient如何处理302重定向
- 基于java的直线型接口测试框架初探
- Tcloud 云测平台--集大成者
非技术文章精选
- 为什么选择软件测试作为职业道路?
- 成为杰出Java开发人员的10个步骤
- 写给所有人的编程思维
- 自动化测试的障碍
- 自动化测试的问题所在
- 测试之《代码不朽》脑图
- 成为优秀自动化测试工程师的7个步骤
- 优秀软件开发人员的态度