Search code examples
javaandroidscreenshot

Java - Get a screenshot of connected android device


I'm trying to write a java program that captures the current screen of the connected (via USB) android device and save it in the pc. I'm looking for any library I can use or any tutorial I can follow.. I'm really lost here..


Solution

  • get the device list from ADB

    IDevice[] devices = bridge.getDevices();
    

    Then you can get the specific device with serial number

    d.getSerialNumber()
    

    Then capture the screen,

    RawImage rawImage = device.getScreenshot();
    

    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);
    

    finally save the image

    ImageIO.write(image, "png", new File(filepath));
    

    use ddmlib.jar

    Source: https://github.com/miracle2k/android-platform_sdk/blob/master/screenshot/src/com/android/screenshot/Screenshot.java

    working and library can be found here.. http://www.java2s.com/Code/Jar/d/Downloadddmlibjar.htm