2

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

user9480
  • 324
  • 1
  • 13

4 Answers4

2

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

user9480
  • 324
  • 1
  • 13
1

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

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.

Community
  • 1
  • 1
Nimila Hiranya
  • 4,842
  • 10
  • 35
  • 52
  • This is from the android side right?? I want to do this from java.. I'm trying to do this without an android app.. – user9480 Sep 16 '15 at 11:01
0

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

Community
  • 1
  • 1
gbaccetta
  • 4,449
  • 2
  • 20
  • 30
  • is there a way to do this with java?? – user9480 Sep 16 '15 at 11:58
  • this should work: Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start(); source: http://stackoverflow.com/questions/5604698/java-programming-call-an-exe-from-java-and-passing-parameters – gbaccetta Sep 16 '15 at 12:14
  • calling like this might work... but the problem is, as I understand this code, here I'm saving the screenshot in phone memory and then I'm getting that to pc..This procedure might take some time.. Here I'm thinking about the time too.. and also I saw in a forum, saying that going through ADB takes some time... :( – user9480 Sep 16 '15 at 12:24
  • yes for sure it takes some time, but it depends on the requirements of your app that could be negligeable... Unfortunately on my side I do not know other way to retrieve screenshots. Sorry about that. Maybe others users will know of a faster solution. – gbaccetta Sep 16 '15 at 12:44
  • Yeah.. I think about the time too... that's the problem... btw thank you for your answer... :) – user9480 Sep 16 '15 at 12:47