0

Making an app at the moment for my personal use (rooted) and it requires getting certain pixels colors from the screen. I was trying to accomplish this through the Runtime.

Process p = Runtime.getRuntime().exec("screencap");
p.waitFor();
InputStream is = p.getInputStream()
BitmapFactory.decodeStream(is);

and I get factory returned null.

but if I dump the process to my sd card through adb -d shell screencap /sdcard/ss.dump and access it from my app

BitmapFactory.decodeFile("/sdcard/ss.dump");

all goes well.

So it there anyway to dump the stream straight into BitmapFactory within my app?

Thanks SO and please excuse the generally laziness/shortcuts of the example code.

2 Answers2

1

This might help if not too far off your intended path. (I think you are using node / javascript). I spawned the ADB.EXE command producing a stream (and being 'jailed' on Windows the program must transform the stream to account for linefeed ending differences. So with that, I have working the following:

exports.capture = function(filename) {

    // you'll need to map your requirement (decodeStream) instead 
    // of streaming to a file

    var strm = fs.createWriteStream(path);  
    var cv = new Convert();
    cv.pipe(strm);

    var capture = spawn(cmd, args);

    capture.stdout.on('data', function(data) {
        cv.write(data);
    });
    capture.stdout.on('exit', function(data) {
        cv.end();
    });
}

To explain the process, spawn is running the ADB command, on windows, CR-LF are inserted (being a PNG stream), and stream is chunked / piped through a fs-transformation. Others on the web have described the process as adb.exe shell screencap -p | sed 's/\r$//' > output.file. And it does work. To be clear the conversion is CR-CR-LF => LF for us window jailed birds. And if you don't want to implement a SED and not use javascript regular expressions converting binary->strings->binary, you may follow my path. There is probably a simpler way, I just don't know what it is...

So, Convert() is an object with methods that converts the byte stream on the fly.

See the codewind blog link is: http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html to build your convert.

CeqUser
  • 11
  • 3
0

When using screencap from inside an app you must use su, i.e. root. When you do this via adb it runs under a different user id, which has more permissions than a normal Android app.

There are several examples how to use screencap, e.g. here.

Community
  • 1
  • 1
Oliver Jonas
  • 1,188
  • 14
  • 12