14.Android UiAutomator 影像處理

來源:互聯網
上載者:User

標籤:

一、BitMap介紹1.映像使用情境

1)效果類
2)不可見的元件圖表像對比
3)失敗與異常
4)利用映像判斷組件

2.部分API簡單說明

API

說明

compress 壓縮圖片
copy 複製圖片
createBitmap 建立圖片
getHeight 擷取圖片高度
getWidth 擷取圖片寬度
getPixel 擷取某個點顏色值
setPixel 設定某個點顏色值

3.建立bitmap執行個體

//方法體代碼public class ImageTestCase extends UiAutomatorTestCase{    public void saveBitMapToSdcard(Bitmap bitmap,String newName){        FileOutputStream out=null;        try {            out=new FileOutputStream("/mnt/sdcard/"+newName+".jpg");            if(out!=null){                //三個參數分別為格式、儲存的檔案品質、檔案流                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);                out.close();            }        } catch (Exception e) {            throw new RuntimeException(e);        }    }}//用例部分代碼public class test1 extends ImageTestCase{    //快速調試    public static void main(String [] args){        new UiAutomatorHelper("test","testDemo1.test1","testDemo1","2");    }    //建立一個Bitmap    public void testDemo1(){        //a.截取一張圖片        String Path="/mnt/sdcard/testBitMap.png";        File storePath=new File(Path);        UiDevice.getInstance().takeScreenshot(storePath);        sleep(1000);        //b.將圖片重新命名並儲存        //從檔案中建立bitmap        Bitmap bitmap=BitmapFactory.decodeFile(Path);        //調用方法體        saveBitMapToSdcard(bitmap,"new-Image-88");      }}
二、擷取像素值與映像截取

像素值:每一個像素點的顏色值

1.擷取某點像素值執行個體
//方法體代碼public class ImageTestCase extends UiAutomatorTestCase{    //根據描述擷取組件    public UiObject obj(String text){        return new UiObject(new UiSelector().description(text));    }    //截取一張圖片後另存    public void cutBitmap(Rect rect,String path){        Bitmap m=BitmapFactory.decodeFile(path);        m=m.createBitmap(m,rect.left,rect.top,rect.width(),rect.height());        //調用上面例子中的那個方法,實際調試過程中如果需要就把那個方法體加上        saveBitMapToSdcard(m, "cutImg_88");    }    //擷取某點的顏色值    public int getColorPicel(int x,int y){        String path="/mnt/sdcard/testcolor.png";        File file=new File(path);        UiDevice.getInstance().takeScreenshot(file);        Bitmap m=BitmapFactory.decodeFile(path);        int color=m.getPixel(x, y);        return color;    }}//用例代碼public class test1 extends ImageTestCase{    //快速調試    public static void main(String [] args){        new UiAutomatorHelper("test","testDemo1.test1","testDemo2","2");    }    //用例    public void testDemo2() throws UiObjectNotFoundException{        //截取圖片        Rect rect = obj("城市").getBounds();        String path="/mnt/sdcard/testcolor.png";        File file=new File(path);        UiDevice.getInstance().takeScreenshot(file);        //調用方法體        cutBitmap(rect,path);        //調用方法體擷取某個點的顏色值        int color=getColorPicel(rect.centerX(),rect.centerY());        System.out.println("COLOR:"+color);    }}
三、映像嵌入文字

的時候希望把用例情境用文字寫在映像上,便於快速查看

1.映像嵌入文字執行個體:
//方法體public class ImageTestCase extends UiAutomatorTestCase{    //方法    public void screenshotAndDrawRext(String path,String imageName,String text){        File file=new File(path);        UiDevice.getInstance().takeScreenshot(file);        Bitmap bitmap=BitmapFactory.decodeFile(path);        Bitmap drawBitmap=drawTextBitmap(bitmap,text);        saveBitMapToSdcard(drawBitmap, imageName);//調用前面第一個例子中的方法    }    //嵌入文字方法    public Bitmap drawTextBitmap(Bitmap bitmap,String text){        int x=bitmap.getWidth();        int y=bitmap.getHeight();        //建立一個更大的位元影像        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 class test1 extends ImageTestCase{    //快速調試    public static void main(String [] args){        new UiAutomatorHelper("test","testDemo1.test1","testDemo3","2");    }    public void testDemo3(){        String path="/mnt/sdcard/testDrawText.png";        String imageName="testDrawText_888";        String text="測試輸入";        //調用方法體        screenshotAndDrawRext(path, imageName, text);       }       }
四、映像對比

某些特殊組件無法擷取到組件資訊,無法判斷狀態

1.映像對比執行個體
//方法體public class ImageTestCase extends UiAutomatorTestCase{    //映像對比的方法    public boolean imageSameAs(String targetImagePath,String comPath,double percent){        try {            //建立兩個bitmap            Bitmap m1=BitmapFactory.decodeFile(targetImagePath);            Bitmap m2=BitmapFactory.decodeFile(comPath);            //聲明變數            int width=m2.getWidth();            int height=m2.getHeight();            int numDiffPixels=0;            //橫縱對比,涉及到兩個迴圈            for(int y=0;y<height;y++){                for(int x=0;x<width;x++){                    //取不相等的像素值                    if(m2.getPixel(x, y)!=m1.getPixel(x, y)){                        numDiffPixels++;                    }                }            }            double totalPices=height*width;//總像素值            double diffPercent=numDiffPixels/totalPices;//不相等的百分比            return percent<=1.0-diffPercent;//返回相似性        } catch (Exception e) {        }           return false;    }}//圖片對比用例部分public class test1 extends ImageTestCase{    //快速調試    public static void main(String [] args){        new UiAutomatorHelper("test","testDemo1.test1","testDemo4","2");    }    //圖片對比    public void testDemo4(){        //截取兩張對比圖        String targetImagePath="/mnt/sdcard/c1.png";        String comPath="/mnt/sdcard/c2.png";        File f1=new File(targetImagePath);        File f2=new File(comPath);        UiDevice.getInstance().takeScreenshot(f1);        sleep(1000);        UiDevice.getInstance().pressHome();//換個情境        sleep(1000);        UiDevice.getInstance().takeScreenshot(f2);        //調用映像對比方法        boolean b=imageSameAs(targetImagePath,comPath,1.0d);        //輸出對比結果        System.out.println("映像比對結果:"+b);    }}

14.Android UiAutomator 影像處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.