解析android截屏问题
程序员文章站
2023-11-25 10:29:16
我是基于android2.3.3系统之上的,想必大家应该知道在android源码下面有个文件叫做screencap吧,位于frameworks\base\services\...
我是基于android2.3.3系统之上的,想必大家应该知道在android源码下面有个文件叫做screencap吧,位于frameworks\base\services\surfaceflinger\tests\screencap\screencap.cpp,你直接在linux下编译(保存在 /system/bin/test-screencap),然后push到手机上再通过电脑去敲命令test-screencap /mnt/sdcard/scapxx.png就可以实现截屏。
/*
* copyright (c) 2010 the android open source project
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
*
* http://www.apache.org/licenses/license-2.0
*
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*/
#include <utils/log.h>
#include <binder/ipcthreadstate.h>
#include <binder/processstate.h>
#include <binder/iservicemanager.h>
#include <binder/imemory.h>
#include <surfaceflinger/isurfacecomposer.h>
#include <skimageencoder.h>
#include <skbitmap.h>
using namespace android;
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s path\n", argv[0]);
exit(0);
}
const string16 name("surfaceflinger");
sp<isurfacecomposer> composer;
getservice(name, &composer);
sp<imemoryheap> heap;
uint32_t w, h;
pixelformat f;
status_t err = composer->capturescreen(0, &heap, &w, &h, &f, 0, 0);
if (err != no_error) {
fprintf(stderr, "screen capture failed: %s\n", strerror(-err));
exit(0);
}
printf("screen capture success: w=%u, h=%u, pixels=%p\n",
w, h, heap->getbase());
printf("saving file as png in %s ...\n", argv[1]);
skbitmap b;
b.setconfig(skbitmap::kargb_8888_config, w, h);
b.setpixels(heap->getbase());
skimageencoder::encodefile(argv[1], b,
skimageencoder::kpng_type, skimageencoder::kdefaultquality);
return 0;
}
其实这个程序真正用到的就是一个叫做capturescreen的函数,而capturescreen会调用capturescreenimpllocked这个函数
下面是代码:
status_t surfaceflinger::capturescreenimpllocked(displayid dpy,
sp<imemoryheap>* heap,
uint32_t* w, uint32_t* h, pixelformat* f,
uint32_t sw, uint32_t sh)
{
logi("capturescreenimpllocked");
status_t result = permission_denied;
// only one display supported for now
if (unlikely(uint32_t(dpy) >= display_count))
return bad_value;
if (!glextensions::getinstance().haveframebufferobject())
return invalid_operation;
// get screen geometry
const displayhardware& hw(graphicplane(dpy).displayhardware());
const uint32_t hw_w = hw.getwidth();
const uint32_t hw_h = hw.getheight();
if ((sw > hw_w) || (sh > hw_h))
return bad_value;
sw = (!sw) ? hw_w : sw;
sh = (!sh) ? hw_h : sh;
const size_t size = sw * sh * 4;
// make sure to clear all gl error flags
while ( glgeterror() != gl_no_error ) ;
// create a fbo
gluint name, tname;
glgenrenderbuffersoes(1, &tname);
glbindrenderbufferoes(gl_renderbuffer_oes, tname);
glrenderbufferstorageoes(gl_renderbuffer_oes, gl_rgba8_oes, sw, sh);
glgenframebuffersoes(1, &name);
glbindframebufferoes(gl_framebuffer_oes, name);
glframebufferrenderbufferoes(gl_framebuffer_oes,
gl_color_attachment0_oes, gl_renderbuffer_oes, tname);
glenum status = glcheckframebufferstatusoes(gl_framebuffer_oes);
if (status == gl_framebuffer_complete_oes) {
// invert everything, b/c glreadpixel() below will invert the fb
glviewport(0, 0, sw, sh);
glscissor(0, 0, sw, sh);
glmatrixmode(gl_projection);
glpushmatrix();
glloadidentity();
glorthof(0, hw_w, 0, hw_h, 0, 1);
glmatrixmode(gl_modelview);
// redraw the screen entirely...
glclearcolor(0,0,0,1);
glclear(gl_color_buffer_bit);
const vector< sp<layerbase> >& layers(mvisiblelayerssortedbyz);
const size_t count = layers.size();
for (size_t i=0 ; i<count ; ++i) {
const sp<layerbase>& layer(layers[i]);
layer->drawforsreenshot();
}
// xxx: this is needed on tegra
glscissor(0, 0, sw, sh);
// check for errors and return screen capture
if (glgeterror() != gl_no_error) {
// error while rendering
result = invalid_operation;
} else {
// allocate shared memory large enough to hold the
// screen capture
sp<memoryheapbase> base(
new memoryheapbase(size, 0, "screen-capture") );
void* const ptr = base->getbase();
if (ptr) {
// capture the screen with glreadpixels()
glreadpixels(0, 0, sw, sh, gl_rgba, gl_unsigned_byte, ptr);
if (glgeterror() == gl_no_error) {
*heap = base;
*w = sw;
*h = sh;
*f = pixel_format_rgba_8888;
result = no_error;
}
} else {
result = no_memory;
}
}
glenable(gl_scissor_test);
glviewport(0, 0, hw_w, hw_h);
glmatrixmode(gl_projection);
glpopmatrix();
glmatrixmode(gl_modelview);
} else {
result = bad_value;
}
// release fbo resources
glbindframebufferoes(gl_framebuffer_oes, 0);
gldeleterenderbuffersoes(1, &tname);
gldeleteframebuffersoes(1, &name);
hw.compositioncomplete();
return result;
}
status_t surfaceflinger::capturescreen(displayid dpy,
sp<imemoryheap>* heap,
uint32_t* width, uint32_t* height, pixelformat* format,
uint32_t sw, uint32_t sh)
{
logi("into capturescreen");
// only one display supported for now
if (unlikely(uint32_t(dpy) >= display_count))
return bad_value;
if (!glextensions::getinstance().haveframebufferobject())
return invalid_operation;
class messagecapturescreen : public messagebase {
surfaceflinger* flinger;
displayid dpy;
sp<imemoryheap>* heap;
uint32_t* w;
uint32_t* h;
pixelformat* f;
uint32_t sw;
uint32_t sh;
status_t result;
public:
messagecapturescreen(surfaceflinger* flinger, displayid dpy,
sp<imemoryheap>* heap, uint32_t* w, uint32_t* h, pixelformat* f,
uint32_t sw, uint32_t sh)
: flinger(flinger), dpy(dpy),
heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(permission_denied)
{
}
status_t getresult() const {
logi("getresult()");
return result;
}
virtual bool handler() {
logi("handler()");
mutex::autolock _l(flinger->mstatelock);
// if we have secure windows, never allow the screen capture
if (flinger->msecureframebuffer)
return true;
result = flinger->capturescreenimpllocked(dpy,
heap, w, h, f, sw, sh);
return true;
}
};
logi("before messagecapturescreen");
sp<messagebase> msg = new messagecapturescreen(this,
dpy, heap, width, height, format, sw, sh);
status_t res = postmessagesync(msg);
if (res == no_error) {
res = static_cast<messagecapturescreen*>( msg.get() )->getresult();
}
return res;
}
而这个函数关键又使用了opengl的几个函数去获得图片,然而opengl又去read framebuffer(这是我的理解)。如果你去用jni调用so的方法去截屏的话,就可以把screencap这个文件稍微修改一下然后做成so文件。
主要是补充一下怎么去存放文件与编译吧,当然我说的方法只是我做的方法不代表是很好用的。
存放:在eclipse新建一个android工程,保存后找到这个工程(如screencap)的存放位置 然后把这个文件放到android源代码的development文件里面,然后在你的那个工程文件里面新建一个文件夹,名字叫做jni(这个文件夹平行于src文件夹,screencap/jni),把上面博客提到的那个c++跟mk(screencap/jni/com_android_screencap_screencapnative.cpp和screencap/jni/android.mk)文件放进去,最后在把编译的mk文件放在screencap目录下(screencap/android.mk);
编译:编译是个很伟大的工程,需要你花大量的时间与精力。直接在终端进入工程存放的所在位置,我的是administrator/android.2.3.3/development,然后mm(builds all of the modules in the current directory),如果成功,那么你运气比较好,在终端回提示你apk保存的位置。push进手机试一试。但是往往是不成功的。你可能会遇到一些问题,比如android.permission.access_surface_flinger ,android.permission.read_frame_buffer(因为capturescrren这个函数是surfaceflinger里面的函数,然而surfaceflinger里面的opengl截屏函数会去读取framebuffer),相关源代码是:
status_t surfaceflinger::ontransact(
uint32_t code, const parcel& data, parcel* reply, uint32_t flags)
{
switch (code) {
case create_connection:
case open_global_transaction:
case close_global_transaction:
case set_orientation:
case freeze_display:
case unfreeze_display:
case boot_finished:
case turn_electron_beam_off:
case turn_electron_beam_on:
{
// codes that require permission check
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
if ((uid != aid_graphics) && !maccesssurfaceflinger.check(pid, uid)) {
loge("permission denial: "
"can't access surfaceflinger pid=%d, uid=%d", pid, uid);
return permission_denied;
}
break;
}
case capture_screen:
{
// codes that require permission check
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
if ((uid != aid_graphics) && !mreadframebuffer.check(pid, uid)) {
loge("permission denial: "
"can't read framebuffer pid=%d, uid=%d", pid, uid);
return permission_denied;
}
break;
}
}
status_t err = bnsurfacecomposer::ontransact(code, data, reply, flags);
if (err == unknown_transaction || err == permission_denied) {
check_interface(isurfacecomposer, data, reply);
if (unlikely(!mhardwaretest.checkcalling())) {
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
logi("err");
loge("permission denial: "
"can't access surfaceflinger pid=%d, uid=%d", pid, uid);
return permission_denied;
}
int n;
switch (code) {
case 1000: // show_cpu, not supported anymore
case 1001: // show_fps, not supported anymore
return no_error;
case 1002: // show_updates
n = data.readint32();
mdebugregion = n ? n : (mdebugregion ? 0 : 1);
return no_error;
case 1003: // show_background
n = data.readint32();
mdebugbackground = n ? 1 : 0;
return no_error;
case 1004:{ // repaint everything
mutex::autolock _l(mstatelock);
const displayhardware& hw(graphicplane(0).displayhardware());
mdirtyregion.set(hw.bounds()); // careful that's not thread-safe
signalevent();
return no_error;
}
case 1005:{ // force transaction
settransactionflags(etransactionneeded|etraversalneeded);
return no_error;
}
case 1006:{ // enable/disable graphiclog
int enabled = data.readint32();
graphiclog::getinstance().setenabled(enabled);
return no_error;
}
case 1007: // set mfreezecount
mfreezecount = data.readint32();
mfreezedisplaytime = 0;
return no_error;
case 1010: // interrogate.
reply->writeint32(0);
reply->writeint32(0);
reply->writeint32(mdebugregion);
reply->writeint32(mdebugbackground);
return no_error;
case 1013: {
mutex::autolock _l(mstatelock);
const displayhardware& hw(graphicplane(0).displayhardware());
reply->writeint32(hw.getpageflipcount());
}
return no_error;
}
}
return err;
}
这个仅仅只是开始! 你会发现你即使在xml里面添加相应的权限仍然会有这个问题出现,为什么呢?在packagemanger文件里面发现相关代码:
int checksignatureslp(signature[] s1, signature[] s2) {
if (s1 == null) {
return s2 == null
? packagemanager.signature_neither_signed
: packagemanager.signature_first_not_signed;
}
if (s2 == null) {
return packagemanager.signature_second_not_signed;
}
hashset<signature> set1 = new hashset<signature>();
for (signature sig : s1) {
set1.add(sig);
}
hashset<signature> set2 = new hashset<signature>();
for (signature sig : s2) {
set2.add(sig);
}
// make sure s2 contains all signatures in s1.
if (set1.equals(set2)) {
return packagemanager.signature_match;
}
return packagemanager.signature_no_match;
}
// check for shared user signatures
if (pkgsetting.shareduser != null && pkgsetting.shareduser.signatures.msignatures != null) {
if (checksignatureslp(pkgsetting.shareduser.signatures.msignatures,
pkg.msignatures) != packagemanager.signature_match) {
slog.e(tag, "package " + pkg.packagename
+ " has no signatures that match those in shared user "
+ pkgsetting.shareduser.name + "; ignoring!");
mlastscanerror = packagemanager.install_failed_shared_user_incompatible;
return false;
}
}
return true;
private boolean verifysignatureslp(packagesetting pkgsetting,
packageparser.package pkg) {
// check for shared user signatures
if (pkgsetting.shareduser != null && pkgsetting.shareduser.signatures.msignatures != null) {
if (checksignatureslp(pkgsetting.shareduser.signatures.msignatures,
pkg.msignatures) != packagemanager.signature_match) {
slog.e(tag, "package " + pkg.packagename
+ " has no signatures that match those in shared user "
+ pkgsetting.shareduser.name + "; ignoring!");
mlastscanerror = packagemanager.install_failed_shared_user_incompatible;
return false;
}
}
return true;
}
你在终端输入adb logcat | grep packagemanager 你会发现这两个权限根本没有赋予给你的apk,我的理解是,程序需要权限,然后apk仍然需要权限。那怎么样给apk赋予权限呢,两个方法,一个是在我上面说的screencap/android.mk里面添加platform一行,然后在回到mm。还有一个方法就是通过sign。这两个方法都是给apk赋予system权限,但是我试过这两种方法,都有问题,就是在adb install的时候会显示签名不兼容,查看源代码会发现uid跟gid不匹配。这些是我这段时间发现的问题,大家有问题可以交流交流。
再说说几个简单的应用层截屏吧,很简单,就是几个函数调用而已
view view = getwindow().getdecorview();
display display = this.getwindowmanager().getdefaultdisplay();
view.layout(0, 0, display.getwidth(), display.getheight());
view.setdrawingcacheenabled(true);//允许当前窗口保存缓存信息,这样 getdrawingcache()方法才会返回一个bitmap
bitmap bmp = bitmap.createbitmap(view.getdrawingcache());
我对这个程序的理解就是,它仅仅只能截取当前的activity,也就是说如果你运行这个程序后它就截取你这个程序的当前屏幕的信息。我们假设你做成一个按钮,在点击按钮后sleep5秒再调用这个方法(假设你的activity叫做screen)。当你点击按钮以后,然后你再点击home或者返回按钮,等到5秒后你那个程序就会截取到你当前屏幕?不是!它只会截取那个运行于后台的screen这个activity。
这些只是我的一点小小的总结,而且肯定有不对的地方,希望大家一起来解决截屏的问题!
复制代码 代码如下:
/*
* copyright (c) 2010 the android open source project
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
*
* http://www.apache.org/licenses/license-2.0
*
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*/
#include <utils/log.h>
#include <binder/ipcthreadstate.h>
#include <binder/processstate.h>
#include <binder/iservicemanager.h>
#include <binder/imemory.h>
#include <surfaceflinger/isurfacecomposer.h>
#include <skimageencoder.h>
#include <skbitmap.h>
using namespace android;
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s path\n", argv[0]);
exit(0);
}
const string16 name("surfaceflinger");
sp<isurfacecomposer> composer;
getservice(name, &composer);
sp<imemoryheap> heap;
uint32_t w, h;
pixelformat f;
status_t err = composer->capturescreen(0, &heap, &w, &h, &f, 0, 0);
if (err != no_error) {
fprintf(stderr, "screen capture failed: %s\n", strerror(-err));
exit(0);
}
printf("screen capture success: w=%u, h=%u, pixels=%p\n",
w, h, heap->getbase());
printf("saving file as png in %s ...\n", argv[1]);
skbitmap b;
b.setconfig(skbitmap::kargb_8888_config, w, h);
b.setpixels(heap->getbase());
skimageencoder::encodefile(argv[1], b,
skimageencoder::kpng_type, skimageencoder::kdefaultquality);
return 0;
}
其实这个程序真正用到的就是一个叫做capturescreen的函数,而capturescreen会调用capturescreenimpllocked这个函数
下面是代码:
复制代码 代码如下:
status_t surfaceflinger::capturescreenimpllocked(displayid dpy,
sp<imemoryheap>* heap,
uint32_t* w, uint32_t* h, pixelformat* f,
uint32_t sw, uint32_t sh)
{
logi("capturescreenimpllocked");
status_t result = permission_denied;
// only one display supported for now
if (unlikely(uint32_t(dpy) >= display_count))
return bad_value;
if (!glextensions::getinstance().haveframebufferobject())
return invalid_operation;
// get screen geometry
const displayhardware& hw(graphicplane(dpy).displayhardware());
const uint32_t hw_w = hw.getwidth();
const uint32_t hw_h = hw.getheight();
if ((sw > hw_w) || (sh > hw_h))
return bad_value;
sw = (!sw) ? hw_w : sw;
sh = (!sh) ? hw_h : sh;
const size_t size = sw * sh * 4;
// make sure to clear all gl error flags
while ( glgeterror() != gl_no_error ) ;
// create a fbo
gluint name, tname;
glgenrenderbuffersoes(1, &tname);
glbindrenderbufferoes(gl_renderbuffer_oes, tname);
glrenderbufferstorageoes(gl_renderbuffer_oes, gl_rgba8_oes, sw, sh);
glgenframebuffersoes(1, &name);
glbindframebufferoes(gl_framebuffer_oes, name);
glframebufferrenderbufferoes(gl_framebuffer_oes,
gl_color_attachment0_oes, gl_renderbuffer_oes, tname);
glenum status = glcheckframebufferstatusoes(gl_framebuffer_oes);
if (status == gl_framebuffer_complete_oes) {
// invert everything, b/c glreadpixel() below will invert the fb
glviewport(0, 0, sw, sh);
glscissor(0, 0, sw, sh);
glmatrixmode(gl_projection);
glpushmatrix();
glloadidentity();
glorthof(0, hw_w, 0, hw_h, 0, 1);
glmatrixmode(gl_modelview);
// redraw the screen entirely...
glclearcolor(0,0,0,1);
glclear(gl_color_buffer_bit);
const vector< sp<layerbase> >& layers(mvisiblelayerssortedbyz);
const size_t count = layers.size();
for (size_t i=0 ; i<count ; ++i) {
const sp<layerbase>& layer(layers[i]);
layer->drawforsreenshot();
}
// xxx: this is needed on tegra
glscissor(0, 0, sw, sh);
// check for errors and return screen capture
if (glgeterror() != gl_no_error) {
// error while rendering
result = invalid_operation;
} else {
// allocate shared memory large enough to hold the
// screen capture
sp<memoryheapbase> base(
new memoryheapbase(size, 0, "screen-capture") );
void* const ptr = base->getbase();
if (ptr) {
// capture the screen with glreadpixels()
glreadpixels(0, 0, sw, sh, gl_rgba, gl_unsigned_byte, ptr);
if (glgeterror() == gl_no_error) {
*heap = base;
*w = sw;
*h = sh;
*f = pixel_format_rgba_8888;
result = no_error;
}
} else {
result = no_memory;
}
}
glenable(gl_scissor_test);
glviewport(0, 0, hw_w, hw_h);
glmatrixmode(gl_projection);
glpopmatrix();
glmatrixmode(gl_modelview);
} else {
result = bad_value;
}
// release fbo resources
glbindframebufferoes(gl_framebuffer_oes, 0);
gldeleterenderbuffersoes(1, &tname);
gldeleteframebuffersoes(1, &name);
hw.compositioncomplete();
return result;
}
status_t surfaceflinger::capturescreen(displayid dpy,
sp<imemoryheap>* heap,
uint32_t* width, uint32_t* height, pixelformat* format,
uint32_t sw, uint32_t sh)
{
logi("into capturescreen");
// only one display supported for now
if (unlikely(uint32_t(dpy) >= display_count))
return bad_value;
if (!glextensions::getinstance().haveframebufferobject())
return invalid_operation;
class messagecapturescreen : public messagebase {
surfaceflinger* flinger;
displayid dpy;
sp<imemoryheap>* heap;
uint32_t* w;
uint32_t* h;
pixelformat* f;
uint32_t sw;
uint32_t sh;
status_t result;
public:
messagecapturescreen(surfaceflinger* flinger, displayid dpy,
sp<imemoryheap>* heap, uint32_t* w, uint32_t* h, pixelformat* f,
uint32_t sw, uint32_t sh)
: flinger(flinger), dpy(dpy),
heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), result(permission_denied)
{
}
status_t getresult() const {
logi("getresult()");
return result;
}
virtual bool handler() {
logi("handler()");
mutex::autolock _l(flinger->mstatelock);
// if we have secure windows, never allow the screen capture
if (flinger->msecureframebuffer)
return true;
result = flinger->capturescreenimpllocked(dpy,
heap, w, h, f, sw, sh);
return true;
}
};
logi("before messagecapturescreen");
sp<messagebase> msg = new messagecapturescreen(this,
dpy, heap, width, height, format, sw, sh);
status_t res = postmessagesync(msg);
if (res == no_error) {
res = static_cast<messagecapturescreen*>( msg.get() )->getresult();
}
return res;
}
而这个函数关键又使用了opengl的几个函数去获得图片,然而opengl又去read framebuffer(这是我的理解)。如果你去用jni调用so的方法去截屏的话,就可以把screencap这个文件稍微修改一下然后做成so文件。
主要是补充一下怎么去存放文件与编译吧,当然我说的方法只是我做的方法不代表是很好用的。
存放:在eclipse新建一个android工程,保存后找到这个工程(如screencap)的存放位置 然后把这个文件放到android源代码的development文件里面,然后在你的那个工程文件里面新建一个文件夹,名字叫做jni(这个文件夹平行于src文件夹,screencap/jni),把上面博客提到的那个c++跟mk(screencap/jni/com_android_screencap_screencapnative.cpp和screencap/jni/android.mk)文件放进去,最后在把编译的mk文件放在screencap目录下(screencap/android.mk);
编译:编译是个很伟大的工程,需要你花大量的时间与精力。直接在终端进入工程存放的所在位置,我的是administrator/android.2.3.3/development,然后mm(builds all of the modules in the current directory),如果成功,那么你运气比较好,在终端回提示你apk保存的位置。push进手机试一试。但是往往是不成功的。你可能会遇到一些问题,比如android.permission.access_surface_flinger ,android.permission.read_frame_buffer(因为capturescrren这个函数是surfaceflinger里面的函数,然而surfaceflinger里面的opengl截屏函数会去读取framebuffer),相关源代码是:
复制代码 代码如下:
status_t surfaceflinger::ontransact(
uint32_t code, const parcel& data, parcel* reply, uint32_t flags)
{
switch (code) {
case create_connection:
case open_global_transaction:
case close_global_transaction:
case set_orientation:
case freeze_display:
case unfreeze_display:
case boot_finished:
case turn_electron_beam_off:
case turn_electron_beam_on:
{
// codes that require permission check
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
if ((uid != aid_graphics) && !maccesssurfaceflinger.check(pid, uid)) {
loge("permission denial: "
"can't access surfaceflinger pid=%d, uid=%d", pid, uid);
return permission_denied;
}
break;
}
case capture_screen:
{
// codes that require permission check
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
if ((uid != aid_graphics) && !mreadframebuffer.check(pid, uid)) {
loge("permission denial: "
"can't read framebuffer pid=%d, uid=%d", pid, uid);
return permission_denied;
}
break;
}
}
status_t err = bnsurfacecomposer::ontransact(code, data, reply, flags);
if (err == unknown_transaction || err == permission_denied) {
check_interface(isurfacecomposer, data, reply);
if (unlikely(!mhardwaretest.checkcalling())) {
ipcthreadstate* ipc = ipcthreadstate::self();
const int pid = ipc->getcallingpid();
const int uid = ipc->getcallinguid();
logi("err");
loge("permission denial: "
"can't access surfaceflinger pid=%d, uid=%d", pid, uid);
return permission_denied;
}
int n;
switch (code) {
case 1000: // show_cpu, not supported anymore
case 1001: // show_fps, not supported anymore
return no_error;
case 1002: // show_updates
n = data.readint32();
mdebugregion = n ? n : (mdebugregion ? 0 : 1);
return no_error;
case 1003: // show_background
n = data.readint32();
mdebugbackground = n ? 1 : 0;
return no_error;
case 1004:{ // repaint everything
mutex::autolock _l(mstatelock);
const displayhardware& hw(graphicplane(0).displayhardware());
mdirtyregion.set(hw.bounds()); // careful that's not thread-safe
signalevent();
return no_error;
}
case 1005:{ // force transaction
settransactionflags(etransactionneeded|etraversalneeded);
return no_error;
}
case 1006:{ // enable/disable graphiclog
int enabled = data.readint32();
graphiclog::getinstance().setenabled(enabled);
return no_error;
}
case 1007: // set mfreezecount
mfreezecount = data.readint32();
mfreezedisplaytime = 0;
return no_error;
case 1010: // interrogate.
reply->writeint32(0);
reply->writeint32(0);
reply->writeint32(mdebugregion);
reply->writeint32(mdebugbackground);
return no_error;
case 1013: {
mutex::autolock _l(mstatelock);
const displayhardware& hw(graphicplane(0).displayhardware());
reply->writeint32(hw.getpageflipcount());
}
return no_error;
}
}
return err;
}
这个仅仅只是开始! 你会发现你即使在xml里面添加相应的权限仍然会有这个问题出现,为什么呢?在packagemanger文件里面发现相关代码:
复制代码 代码如下:
int checksignatureslp(signature[] s1, signature[] s2) {
if (s1 == null) {
return s2 == null
? packagemanager.signature_neither_signed
: packagemanager.signature_first_not_signed;
}
if (s2 == null) {
return packagemanager.signature_second_not_signed;
}
hashset<signature> set1 = new hashset<signature>();
for (signature sig : s1) {
set1.add(sig);
}
hashset<signature> set2 = new hashset<signature>();
for (signature sig : s2) {
set2.add(sig);
}
// make sure s2 contains all signatures in s1.
if (set1.equals(set2)) {
return packagemanager.signature_match;
}
return packagemanager.signature_no_match;
}
// check for shared user signatures
if (pkgsetting.shareduser != null && pkgsetting.shareduser.signatures.msignatures != null) {
if (checksignatureslp(pkgsetting.shareduser.signatures.msignatures,
pkg.msignatures) != packagemanager.signature_match) {
slog.e(tag, "package " + pkg.packagename
+ " has no signatures that match those in shared user "
+ pkgsetting.shareduser.name + "; ignoring!");
mlastscanerror = packagemanager.install_failed_shared_user_incompatible;
return false;
}
}
return true;
private boolean verifysignatureslp(packagesetting pkgsetting,
packageparser.package pkg) {
// check for shared user signatures
if (pkgsetting.shareduser != null && pkgsetting.shareduser.signatures.msignatures != null) {
if (checksignatureslp(pkgsetting.shareduser.signatures.msignatures,
pkg.msignatures) != packagemanager.signature_match) {
slog.e(tag, "package " + pkg.packagename
+ " has no signatures that match those in shared user "
+ pkgsetting.shareduser.name + "; ignoring!");
mlastscanerror = packagemanager.install_failed_shared_user_incompatible;
return false;
}
}
return true;
}
你在终端输入adb logcat | grep packagemanager 你会发现这两个权限根本没有赋予给你的apk,我的理解是,程序需要权限,然后apk仍然需要权限。那怎么样给apk赋予权限呢,两个方法,一个是在我上面说的screencap/android.mk里面添加platform一行,然后在回到mm。还有一个方法就是通过sign。这两个方法都是给apk赋予system权限,但是我试过这两种方法,都有问题,就是在adb install的时候会显示签名不兼容,查看源代码会发现uid跟gid不匹配。这些是我这段时间发现的问题,大家有问题可以交流交流。
再说说几个简单的应用层截屏吧,很简单,就是几个函数调用而已
复制代码 代码如下:
view view = getwindow().getdecorview();
display display = this.getwindowmanager().getdefaultdisplay();
view.layout(0, 0, display.getwidth(), display.getheight());
view.setdrawingcacheenabled(true);//允许当前窗口保存缓存信息,这样 getdrawingcache()方法才会返回一个bitmap
bitmap bmp = bitmap.createbitmap(view.getdrawingcache());
我对这个程序的理解就是,它仅仅只能截取当前的activity,也就是说如果你运行这个程序后它就截取你这个程序的当前屏幕的信息。我们假设你做成一个按钮,在点击按钮后sleep5秒再调用这个方法(假设你的activity叫做screen)。当你点击按钮以后,然后你再点击home或者返回按钮,等到5秒后你那个程序就会截取到你当前屏幕?不是!它只会截取那个运行于后台的screen这个activity。
这些只是我的一点小小的总结,而且肯定有不对的地方,希望大家一起来解决截屏的问题!