Android 如何通过ADB获得屏幕像素的颜色

Android 如何通过ADB获得屏幕像素的颜色,android,colors,screen,adb,pixel,Android,Colors,Screen,Adb,Pixel,我需要在我的android手机屏幕上获取特定点的颜色信息 有没有办法通过亚洲开发银行做到这一点 我现在使用内置命令screencap捕获整个屏幕,然后读取特定点的颜色。但是,速度太慢。如果您的手机是根目录,并且您知道其帧缓冲区格式,则可以使用dd和hd(hextump)直接从帧缓冲区文件获取像素表示: adb shell "dd if=/dev/graphics/fb0 bs=<bytes per pixel> count=1 skip=<pixel offset> 2&

我需要在我的android手机屏幕上获取特定点的颜色信息

有没有办法通过亚洲开发银行做到这一点


我现在使用内置命令screencap捕获整个屏幕,然后读取特定点的颜色。但是,速度太慢。

如果您的手机是根目录,并且您知道其
帧缓冲区
格式,则可以使用
dd
hd
(hextump)直接从
帧缓冲区
文件获取像素表示:

adb shell "dd if=/dev/graphics/fb0 bs=<bytes per pixel> count=1 skip=<pixel offset> 2>/dev/null | hd"
adb shell“dd if=/dev/graphics/fb0 bs=count=1 skip=2>/dev/null | hd”

通常
=4
=Y*width+X
,但在您的手机上可能会有所不同。

我将发布我自己问题的答案。答案可能是指定的设备(nexus7 2013),您可以根据自己的设备进行调整

1.首先,我发现命令
screencap screen.png
非常慢,因为它需要花费大部分时间转换为png文件类型。所以,为了节省时间,第一步是将屏幕转储到原始数据文件。

亚行壳牌
screencap screen.dump

2.检查文件大小。我的屏幕分辨率是1920*1200,文件大小是9216012字节。注意到9216012=1920*1200*4+12,我猜数据文件使用4个字节来存储每个像素信息,并使用另一个12字节来执行一些神秘任务。只要再做一些屏幕截图,我发现每个文件头的12字节都是一样的。因此,额外的12字节位于数据文件的开头

3.现在,通过使用
dd
hd
,事情变得简单了。假设我想要得到(x,y)处的颜色:

设偏移量=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
总和21e

4b 73 61 ff
是我的答案。

基于之前接受的答案,我编写了一个SH函数,可以计算缓冲区等,在手机上即时运行

用法:

GetColorAtPixel X Y
要点:

widthheight=$(wm size | sed“s/*/”)
宽度=$($(回声$widthheight | sed“s/x.*//g”)+0))
高度=$($(回声$widthheight | sed“s/*x//g”)+0))
GetColorAtPixel(){
x=1美元;y=2美元;
rm./screen.dump 2>/dev/null
screencap screen.dump
屏幕截图大小=$($(wc-c/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)
回声颜色;
}
还有一个替代版本(第一个版本在我将其嵌入sh文件时不适用于我,因为存在一些未知的默认hexdump行为问题)

widthheight=$(wm size | sed“s/*/”)
宽度=$($(回声$widthheight | sed“s/x.*//g”)+0))
高度=$($(回声$widthheight | sed“s/*x//g”)+0))
GetColorAtPixel(){
x=1美元;y=2美元;
rm./screen.dump 2>/dev/null
screencap screen.dump
屏幕截图大小=$($(wc-c/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)
回声颜色;
}

I type
adb shell“dd if=/dev/graphics/fb0 bs=4 count=1 skip=1000 2>/dev/null | hd”
,它返回
用法:hd[-b base][c count][r delay]文件
。似乎“hd”是在我的bash shell中执行的,而不是在adb shell中执行的。我尝试以下操作:我很可能您没有
root
,因此
dd
不会为
hd
生成任何要处理的输出。我尝试以下操作:
adb shell
su
dd if=/dev/graphics/fb0bs=4 count=1 skip=10002>/dev/null | hd
它返回类似
00000000:00s的值0
sum 0
这4个字节
00
是您要查找的数据。我得到了
C67A2562
,如何将值更改为RGB?很好的解决方案,但为什么我在手机的末尾添加“| hd”时无法执行命令,但在adb.exe控制台执行时可以执行该命令?“hd”做什么?
hd
hextump
用于显示二进制数据。例如,文本文件中有“123”,并且
hd-file
获取
313233
。你的命令是什么?嗨,我不太懂坐标,为什么$y乘以1200而不是1920?4b 73 61 ff也翻译什么?RGB颜色?为什么是4字节?回答我自己。它是红色、绿色、蓝色、Alpha,按此顺序排列。在第一个示例中,您是否尝试过使用
hextdump-C
而不是
hd
?也许这样可以解决嵌入问题。
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;
}
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;
}