android后台截屏实现(2)--screencap源码修改
首先找到screencap类在Android源码中的位置,/442/frameworks/base/cmds/screencap/screencap.cpp。
源码如下:
- /*
- * 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 <errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <binder/ProcessState.h>
- #include <gui/SurfaceComposerClient.h>
- #include <gui/ISurfaceComposer.h>
- #include <ui/PixelFormat.h>
- #include <SkImageEncoder.h>
- #include <SkBitmap.h>
- #include <SkData.h>
- #include <SkStream.h>
- using namespace android;
- static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
- static void usage(const char* pname)
- {
- fprintf(stderr,
- "usage: %s [-hp] [-d display-id] [FILENAME]\n"
- " -h: this message\n"
- " -p: save the file as a png.\n"
- " -d: specify the display id to capture, default %d.\n"
- "If FILENAME ends with .png it will be saved as a png.\n"
- "If FILENAME is not given, the results will be printed to stdout.\n",
- pname, DEFAULT_DISPLAY_ID
- );
- }
- static SkBitmap::Config flinger2skia(PixelFormat f)
- {
- switch (f) {
- case PIXEL_FORMAT_RGB_565:
- return SkBitmap::kRGB_565_Config;
- default:
- return SkBitmap::kARGB_8888_Config;
- }
- }
- static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
- uint32_t* bytespp, uint32_t* f)
- {
- switch (vinfo.bits_per_pixel) {
- case 16:
- *f = PIXEL_FORMAT_RGB_565;
- *bytespp = 2;
- break;
- case 24:
- *f = PIXEL_FORMAT_RGB_888;
- *bytespp = 3;
- break;
- case 32:
- // TODO: do better decoding of vinfo here
- *f = PIXEL_FORMAT_RGBX_8888;
- *bytespp = 4;
- break;
- default:
- return BAD_VALUE;
- }
- return NO_ERROR;
- }
- int main(int argc, char** argv)
- {
- ProcessState::self()->startThreadPool();
- const char* pname = argv[0];
- bool png = false;
- int32_t displayId = DEFAULT_DISPLAY_ID;
- int c;
- while ((c = getopt(argc, argv, "phd:")) != -1) {
- switch (c) {
- case 'p':
- png = true;
- break;
- case 'd':
- displayId = atoi(optarg);
- break;
- case '?':
- case 'h':
- usage(pname);
- return 1;
- }
- }
- argc -= optind;
- argv += optind;
- int fd = -1;
- if (argc == 0) {
- fd = dup(STDOUT_FILENO);
- } else if (argc == 1) {
- const char* fn = argv[0];
- fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
- if (fd == -1) {
- fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
- return 1;
- }
- const int len = strlen(fn);
- if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
- png = true;
- }
- }
- if (fd == -1) {
- usage(pname);
- return 1;
- }
- void const* mapbase = MAP_FAILED;
- ssize_t mapsize = -1;
- void const* base = 0;
- uint32_t w, s, h, f;
- size_t size = 0;
- ScreenshotClient screenshot;
- sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
- if (display != NULL && screenshot.update(display) == NO_ERROR) {
- base = screenshot.getPixels();
- w = screenshot.getWidth();
- h = screenshot.getHeight();
- s = screenshot.getStride();
- f = screenshot.getFormat();
- size = screenshot.getSize();
- } else {
- const char* fbpath = "/dev/graphics/fb0";
- int fb = open(fbpath, O_RDONLY);
- if (fb >= 0) {
- struct fb_var_screeninfo vinfo;
- if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
- uint32_t bytespp;
- if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
- size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp;
- w = vinfo.xres;
- h = vinfo.yres;
- s = vinfo.xres;
- size = w*h*bytespp;
- mapsize = offset + size;
- mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
- if (mapbase != MAP_FAILED) {
- base = (void const *)((char const *)mapbase + offset);
- }
- }
- }
- close(fb);
- }
- }
- if (base) {
- if (png) {
- SkBitmap b;
- b.setConfig(flinger2skia(f), w, h, s*bytesPerPixel(f));
- b.setPixels((void*)base);
- SkDynamicMemoryWStream stream;
- SkImageEncoder::EncodeStream(&stream, b,
- SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
- SkData* streamData = stream.copyToData();
- write(fd, streamData->data(), streamData->size());
- streamData->unref();
- } else {
- write(fd, &w, 4);
- write(fd, &h, 4);
- write(fd, &f, 4);
- size_t Bpp = bytesPerPixel(f);
- for (size_t y=0 ; y<h ; y++) {
- write(fd, base, w*Bpp);
- base = (void *)((char *)base + s*Bpp);
- }
- }
- }
- close(fd);
- if (mapbase != MAP_FAILED) {
- munmap((void *)mapbase, mapsize);
- }
- return 0;
- }
由源码可以看出,screencap的入口main方法是从命令行获取参数,通过分析后执行相应的操作。我们要想在java层调用这个类,就要把它的入口改成native方法的接口,修改后的代码如下:
- #include <jni.h>
- #include "com_android_servicescreencap_ScreenCap.h"
- #include <errno.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <binder/ProcessState.h>
- #include <gui/SurfaceComposerClient.h>
- #include <gui/ISurfaceComposer.h>
- #include <ui/PixelFormat.h>
- #include <SkImageEncoder.h>
- #include <SkBitmap.h>
- #include <SkData.h>
- #include <SkStream.h>
- #include <android/log.h>
- #define LOG_TAG "ServiceScreenCap"
- #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
- #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
- #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
- using namespace android;
- static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
- static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
- uint32_t* bytespp, uint32_t* f)
- {
- switch (vinfo.bits_per_pixel) {
- case 16:
- *f = PIXEL_FORMAT_RGB_565;
- *bytespp = 2;
- break;
- case 24:
- *f = PIXEL_FORMAT_RGB_888;
- *bytespp = 3;
- break;
- case 32:
- // TODO: do better decoding of vinfo here
- *f = PIXEL_FORMAT_RGBX_8888;
- *bytespp = 4;
- break;
- default:
- return BAD_VALUE;
- }
- return NO_ERROR;
- }
- static SkBitmap::Config flinger2skia(PixelFormat f)
- {
- switch (f) {
- case PIXEL_FORMAT_RGB_565:
- return SkBitmap::kRGB_565_Config;
- default:
- return SkBitmap::kARGB_8888_Config;
- }
- }
- /*
- * Class: com_android_servicescreencap_ScreenCap
- * Method: currentscreen
- * Signature: (Ljava/lang/String;)I
- */
- JNIEXPORT jint
- JNICALL ScreenCap_currentscreen(JNIEnv *env,
- jclass clazz, jstring jpath) {
- ProcessState::self()->startThreadPool();
- int32_t displayId = DEFAULT_DISPLAY_ID;
- const char* fn = env->GetStringUTFChars(jpath,NULL);
- LOGI("=====jpath:%s \n", fn);
- if (fn == NULL) {
- LOGE("=====path = %s \n =====err: %s \n",fn, strerror(errno));
- return 1;
- }
- int fd = -1;
- fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
- LOGI("=====after open ,fd:%d \n",fd);
- if (fd == -1) {
- LOGE("=====err: %s \n", strerror(errno));
- return 2;
- }
- void const* mapbase = MAP_FAILED;
- ssize_t mapsize = -1;
- void const* base = 0;
- uint32_t w, s, h, f;
- size_t size = 0;
- ScreenshotClient screenshot;
- sp < IBinder > display = SurfaceComposerClient::getBuiltInDisplay(displayId);
- if (display != NULL && screenshot.update(display) == NO_ERROR) {
- base = screenshot.getPixels();
- w = screenshot.getWidth();
- h = screenshot.getHeight();
- s = screenshot.getStride();
- f = screenshot.getFormat();
- size = screenshot.getSize();
- } else {
- const char* fbpath = "/dev/graphics/fb0";
- int fb = open(fbpath, O_RDONLY);
- LOGI("=====read framebuffer, fb:%d \n", fb);
- if (fb >= 0) {
- struct fb_var_screeninfo vinfo;
- if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
- uint32_t bytespp;
- if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
- size_t offset = (vinfo.xoffset + vinfo.yoffset * vinfo.xres)
- * bytespp;
- w = vinfo.xres;
- h = vinfo.yres;
- s = vinfo.xres;
- size = w * h * bytespp;
- mapsize = offset + size;
- mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
- if (mapbase != MAP_FAILED) {
- base = (void const *) ((char const *) mapbase + offset);
- }
- }
- }
- close(fb);
- }else{
- LOGE("=====fb = %d , err: %s \n",fb, strerror(errno));
- return 3;
- }
- }
- if (base) {
- SkBitmap b;
- b.setConfig(flinger2skia(f), w, h, s * bytesPerPixel(f));
- b.setPixels((void*) base);
- SkDynamicMemoryWStream stream;
- SkImageEncoder::EncodeStream(&stream, b, SkImageEncoder::kPNG_Type,
- SkImageEncoder::kDefaultQuality);
- SkData* streamData = stream.copyToData();
- write(fd, streamData->data(), streamData->size());
- streamData->unref();
- }
- close (fd);
- if (mapbase != MAP_FAILED) {
- munmap((void *) mapbase, mapsize);
- }
- return 0;
- }
- static JNINativeMethod methods[] = {
- {"currentscreen","(Ljava/lang/String;)I",(void*)ScreenCap_currentscreen},
- };
- static int registerNativeMethods(JNIEnv* env,const char* classname,JNINativeMethod* gMethods,int numMethods ){
- jclass clazz;
- clazz = env->FindClass(classname);
- if(clazz == NULL){
- return JNI_FALSE;
- }
- if(env->RegisterNatives(clazz,gMethods,numMethods) <0 ){
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- static int registerNatives(JNIEnv* env)
- {
- if (!registerNativeMethods(env, "com/android/servicescreencap/ScreenCap",
- methods, sizeof(methods) / sizeof(methods[0]))) {
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- typedef union {
- JNIEnv* env;
- void* venv;
- } UnionJNIEnvToVoid;
- jint JNI_OnLoad(JavaVM* vm, void* reserved)
- {
- UnionJNIEnvToVoid uenv;
- uenv.venv = NULL;
- jint result = -1;
- JNIEnv* env = NULL;
- if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_6) != JNI_OK) {
- return result;
- }
- env = uenv.env;
- if (registerNatives(env) != JNI_TRUE) {
- return result;
- }
- result = JNI_VERSION_1_6;
- return result;
- }
修改后的代码入口是ScreenCap_currentscreen,该方法接收一个地址,将当前屏幕截取到该地址下。代码中加入了日志,可以打印native层的错误信息。
此处需采用动态方式注册本地方法,静态方式好像会有问题。
参考链接:
Android: How to Capture Screen in Gingerbread(2.3中实现截屏)
Android: How to Capture Screen in Gingerbread(2.3中实现截屏)(续)
Android系统截屏的实现(附代码)
原文地址: http://blog.csdn.net/lingfengxu/article/details/43487793
android后台截屏实现(2)--screencap源码修改相关推荐
- 快捷键自动截屏保存图片工具附源码0积分共享
快捷键自动截图保存在工具所在目录,目前快捷键复用的原来的截屏软件,不影响原来截屏键功能.源码公开可免费下载需要者自取,可以根据需要自行修改.用对话框实现的,但是做成了系统托盘图标,没有界面显示,方便后 ...
- Windows CE/Moblie截屏工具及实现源码
Windows CE/Moblie截屏工具及实现源码 [img]http://dl2.iteye.com/upload/attachment/0084/6997/c72941f2-b86e-3683- ...
- Android后台截屏功能
前言 最近公司领导要求我做一个截屏的功能,说是为了方便监控小屏.本来以为没什么难度,然后就答应了下来.谁知道全都是坑. 这里要说明一点,我这里做的Android程序是 安装在 Android小屏上和机 ...
- android 后台截屏代码,Android实现截图和分享功能的代码
先给大家展示下效果图吧 直接上代码: xml的布局: android:id="@+id/btn_jp" android:layout_marginTop="10dip&q ...
- android后台截屏实现(3)--编译screencap
修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...
- 一键锁屏源代码c语言,Android一键锁屏开发全过程【源码】【附图】
一.项目简介: 项目:<Android 一键锁屏> 开发周期:4天 代码量:100行 ------------------------ 二.项目流程: 三.项目代码 1.主程序代码: Ja ...
- Android开发WebView之加载HTML源码修改HTML字体大小以及缩放HTML的方法
老套路上图: 再看下缩放功能: 先说下设置HTML网页字体大小的方法: //设置网页字体大小webview.getSettings().setTextSize(WebSettings.TextSize ...
- 【源码解读】Screencap源码分析-基础篇
本文期望达到的目的: 了解screencap使用 了解screencap实现基础原理 为后续screencap源码修改和其他应用做准备 源码位置: android4.0之后内置了截图工具screenc ...
- android截屏功能实现方式汇总【包括后台截屏】
前言 对于android实现截屏功能,简单讲述一下可行的方法和之间的利弊 使用canvas View v = getWindow().getDecorView(); Bitmap bitmap = B ...
最新文章
- linux pps 包 网卡,linux下安装PPS
- tfs连不上团队资源管理器问题
- css实现横向进度条和竖向进度条
- 二叉树链表结构表示法
- python文件操作模式是什么,python --文件操作模式详解
- sap模块介绍_小迈说|SAP究竟有多少模块?
- 设计模式之 - 代理模式(Proxy Pattern)
- 常用而又不为大多数人所知的三种网页特效
- python opencv用法中文教程
- C# —— IEnumerator和foreach
- Chrome , Firfox 对应IE fireEvent 的方法
- ubuntu 一张网卡绑定多个ip
- 微信配置JS接口安全域名-Nginx配置 文件的配置
- Java 設計模式 - 適配器模式
- 微信小程序开发时,下载node.js安装后出错误怎么办
- 克鲁格曼《微观经济学》第2版课后习题答案
- input:-webkit-autofill
- C语言写一个函数,可以逆序一个字符串的内容。
- Java实现编辑图片并添加文字
- 疫情错峰云计算冲锋,给Zoom带来了多少溢价?