uiautomator--图像处理
一、图像处理在自动化中使用场景
二、BitMap图像处理
1.部分API简单说明
BitMap给我们提供很多图片的处理方法。
| API | 说明 |
| compress | 压缩图片 |
| copy | 复制图片 |
| createBitmap | 创建图片 |
| getHeight | 获取图片高度 |
| getWidth | 获取图片宽度 |
| getPixel | 获取某个点颜色值 |
| setPixel | 设置某个点颜色值 |
三、图像处理实例
package com.yoyo.testsuites; import java.lang.Exception; import java.io.File;
import java.io.FileOutputStream; 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; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect; public class bitmapBase extends UiAutomatorTestCase {
//快速调试
public static void main(String[] args) {
String jarName = "TestYoyo";
String testClass = "com.yoyo.testsuites.bitmapBase";
String testName = "runTest";
String androidId = "5";
new UiAutomatorHelper(jarName, testClass, testName, androidId);
} UiDevice device = UiDevice.getInstance(); //用例
public void runTest() throws UiObjectNotFoundException { //创建bitmap
crateBitmap("bitmap"); //截取组件图片
UiObject object=new UiObject(new UiSelector().resourceId("dianyun.baobaowd:id/checkin_iv"));
cutImg(object,"screenshot","checkinImg"); //获取截图中(100,200)坐标位置的颜色值
int colorValue=getColor(100,200,"ColorImg");
System.out.println("该坐标点的颜色值为="+colorValue); //截图并嵌入文字
scrennshotAndDrawnText("screenshot","textImg", "截图并嵌入文字"); //截取组件,与保存的组件原图进行对比
UiObject object2 =new UiObject(new UiSelector().resourceId("dianyun.baobaowd:id/checkin_iv"));
cutImg(object2, "screenshot", "moduleImg");//截取组件图
String targetImgPath="mnt/sdcard/yoyoTargetImg/checkImg.jpg";//已保存将用来做对比的截图
String comPath="mnt/sdcard/yoyoTest/moduleImg.jpg";
double percent=0.9;
//调用图像对比方法
boolean b=imgSameAs(targetImgPath,comPath,percent);
//输出对比结果
System.out.println("图像比对结果:"+b);
} /*-------------------图像处理方法---------------------*/ //创建bitmap
public void crateBitmap(String ImgName) {
//截取一张图片
String path="mnt/sdcard/yoyoTest/takescreenshot.jpg";
File file=new File(path);
device.takeScreenshot(file);
sleep(1000);
//将图片重命名保存
//先将截图通过BitmapFactory(bitmap工厂模式)创建为bitmap,然后将bitmap压缩保存
Bitmap bitmap=BitmapFactory.decodeFile(path);
saveBitMapToSdcard(bitmap,ImgName);
} //压缩保存bitmap图
public void saveBitMapToSdcard(Bitmap bitmap,String ImgName){
FileOutputStream out=null;
try {
out=new FileOutputStream("/mnt/sdcard/yoyoTest/"+ImgName+".jpg");
if(out!=null){
//三个参数分别为格式、保存的文件质量90为原图的90%、文件流
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} //组件区域截图
//object:组件的object
public void cutImg(Object object,String screenshotName,String moduleImgName) throws UiObjectNotFoundException {
//截取图片并保存至path
String path="/mnt/sdcard/yoyoTest/"+screenshotName+".png";
File file=new File(path);
UiDevice.getInstance().takeScreenshot(file);
//根据path路径找到截图,截取组件图
Bitmap m=BitmapFactory.decodeFile(path);
Rect rect = ((UiObject) object).getBounds();
m=Bitmap.createBitmap(m,rect.left,rect.top,rect.width(),rect.height());
//保存组件截图
saveBitMapToSdcard(m, moduleImgName);
} //获取某一点的颜色值
public int getColor(int x,int y,String ImgName) {
String path="/mnt/sdcard/yoyoTest/"+ImgName+".jpg";
File file =new File(path);
device.takeScreenshot(file);
Bitmap bitmap=BitmapFactory.decodeFile(path);
int color=bitmap.getPixel(x,y);
System.out.println(color);
return color;
} //图像嵌入文字
public void scrennshotAndDrawnText(String screenshotName,String textImgName,String text) {
String path="/mnt/sdcard/yoyoTest/"+screenshotName+".jpg";
File file =new File(path);
device.takeScreenshot(file);
Bitmap bitmap=BitmapFactory.decodeFile(path);
Bitmap drawBitmap=drawTextBitmap(bitmap,text);
saveBitMapToSdcard(drawBitmap, textImgName);//调用嵌入文字方法
} //嵌入文字方法
private Bitmap drawTextBitmap(Bitmap bitmap, String text) {
int x=bitmap.getWidth();
int y=bitmap.getHeight();
///创建一个更大的位图,Config.ARGB_8888为创建的位图的信息,32位
Bitmap newBitmap=Bitmap.createBitmap(x,y+80,Bitmap.Config.ARGB_8888);
//创建画布
Canvas canvans=new Canvas(newBitmap);
//创建画笔
Paint paint=new Paint();
//在原图位置(0,0)叠加一张图片
canvans.drawBitmap(bitmap, 0, 0,paint);
//画笔颜色
paint.setColor(Color.parseColor("#FF0000"));
paint.setTextSize(80);//设置文字大小
canvans.drawText(text, 300, y+55, paint);//写字
canvans.save(Canvas.ALL_SAVE_FLAG);//保存
canvans.restore();
return newBitmap;
} //图像对比
public boolean imgSameAs(String targetImgPath,String comPath,double percent) {
try {
//创建两个bitmap
Bitmap targetImg=BitmapFactory.decodeFile(targetImgPath);
Bitmap compareImg=BitmapFactory.decodeFile(comPath);
//声明变量
int width=compareImg.getWidth();
int height=compareImg.getHeight();
int numDiffPixels=0;//两张图片像素差
for (int y= 0; y< height ;y++) {
for(int x=0;x<width;x++){
//取不相等的像素值
if (compareImg.getPixel(x, y)!=targetImg.getPixel(x, y)) {
numDiffPixels++;
}
}
}
double totalPixels=width*height;//像素值总量=width*height
double diffPercent=numDiffPixels/totalPixels;//差异度百分比=不等像素值/像素值总量
System.out.println(1.0-diffPercent);//相似度百分比=1.0-差异度百分比
return percent<=1.0-diffPercent; } catch (Exception e) {
} return false;
} }
uiautomator--图像处理的更多相关文章
- 14.Android UiAutomator 图像处理
一.BitMap介绍 1.图像使用场景 1)效果类截图 2)不可见的组件图像对比 3)失败与异常截图 4)利用图像判断组件 2.部分API简单说明 API 说明 compress 压缩图片 copy ...
- Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉
Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉 1.1. 按照当前流行的分类方法,可以分为以下三部分:三部分 图像处理 图像分析 计算机视觉1 1.2. 图像处理需要 ...
- Atitit 图像处理的摩西五经attilax总结
Atitit 图像处理的摩西五经attilax总结 1. 数字图像处理(第三版)1 2. 图像处理基础(第2版)(世界著名计算机教材精选)1 3. 计算机视觉特征提取与图像处理(第三版)2 4. Op ...
- Atitit 图像处理的心得与疑惑 attilax总结
Atitit 图像处理的心得与疑惑 attilax总结 1.1. 使用类库好不好??还是自己实现算法1 1.2. 但是,如果遇到类库体积太大,后者没有合适的算法,那就只能自己开发算法了1 1.3. 如 ...
- Atitit 图像处理 调用opencv 通过java api attilax总结
Atitit 图像处理 调用opencv 通过java api attilax总结 1.1. Opencv java api的支持 opencv2.4.2 就有了对java api的支持1 1. ...
- Atitit MATLAB 图像处理 经典书籍attilax总结
Atitit MATLAB 图像处理 经典书籍attilax总结 1.1. MATLAB数字图像处理1 1.2. <MATLAB实用教程(第二版)>((美)穆尔 著)[简介_书评_在线阅读 ...
- Atitit 图像处理类库大总结attilax qc20
Atitit 图像处理类库大总结attilax qc20 1.1. 选择与组合不同的图像处理类库1 1.2. Halcon 貌似商业工具,功能强大.1 1.3. Openvc Openvc功能也是比 ...
- Atitit MATLAB 图像处理attilax总结
Atitit MATLAB 图像处理attilax总结 1.1. 下载 Matlab7.0官方下载_Matlab2012 v7.0 官方简体中文版-办公软件-系统大全.html1 1.2. Matla ...
- 使用MATLAB对图像处理的几种方法(下)
试验报告 一.试验原理: 图像点处理是图像处理系列的基础,主要用于让我们熟悉Matlab图像处理的编程环境.灰度线性变换和灰度拉伸是对像素灰度值的变换操作,直方图是对像素灰度值的统计,直方图均衡是对 ...
- 使用MATLAB对图像处理的几种方法(上)
实验一图像的滤波处理 一.实验目的 使用MATLAB处理图像,掌握均值滤波器和加权均值滤波器的使用,对比两种滤波器对图像处理结果及系统自带函数和自定义函数性能的比较,体会不同大小的掩模对图像细节的影响 ...
随机推荐
- python3----练习题(冒泡排序)
冒泡,原理是临近的数字两两进行比较,按照从小到大的顺序进行交换,这样交换一次之后,最大的数字就被交换到了最后一位. li = [33, 2, 10, 1] for j in range(1, len( ...
- Bellman-Ford算法(有向图)
#include <iostream> #include <cstring> #include <cstdio> #define MAX 100 #define I ...
- PANDAS 数据合并与重塑(join/merge篇)
pandas中也常常用到的join 和merge方法 merge pandas的merge方法提供了一种类似于SQL的内存链接操作,官网文档提到它的性能会比其他开源语言的数据操作(例如R)要高效. 和 ...
- cocos3.9 windows平台 AssetsManager创建文件失败问题
在做热更新功能时用到了AssetsManager,发现在windows平台总是报CREATE_FILE错误,errorStr "Can't renamefile from: xxx.tmp ...
- flex组合流动布局实例---利用css的order属性改变盒子排列顺序
flex弹性盒子 <div class="container"> <div class="box yellow"></div> ...
- 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280
POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...
- JS给html控件赋值
<html> <head> <title> JS给html控件赋值 </title> <script language="javascr ...
- Codeblocks自动代码格式化+快捷键
Codeblocks自动代码格式化+快捷键 - u010112268的博客 - CSDN博客 https://blog.csdn.net/u010112268/article/details/8110 ...
- BaseDao 接口
// 以后所有的 Dao 接口都需要继承 BaseDao 接口; // 自定义泛型接口 public interface BaseDao<T>{ public void save(T t) ...
- 2015-03-12——简析DOM2级样式
CSSStyleSheet对象 表示某种类型的样式表CSSStyleRule对象 样式表中的每条规则 获得文档中的所有样式表document.styleSheets CSSStyleSheet对象 ...