user9480
user9480

Reputation: 324

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

Upvotes: 2

Views: 2224

Answers (4)

Reetpreet Brar
Reetpreet Brar

Reputation: 104

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

Upvotes: 1

user9480
user9480

Reputation: 324

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

Upvotes: 2

gbaccetta
gbaccetta

Reputation: 4577

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

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

Using perl:
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

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

Upvotes: 0

Nimila Hiranya
Nimila Hiranya

Reputation: 5192

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.

Upvotes: 0

Related Questions