简体   繁体   中英

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..

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

Take Screenshot :

           ProcessBuilder processBuilderq1 = new ProcessBuilder(new String[] { "cmd.exe", "/c", "adb shell /system/bin/screencap -p /sdcard/s.png"});
           Process pq1 = processBuilderq1.start();

Pull to PC :

               ProcessBuilder processBuilderq1 = new ProcessBuilder(new String[] { "cmd.exe", "/c", "adb pull /sdcard/s.png D:\\Images\\s1.png"});
               Process pq1 = processBuilderq1.start();

you might want to check this. How to programmatically take a screenshot in Android?

Try taking the picture and saving it in a separate process.

you can try calling ADB in your program (supposing USB driver are installed and computer authorized on the mobile):

Using perl:

Source: here

You can call executable in Java using: Process process = new ProcessBuilder("C:\\\\PathToExe\\\\MyExe.exe","param1","param2").start();

Source: Java Programming: call an exe from Java and passing parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM