Search code examples
pythonandroidandroid-emulatoradb

how to take screenshot in coordinates using adb


I am testing android adb to screen capture. Then I can take a full screen shot, now I want to capture the coordinates. I use the following command line: adb shell screencap -p -d 0 /sdcard/test.png With this command line, I captured the full screen. After consulting n+1 places, I was told that the following command line captures by coordinates (coordinates are placed after -d id) adb shell screencap -p -d 0 266 655 664 554 /sdcard/test.png

But after running, the results are returned as below, can someone help me

usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the display id to capture, default 0.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

Solution

  • screencap does not accept coordinates as the help clearly states.

    -d specifies the display ID that can be obtained using

     dumpsys SurfaceFlinger --display-id
    

    on a single display device, you'll be fine using the default though.

    Using AndroidViewClient/culebra you can do this

    #! /usr/bin/env python3
    
    from com.dtmilano.android.viewclient import ViewClient
    
    
    device, serialno = ViewClient.connectToDeviceOrExit()
    device.takeSnapshot().crop((100, 50, 500, 600)).save('/tmp/myscreencap.png', 'PNG')
    

    then /tmp/myscreencap.png will contain the cropped box (as (left, top, right, bottom)).

    However, if what you are trying t achieve is to obtain screenshots of specific Views and not just random coordinates you can also use View.writeImageToFile()

    update

    check https://github.com/dtmilano/AndroidViewClient/blob/master/examples/adbclient/screenshot-box for a complete example