2

I'm a newbie in Android. I using Nexus7 reference device and I've downloaded the full source code from source.android.com. I have an engineering system image and I can make a system application.

/system/bin/screencap utility is good for me to capture screen. I want to get a pixel data using screencap.cpp directly in my application.

When I used to screencap utility, the process is like below.

  1. capture screen and save an image.
  2. Open image file
  3. decodefile to bitmap
  4. get pixel data(int array) from bitmap

I want to remove the step 1, 2 and 3. Just call api to get pixel data of a screen directly, How can I do that?

Alex
  • 781
  • 10
  • 23

3 Answers3

1

If you're running with system privileges, you can just ask SurfaceComposerClient for the pixel data rather than launching a separate process to do it for you.

Looking at the screencap source code, all you really need is the Binder initialization:

ProcessState::self()->startThreadPool();

and the SurfaceComposerClient IPC call:

ScreenshotClient screenshot;
sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
if (display != NULL && screenshot.update(display, Rect(), false) == NO_ERROR) {
    base = screenshot.getPixels();
    w = screenshot.getWidth();
    h = screenshot.getHeight();
    s = screenshot.getStride();
    f = screenshot.getFormat();
    size = screenshot.getSize();
}

You can safely ignore all the /dev/graphics/fb0 stuff below it -- it's an older approach that no longer works. The rest of the code is just needed for the PNG compression.

If you're not running with system privileges, you can't capture the entire screen. You can capture your app though.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
0

If you are writing a Java app just call /system/bin/screencap from your application (using java.lang.Process) and read the result into memory as a binary stream. You can see the binary structure in screencap.cpp, but it's just width, height, and format as four byte integers followed by the data.

Note to other readers: this is only be possible if you are a system app.

Rupert Rawnsley
  • 2,622
  • 1
  • 29
  • 40
0

1) You can transfer data from your screencap utility to your App over the network by using sockets.

2) Android NDK can be used for direct function calls of your utility from your App.

Dipika
  • 584
  • 2
  • 12