Uiautomator中获取屏幕坐标/控件颜色RGB值

在实际测试中有时候会遇到一些开关按钮,但是这些按钮的checkable属性为false,很难从控件属性上判定开关状态的时候,我们可以根据开关的当前颜色来判定。

比如:

所以,这就需要获取控件的坐标的颜色值,随后通过颜色值来判定开关状态。

    /**
	 * 获取给定图片的指定坐标点的RGB值,结果以16进制输出(例:ffffff)
	 *
	 * @param picPath 需要获取像素点的图片地址
	 * @param x       像素点的横坐标值
	 * @param y       像素点的纵坐标值
	 * @return 16进制的RGB值,以String类型返回。例:ffffff
	 * @throws AutoException
	 * @author ZeKyll
	 */
	public String getRGB(String picPath, int x, int y) throws AutoException {
		LogTimeGetter.updateTimeString();
		BitmapFactory.Options op = new BitmapFactory.Options();
		op.inPreferredConfig = Bitmap.Config.ARGB_8888;
		Bitmap targetBitmap = BitmapFactory.decodeFile(picPath, op);
		int rgbPixel = targetBitmap.getPixel(x, y);
		// 转换字符
		String r1 = Integer.toHexString(Color.red(rgbPixel));
		String g1 = Integer.toHexString(Color.green(rgbPixel));
		String b1 = Integer.toHexString(Color.blue(rgbPixel));
		String colorStr = r1 + g1 + b1;
//		Log.i("Value--------",colorStr);
		TestReport.i(LOGTAG, "获取图片坐标点(" + "x-" + x + ",y-" + y + ")RGB值:" + colorStr);
		return colorStr;
	}
	/**
	 * 获取当前界面中指定坐标点的RGB值,结果以16进制输出(例:ffffff)
	 * @param x       像素点的横坐标值
	 * @param y       像素点的纵坐标值
	 * @return 16进制的RGB值,以String类型返回。例:ffffff
	 * @throws AutoException
	 * @author ZeKyll
	 */
	public String getRGB(int x, int y) throws AutoException {
		LogTimeGetter.updateTimeString();
		String retRGB = "";
		String imageFileName=screenshotTaker.takeScreenshot("_RGB");
		File file = new File(imageFileName);
		BitmapFactory.Options op = new BitmapFactory.Options();
		op.inPreferredConfig = Bitmap.Config.ARGB_8888;
		Bitmap targetBitmap = BitmapFactory.decodeFile(imageFileName, op);
		int rgbPixel = targetBitmap.getPixel(x, y);
		// 转换字符
		String r1 = Integer.toHexString(Color.red(rgbPixel));
		String g1 = Integer.toHexString(Color.green(rgbPixel));
		String b1 = Integer.toHexString(Color.blue(rgbPixel));
		String colorStr = r1 + g1 + b1;
//		Log.i("Value--------",colorStr);
		TestReport.i(LOGTAG, "获取当前屏幕坐标点(" + "x-" + x + ",y-" + y + ")RGB值:" + colorStr);
		if (file.exists()){
			file.delete();
		}
		return colorStr;
	}



    /***
	 * 截屏后直接保存在savepath下
	 * 
	 * @param savename 截图文件名称
	 * @return 保存的截图文件的绝对路径
	 * @author ZeKyll
	 */
	public String takeScreenshot(String savename)
	{
		savename=df.format(new Date())+savename;
		LogTimeGetter.updateTimeString();
		screenshortDevice.takeScreenshot(new File(savepath+savename+".png"));
		return savepath+savename+".png";
	}

说明:这里获取的颜色值为一张图片中的一个坐标点,所以,需要给一张图片。第一个方法是自动截当前界面的图片,第二个是传递图片地址参数进入方法。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值