本人在用UiAutomator作測試的時候,常常會遇到一些控件由於不一樣的條件顯示不一樣的顏色,在學習了UiAutomator圖像處理以後,本身嘗試寫了一個方法來處理不一樣顏色控件的區分。分享代碼供你們參考。java
//根據顏色判斷狀態 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; }