我需要在我的安卓手机屏幕上获取一个特定点的颜色信息。
有没有办法通过ADB来实现?
我现在使用内置的命令screencap来捕捉整个屏幕,然后读取特定点的颜色。但是,这太慢了。
我把自己的问题的答案贴出来。答案也许是设备指定的(nexus7 2013),你可以根据自己的设备调整。
1.首先,我发现,命令 screencap screen.png
是相当缓慢的,因为它需要大部分时间转换为png文件类型。所以,为了节省时间,第一步是将屏幕转为原始数据文件。
adb shell
screencap screen.dump
2、检查文件大小。我的屏幕分辨率是1920*1200,文件大小是9216012字节。注意到9216012=1920*1200*4+12,我猜测数据文件用4个字节来存储每一个像素信息,另外用12个字节来做一些神秘的工作人员。只是再做一些截图,我发现每个文件头部的12个byte都是一样的。所以,多出来的12个byte是在数据文件的头部。
3.现在,事情很简单,用 dd
和 hd
. 假设我想得到(x,y)处的颜色。
let offset=1200*$y+$x+3
dd if='screen.dump' bs=4 count=1 skip=$offset 2>/dev/null | hd
我得到这样的输出
00000000: 4b 73 61 ff s 21e
sum 21e
该 4b 73 61 ff
是我的答案。
如果你的手机已经root了,而且你知道它的。framebuffer
你可以使用 dd
和 hd
(hexdump)来直接获取像素表示法。framebuffer
文件。
adb shell "dd if=/dev/graphics/fb0 bs=<bytes per pixel> count=1 skip=<pixel offset> 2>/dev/null | hd"
通常 <bytes per pixel> = 4
和 <pixel offset> = Y * width + X
但在你的手机上可能有所不同。
根据之前接受的答案,我写了一个SH函数,能够计算缓冲区等,在我的手机上运行。
使用方法
GetColorAtPixel X Y
GIST : https:/gist.github.comThomazPomd5a6d74acdec5889fabcb0effe67a160。
widthheight=$(wm size | sed "s/.* //")
width=$(($(echo $widthheight | sed "s/x.*//g" )+0))
height=$(($(echo $widthheight | sed "s/.*x//g" )+0))
GetColorAtPixel () {
x=$1;y=$2;
rm ./screen.dump 2> /dev/null
screencap screen.dump
screenshot_size=$(($(wc -c < ./screen.dump)+0));
buffer_size=$(($screenshot_size/($width*height)))
let offset=$width*$y+$x+3
color=$(dd if="screen.dump" bs=$buffer_size count=1 skip=$offset 2>/dev/null | hd | grep -Eo "([0-9A-F]{2} )" |sed "s/[^0-9A-F]*\$//g" | sed ':a;N;$!ba;s/\n//g' |cut -c3-8)
echo $color;
}
还有一个替代版本,(当我把它嵌入到sh文件中时,第一个版本对我来说不适用,因为有一些未知的默认hexdump行为问题)。
widthheight=$(wm size | sed "s/.* //")
width=$(($(echo $widthheight | sed "s/x.*//g" )+0))
height=$(($(echo $widthheight | sed "s/.*x//g" )+0))
GetColorAtPixel () {
x=$1;y=$2;
rm ./screen.dump 2> /dev/null
screencap screen.dump
screenshot_size=$(($(wc -c < ./screen.dump)+0));
buffer_size=$(($screenshot_size/($width*height)))
let offset=$width*$y+$x+3
color=$(dd if="screen.dump" bs=$buffer_size count=1 skip=$offset 2>/dev/null | /system/xbin/hd | awk '{ print toupper($0) }' | grep -Eo "([0-9A-F]{2})+" | sed ':a;N;$!ba;s/\n//g' | cut -c9-14 )
echo $color;
}