1、圖像處理在自動化中使用場景java
2、BitMap圖像處理android
1.部分API簡單說明ui
BitMap給咱們提供不少圖片的處理方法。spa
API | 說明 |
compress | 壓縮圖片 |
copy | 複製圖片 |
createBitmap | 建立圖片 |
getHeight | 獲取圖片高度 |
getWidth | 獲取圖片寬度 |
getPixel | 獲取某個點顏色值 |
setPixel | 設置某個點顏色值 |
3、圖像處理實例調試
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; } }