android - "adb screencap/sdcard/screenshot.raw"产生什么格式? (没有 "-p"标志)

标签 android python adb python-imaging-library

我希望在没有 -p 标志的情况下使用 adb screencap 实用程序。我想象输出将以原始格式转储,但看起来不像。我尝试使用 Pillow (python) 库打开原始图像文件的结果是:

$ adb pull /sdcard/screenshot.raw screenshot.raw
$ python
>>> from PIL import Image
>>> Image.open('screenshot.raw')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/....../lib/python2.7/site-packages/PIL/Image.py", line 2025, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file

发现这样读取原始图像的方法不对,我什至试了一下:How to read a raw image using PIL?

>>> with open('screenshot.raw', 'rb') as f:
...     d = f.read()
... 
>>> from PIL import Image
>>> Image.frombuffer('RGB', len(d), d)
__main__:1: RuntimeWarning: the frombuffer defaults may change in a future release; for portability, change the call to read:
  frombuffer(mode, size, data, 'raw', mode, 0, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1896, in frombuffer
    return frombytes(mode, size, data, decoder_name, args)
  File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1821, in frombytes
    im = new(mode, size)
  File "/Users/..../lib/python2.7/site-packages/PIL/Image.py", line 1787, in new
    return Image()._new(core.fill(mode, size, color))
TypeError: must be 2-item sequence, not int

所有可能的模式选项都会导致相同的 TypeError 异常。

这是 hexdump 实用程序揭示的内容:

$ hexdump -C img.raw | head
00000000  d0 02 00 00 00 05 00 00  01 00 00 00 1e 1e 1e ff  |................|
00000010  1e 1e 1e ff 1e 1e 1e ff  1e 1e 1e ff 1e 1e 1e ff  |................|
*
000038c0  1e 1e 1e ff 1e 1e 1e ff  21 21 21 ff 2b 2b 2b ff  |........!!!.+++.|
000038d0  1e 1e 1e ff 1e 1e 1e ff  1e 1e 1e ff 1e 1e 1e ff  |................|
*
00004400  1e 1e 1e ff 1e 1e 1e ff  47 47 47 ff 65 65 65 ff  |........GGG.eee.|
00004410  20 20 20 ff 1e 1e 1e ff  1e 1e 1e ff 1e 1e 1e ff  |   .............|
00004420  1e 1e 1e ff 1e 1e 1e ff  1e 1e 1e ff 1e 1e 1e ff  |................|
*

在 OSX 上:

$ file screenshot.raw 
screenshot.raw: data

screencap 帮助页面没有透露太多关于没有 -p 标志的输出数据的格式:

$ adb shell screencap -h
usage: screencap [-hp] [FILENAME]
   -h: this message
   -p: save the file as a png.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

最佳答案

格式:

  • 4 个字节作为 uint32 - 宽度
  • 4 个字节作为 uint32 - 高度
  • 4 个字节作为 uint32 - pixel format
  • (width * heigth * bytespp) 字节作为字节数组 - 图像数据,其中 bytespp 是每像素的字节数,取决于像素格式。通常bytespp为4。

信息来自 source code of screencap .

以你的例子为例:

00000000  d0 02 00 00 00 05 00 00  01 00 00 00 1e 1e 1e ff
  • d0 02 00 00 - 宽度 - uint32 0x000002d0 = 720
  • 00 05 00 00 - 高度 - uint32 0x00000500 = 1280
  • 01 00 00 00 - 像素格式 - uint32 0x00000001 = 1 = PixelFormat.RGBA_8888 => bytespp = 4 => RGBA<
  • 1e 1e 1e ff - 第一个像素数据 - R = 0x1e; G = 0x1e; B = 0x1e; A = 0xff;

数据存储在大小为 720*1280*4 的字节数组中的像素。

关于android - "adb screencap/sdcard/screenshot.raw"产生什么格式? (没有 "-p"标志),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22034959/

相关文章:

android - 如何在选项卡式应用程序中替换 LinearLayout 下的 fragment

java - 如何从 Firebase 实时数据库获取数据并将其显示在微调器中?

python - as_strided : Linking stepsize (strides of conv2d) with as_strided strides parameter

android - Lollipop View 的涟漪效应

python - 使用列表打开文件?

python - 无法使用 read_excel 从 pandas 中的 xlsx 文件读取日期列?

macOS 中的 Android Studio 在点击 'run' 后无法识别物理设备

android - android 上的 Tcpdump 跟踪 - 无法加载 "libssl.so"所需的库 "/system/bin/tcpdump"

adb - 如何从私钥获取adbkey.pub?

android - 具有 AppCompat 的透明覆盖 ActionBar?