android后台获取当前屏幕截图(screencap.cpp修改)

2023-11-07

    本文基于android6.0。首先找到screencap在Android源码中的位置,若不清楚,可以通过在android目录下通过命令find . -namescreencap.cpp。本文直接给出路径/android/frameworks/base/cmds/screencap/screencap.cpp。入口函数为main,只要编译就可以直接使用。如果需要将数据传出来,需要利用socket,后面会更新,将当前屏幕信息实时传输到web端显示。(data->data(), data->size()是需求的数据和大小)。

screencap.cpp源码如下:

 

<pre name="code" class="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 <stdlib.h>
#include <string.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/DisplayInfo.h>
#include <ui/PixelFormat.h>

// TODO: Fix Skia.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <SkImageEncoder.h>
#include <SkData.h>
#pragma GCC diagnostic pop

using namespace android;

static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;//eDisplayIdMain = 0

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 SkColorType flinger2skia(PixelFormat f)
{
    switch (f) {
        case PIXEL_FORMAT_RGB_565:
            return kRGB_565_SkColorType;
        default:
            return kN32_SkColorType;
    }
}

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 status_t notifyMediaScanner(const char* fileName) {
    String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
    String8 fileUrl("\"");
    fileUrl.append(fileName);
    fileUrl.append("\"");
    cmd.append(fileName);
    cmd.append(" > /dev/null");
    int result = system(cmd.string());
    if (result < 0) {
        fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
        return UNKNOWN_ERROR;
    }
    return NO_ERROR;
}

