안드로이드에서 스크린샷 가져오는 방법을 찾아보면
대부분의 대답들이 "On rooted device only"라고 합니다.
그런데 아무리 찾아도 루트된 디바이스에서는 screenshot을 어떻게 가져올수 있는지 안나와 있네요.
제가 여러가지를 해봤습니다:
1. 그냥 layout screenshot가져오는 방법:
LinearLayout layoutRoot = (LinearLayout) findViewById(R.id.layoutRoot);
View v = layoutRoot.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
결과: 그냥 app안에서만 됩니다. Root되도 activity밖으로 나가면 안되네요.
2. NDK사용하여
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");
}
--------------- c 코드 ----------------------
void screen_capture()// AndroidBitmapInfo* info, void* pixels, double t )
{
LOGI("Start\n");
int fd;
struct fb_var_screeninfo vinfo;
int fb, offset;
char x[256];
struct fbinfo fbinfo;
unsigned i, bytespp, j;
LOGI("Before /dev/graphics/fb0\n");
fb = open("/dev/graphics/fb0", O_RDONLY);
LOGI("After /dev/graphics/fb0\n");
if(fb < 0) goto done;
LOGI("fb > 0 yes~!!!\n");
if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done;
fcntl(fb, F_SETFD, FD_CLOEXEC);
bytespp = vinfo.bits_per_pixel / 8;
}
결과: fb = open("/dev/graphics/fb0", O_RDONLY);하고 나서 fb가 null이네요. ㅠ.ㅠ
3. OpenGL
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);
//
Making created bitmap (from OpenGL points) compatible with Android
//
bitmap
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");
//
/lastScreenshot = bitmap;
//
screenshot = false;
}
private void initGLFr() {
egl
= (EGL10) EGLContext.getEGL();
display
= egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[]
ver = new int[2];
egl.eglInitialize(display,
ver);
int[]
configSpec = { EGL10.EGL_NONE };
EGLConfig[]
configOut = new EGLConfig[1];
int[]
nConfig = new int[1];
egl.eglChooseConfig(display,
configSpec, configOut, 1, nConfig);
config
= configOut[0];
eglContext
= egl.eglCreateContext(display, config,
EGL10.EGL_NO_CONTEXT,
null);
//
EGLSurface surface = egl.eglCreateWindowSurface(display, config,
//
SurfaceHolder.SURFACE_TYPE_GPU, null);
//
egl.eglMakeCurrent(display, surface, surface, eglContext);
gl
= (GL11) eglContext.getGL();
}
결과: glReadPixels(..) 이 스크린을 읽지 못하는 것 같습니다.
4. IWandowManager사용하는 방법
결과: Root된 전화기도 이 기능이 안됩니다.
5. 리눅스 command (/dev/graphics/fb0)
Process process = null;
process = Runtime.getRuntime().exec(
"su -c cat /dev/graphics/fb0 > /sdcard/frame.raw");
process.waitFor();
결과: Root된 전화기도 이 기능이 안됩니다.
------------
정말 여러가지 해봤는데 안되네요. 도와주세요.
ADB를 통해서 (DDMS) 하는 방법 말고요.
혹시 아시는 분 있으신가요?