Android: How can you get framebuffer (screenshot) on rooted device?
Asked Answered
A

2

12

I tried :

process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0 > /sdcard/frame.raw");
process.waitFor();

but it doesn't work. My device is rooted.

I see many answers that it requires rooted access, but no actual code to get the framebuffer.

I also tried glReadPixels() but no luck.

public void TakeScreen() {

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        int screenshotSize = width * height;
        ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
        bb.order(ByteOrder.nativeOrder());
        gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA,
                GL10.GL_UNSIGNED_BYTE, bb);
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer().get(pixelsBuffer);
        bb = null;
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.RGB_565);
        bitmap.setPixels(pixelsBuffer, screenshotSize - width, -width, 0, 0,
                width, height);
        pixelsBuffer = null;

        short sBuffer[] = new short[screenshotSize];
        ShortBuffer sb = ShortBuffer.wrap(sBuffer);
        bitmap.copyPixelsToBuffer(sb);

        for (int i = 0; i < screenshotSize; ++i) {
            short v = sBuffer[i];
            sBuffer[i] = (short) (((v & 0x1f) << 11) | (v & 0x7e0) | ((v & 0xf800) >> 11));
        }
        sb.rewind();
        bitmap.copyPixelsFromBuffer(sb);
        saveBitmap(bitmap, "/screenshots", "capturedImage");
    }
Aletaaletha answered 14/2, 2011 at 23:28 Comment(2)
Were you able to this in the end? If so, could you share how?Bamford
See #17305111Cornerstone
S
0

The answer lies in replicating the way the device itself handles it:

fb = open("/dev/graphics/fb0", O_RDONLY);

check this

Sabin answered 7/8, 2011 at 2:8 Comment(0)
V
0

Seems to me like your problem is this sign: >. You cannot redirect output using exec. What you need to do is grab the output stream of the process (which is the input stream for you) and store it to file;

process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0");
InputStream is = process.getInputStream();
...
Vicentevicepresident answered 7/8, 2011 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.