Ways to take android screenshot from command line

adb shell screencap

Android 4.0 +

adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
adb shell rm /sdcard/screen.png

w/o adb pull

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
adb shell screencap -p | perl -pi -e 's/\r\n/\n/g' > screen.png

This (w/o adb pull) is faster than saving the screen capture image to device sd card then pull/read it from that card and transfer it to PC via USB.

The former way takes a couple of seconds and the latter usually takes less than a sec.

Read framebuffer

adb pull /dev/graphics/fb0

adb pull /dev/graphics/fb0 fb.raw
adb pull /dev/fb0 fb.raw
ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s X:Y -i fb.raw -f image2 -vcodec png ss.png

directly speak to adb server

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 5037))
s.send("%04x%s" % (len("framebuffer:"), "framebuffer:"))
s.recv(4096) # ret should be "OKAY", 4096 is just a buffer size
s.recv(52) # ret should be struct fbinfo

f = open('fb.raw', 'w')
while True:
  data = s.recv(4096*16) # real fb data
  if data == "":
    break
  f.write(data)
f.close

ddmlib

mport com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public final String adbLocation = 'PATH_TO_ADB_PLATFORM_TOOLS';

AndroidDebugBridge.init(false);
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(
                    adbLocation, true);
IDevice device = bridge.getDevices()[0];
RawImage rawImage;

        try {
            rawImage = device.getScreenshot();
        }
        catch (IOException ioe) {
            printAndExit("Unable to get frame buffer: " + ioe.getMessage(), true /* terminate */);
            return;
        }

        // device/adb not available?
        if (rawImage == null)
            return;

        if (landscape) {
            rawImage = rawImage.getRotated();
        }

        // convert raw data to an Image
        BufferedImage image = new BufferedImage(rawImage.width, rawImage.height,
                BufferedImage.TYPE_INT_ARGB);

        int index = 0;
        int IndexInc = rawImage.bpp >> 3;
        for (int y = 0 ; y < rawImage.height ; y++) {
            for (int x = 0 ; x < rawImage.width ; x++) {
                int value = rawImage.getARGB(index);
                index += IndexInc;
                image.setRGB(x, y, value);
            }
        }

        if (!ImageIO.write(image, "png", new File(filepath))) {
            throw new IOException("Failed to find png writer");
        }

monkeyrunner

Pre requisite

  1. Confirm you have installed monkeyrunner
    • it is located at $ANDROID_HOME/tools by default (where ANDROID_HOME should refer to the directory Android SDK is installed).
  2. (Optional) Confirm installed monkeyrunner is in your $PATH environment variable

Prepare a monkeyrunner script which does the job(taking a screenshot of target device).

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('1.png','png')

Then run monkeyrunner.

$ monkeyrunner ss.py

Reference

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s