int main(int argc, char** argv)
{
    //while(1){
        //创建ProcessState 当前进程属性,启动进程的线程池
    ProcessState::self()->startThreadPool();
    
    const char* pname = argv[0];
    bool png = false;
    int32_t displayId = DEFAULT_DISPLAY_ID;//DEFAULT_DISPLAY_ID = eDisplayIdMain = 0,
    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;//optind = 1
    argv += optind;

    struct timeval tv;  //超时时间设置
    while(1){
        gettimeofday(&tv,NULL);
        ALOGE("tv.tv_sec = %ld ,tv.tv_usec = %ld\n", tv.tv_sec, tv.tv_usec);
        int fd = -1;
        const char* fn = NULL;
        if (argc == 0) {
            fd = dup(STDOUT_FILENO);//STDOUT_FILENO=1
        } else if (argc == 1) {
            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 = NULL;
    uint32_t w, s, h, f;
    size_t size = 0;
    // Maps orientations from DisplayInfo to ISurfaceComposer
    static const uint32_t ORIENTATION_MAP[] = {
        ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0
        ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90
        ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180
        ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270
    };
//------------------------------------------------------------------------------------
    ScreenshotClient screenshot;
    sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
    if (display == NULL) {
        fprintf(stderr, "Unable to get handle for display %d\n", displayId);
        return 1;
    }

    Vector<DisplayInfo> configs;
    SurfaceComposerClient::getDisplayConfigs(display, &configs);
    int activeConfig = SurfaceComposerClient::getActiveConfig(display);
    if (static_cast<size_t>(activeConfig) >= configs.size()) {
        fprintf(stderr, "Active config %d not inside configs (size %zu)\n",
                activeConfig, configs.size());
        return 1;
    }
    uint8_t displayOrientation = configs[activeConfig].orientation;
    uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
    status_t result = screenshot.update(display, Rect(), 0, 0, 0, -1U,
            false, captureOrientation);
    if (result == 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 != NULL) {
        if (png) {
            const SkImageInfo info = SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType);
            SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(info, base, s*bytesPerPixel(f),
                    SkImageEncoder::kJPEG_Type, SkImageEncoder::kDefaultQuality));
            if (data.get()) {
                <span style="color:#FF0000;">FILE *fp;  
                if((fp=fopen("/data/audioBuffer.txt","a+"))==NULL) {  
                    ALOGE("00000000000000000\n");   
                }  
                else {         
                    fwrite(data->data(),data->size(),1, fp);
                    ALOGE("data->size() = %zu\n",data->size()); 
                    
                } 
                fclose(fp);</span>
                write(fd, data->data(), data->size()); //data->data(), data->size()可以将数据传出去实时录制当前屏幕
            }
            if (fn != NULL) {
                notifyMediaScanner(fn);
            }
        } 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;
}

 

 

 

修改后进行编译:

 

 

 

source build/envsetup.sh

lunch 6

make -j8

 

运行模拟器:emulator -sdcard /home/bruceking90/Documents/workplace/sdcard.img &

 

之后进行截屏:adb shell screencap -p | sed 's/\r$//' > screen.png

此命令可以直接将png存放在android即当前目录下。

或者adb shell screencap -p /sdcard/screen.png

 

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android后台获取当前屏幕截图(screencap.cpp修改) 的相关文章

随机推荐

  • CUBEMX完成初始化

    目录 软件包下载 选择芯片类型 引脚选择 时钟设置 文件生成路径 设置为代码自动更新 打开文件运行 流水灯 软件包下载 选择芯片类型 引脚选择 三个引脚都选择为推挽输出模式 时钟设置 文件生成路径 中文路径下需要手动安装启动文件 设置为代码
  • mmpose关键点(四):优化关键点模型(原理与代码讲解,持续更新)

    在工程中 模型的运行速度与精度是同样重要的 本文中 我会运用不同的方法去优化比较模型的性能 希望能给大家带来一些实用的trick与经验 有关键点检测相关经验的同学应该知道 关键点主流方法分为Heatmap based与Regression
  • openwrt第三方插件库及插件包安装方法及名称对照表

    openwrt第三方插件库及插件包安装方法及名称对照表 把openwrt packages与small仓库重新归类 ssr passwall vssr以及依赖合并small 喜欢追新的可以去下载small package 该仓库每天自动同步
  • 实现isPrime()函数,参数为整数,要有异常处理,如果是质数返回True,否则返回False

    实现isPrime 函数 参数为整数 要有异常处理 如果是质数返回True 否则返回False def isPrime n if n lt 2 return False else for i in range 2 int pow n 0 5
  • (数据结构)循环队列操作——C实现

    队列 特点 1 先进先出 2 对队列的操作可以理解为头删 尾插 在这里我们主要实现循环队列 循环队列的结构设计 typedef struct Que Elemtype data 存储空间 int front int rear SeQueue
  • 操作系统笔记七(Linux文件操作与文件系统)

    1 Linux的文件基本操作 1 1Linux目录 Linux也有一个目录树 如下图 绝对路径 是从盘符开始的路径 形如C windows system32 cmd exe 相对路径 是从当前路径开始的路径 假如当前路径为C windows
  • 前言-如何学习区块链

    最新内容会更新在主站深入浅出区块链社区 原文链接 前言 如何学习区块链 摘要 区块链未来3到5年应该会出现行业井喷式发展 相应所需的人才必定水涨船高 每一个开发人员都不应该错过这样的机会 区块链涉及的技术很多 很多开发人员看了一些资料后 感
  • C语言常见问题(10):Sections of code should not be commented out

    注释掉的代码及时清理掉 让函数的body干净清洁 不要杂草丛生
  • clang: error: no such file or directory:xxx的处理方法

    经常会遇到这个问题 后来看了一下 大概是在编程的适合改变了工程目录结构 在编译的时候无法找到相应的文件结构 说句话白话 编译器是傻的 你小心或者不小心 结果文件的结果和以前不一样了 编译器是不知情的 还按照之前的方式进行文件之间的链接 所以
  • JavaScript面试题看这一篇就够了,简单全面一发入魂(持续更新 step1)

    目录 1 BOM DOM有什么区别 1 DOM 2 BOM 2 JavaScript中代码后加不加分号 有什么区别 3 var与let const有什么区别 4 原始值和引用值有什么区别 5 什么是执行上下文 6 解释一下JavaScrip
  • hive的高级分组

    1 with cube 2的n次方 使用场景 维度字段之间无关系 底层分组实现 可以实现hive多个任意维度的查询 cube a b c 则首先会对 a b c 进行group by 然后依次是 a b a c a b c b c 最后在对
  • 「已解决」 iTunes 由于卸载 Apple Software Upadate 失败的问题。

    已解决 iTunes 由于卸载 Apple Software Upadate 失败的问题 官方给出的解决方案是 相信大家都会卡在第 2 个步骤上面 卸载 Apple Software Update 时发生出错 这里给出几个方案 方案 1 将
  • 广电大数据用户画像及营销推荐策略(四)——Python实现

    本次大数据项目数据及分析均做脱敏化和保密化 主要分享思路体系 全程用Python实现 数据和代码均不提供 如有建议欢迎讨论 4 模型构建 在实际应用中 构造推荐系统时 并不是采用单一的某种推荐方法进行推荐 为了实现较好的推荐效果 大部分都将
  • 读书思考:步步惊心的《技术陷阱》

    技术陷阱 这本书450页 43万字之巨 信息量密密麻麻 采集的资料极其丰富 复习了一遍大停滞 大分流 大平衡 大逆转时代 并展望未来 看完了有很多想法 随手写了下来 希望不是蹭热点 一 时间折半加速 忽略人类起源到中世纪的万年大停滞 175
  • unity 调用 .dll 或 .so时遇到的问题

    1 32位的 dll 无法在64位的unity编辑器下运行 System DllNotFoundException xxx 64位的程序运行32位的dll是会报这种错 2 Failed to load Assets Plugins xxx
  • 个人网站实现微信扫码登录

    个人网站实现微信扫码登录 效果图 外链图片转存失败 源站可能有防盗链机制 建议将图片保存下来直接上传 img kzSrNgiv 1685034480658 https img ggball top picGo 动画 gif 开发背景 为什么
  • javascript canvas 模拟mac最小化

    文章是别人写的 不是我自己的 链接忘记了
  • 【用JS自制表格软件玩数据】4. 行列计数器的实现

    渲染单元格 列计数器 原理 步进器 字符的运算表 源码 当写完本系列后 我会把源代码分享出来给大家 本课程也会持续更新与矫正 欢迎留言指正 前面已经设计了一个底层渲染类 现在准备在这个底层类的基础上 构建一个渲染单元格的模块 列计数器 通常
  • 去中心化时代的创作者经济

    所谓创作者经济 具体是指利用各种互联网工具 由个人或团体进行内容创作 分发及一系列与创作者相关服务下产生的经济收益 这一概念也主要在当前的 web2互联网时代 并且有很多鲜明的案例凸显出了创作者经济的强大潜力 像我们熟知的抖音 哔哩哔哩等都
  • android后台获取当前屏幕截图(screencap.cpp修改)

    本文基于android6 0 首先找到screencap在Android源码中的位置 若不清楚 可以通过在android目录下通过命令find namescreencap cpp 本文直接给出路径 android frameworks ba