当前位置: 首页 > news >正文

安卓截屏;前台服务

  private var mediaProjectionManager: MediaProjectionManager? = nullval REQUEST_MEDIA_PROJECTION = 10001private var isstartservice = true//启动MediaService服务fun startMediaService() {if (isstartservice) {startService(Intent(this, MediaService::class.java))isstartservice = false}}//停止MediaService服务fun stopMediaService(context: Context) {val intent = Intent(context, MediaService::class.java)context.stopService(intent)}// 申请截屏权限private fun getScreenShotPower() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {mediaProjectionManager =getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManagerif (mediaProjectionManager != null) {val intent = mediaProjectionManager!!.createScreenCaptureIntent()startActivityForResult(intent, REQUEST_MEDIA_PROJECTION)}}}@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {super.onActivityResult(requestCode, resultCode, data)if (requestCode == REQUEST_MEDIA_PROJECTION && data != null) {
//            mediaProjection = mediaProjectionManager!!.getMediaProjection(RESULT_OK, data)val mediaPro = mediaProjectionManager!!.getMediaProjection(RESULT_OK, data)val bitmap = screenShot(mediaPro)      //截屏val bs = getBitmapByte(bitmap)//对图片进行处理逻辑  例如可以保存本地、进行图片分享等等操作Log.e("WWW", "EEE" + bitmap)if (bitmap != null) {if (typeShare != -1) {if (typeShare == 0) {/*** 微信分享*/shareWechat(bitmap)} else if (typeShare == 1) {/*** 保存到本地相册*/bitmap?.let { it1 -> ImageSaver.saveBitmapToGallery(this, it1, "-", "+++") }stopMediaService(this)}}}//            startRecord()    //三:录屏
//            if (!isStart) {
//                isStart = !isStart
//                startPlayRoute()
//                multiple = 150f
//            } else {
//                multiple = 150f
//            }
//            isShowShareTipsPop = true}}/*** 分享到微信* @param view View*/private fun shareWechat(view: Bitmap?) {
//        var bmp: Bitmap? = loadBitmapFromViewBySystem(view)val imgObj = WXImageObject(view)val msg = WXMediaMessage()msg.mediaObject = imgObjval thumbBmp = Bitmap.createScaledBitmap(view!!,150,150,true)msg.thumbData = bmpToByteArray(thumbBmp, true)val req = SendMessageToWX.Req()req.transaction = buildTransaction("img")req.message = msgreq.scene = SendMessageToWX.Req.WXSceneSession // 分享到对话框api!!.sendReq(req)stopMediaService(this)}@RequiresApi(api = Build.VERSION_CODES.KITKAT)fun screenShot(mediaProjection: MediaProjection): Bitmap? {val wm1 = this.windowManagerval width = wm1.defaultDisplay.widthval height = wm1.defaultDisplay.heightObjects.requireNonNull(mediaProjection)@SuppressLint("WrongConstant") val imageReader: ImageReader =ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 60)var virtualDisplay: VirtualDisplay? = nullif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {virtualDisplay = mediaProjection.createVirtualDisplay("screen",width,height,1,DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,imageReader.getSurface(),null,null)}SystemClock.sleep(1000)//取最新的图片val image: Image = imageReader.acquireLatestImage()// Image image = imageReader.acquireNextImage();//释放 virtualDisplay,不释放会报错virtualDisplay!!.release()return image2Bitmap(image)}// 位图转 Byteprivate fun getBitmapByte(bitmap: Bitmap?): ByteArray {val out = ByteArrayOutputStream()// 参数1转换类型,参数2压缩质量,参数3字节流资源bitmap!!.compress(Bitmap.CompressFormat.PNG, 100, out)try {out.flush()out.close()} catch (e: IOException) {e.printStackTrace()}return out.toByteArray()}@RequiresApi(api = Build.VERSION_CODES.KITKAT)fun image2Bitmap(image: Image?): Bitmap? {if (image == null) {println("image 为空")return null}val width: Int = image.getWidth()val height: Int = image.getHeight()val planes: Array<Image.Plane> = image.planesval buffer: ByteBuffer = planes[0].bufferval pixelStride: Int = planes[0].pixelStrideval rowStride: Int = planes[0].rowStrideval rowPadding = rowStride - pixelStride * widthval bitmap =Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888)bitmap.copyPixelsFromBuffer(buffer)//截取图片// Bitmap cutBitmap = Bitmap.createBitmap(bitmap,0,0,width/2,height/2);//压缩图片// Matrix matrix = new Matrix();// matrix.setScale(0.5F, 0.5F);// System.out.println(bitmap.isMutable());// bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);image.close()return bitmap}

package com.allynav.iefa.service

import android.R
import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
/**
*@author zd
*@time 2023/4/10 8:55
*@description : 截屏前台服务
*
**/

class MediaService : Service() {private val NOTIFICATION_CHANNEL_ID = "com.tencent.trtc.apiexample.MediaService"private val NOTIFICATION_CHANNEL_NAME = "com.tencent.trtc.apiexample.channel_name"private val NOTIFICATION_CHANNEL_DESC = "com.tencent.trtc.apiexample.channel_desc"override fun onCreate() {super.onCreate()startNotification()}fun startNotification() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Call Start foreground with notificationval notificationIntent = Intent(this, MediaService::class.java)val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.alert_dark_frame)).setSmallIcon(R.drawable.alert_dark_frame).setContentTitle("Starting Service").setContentText("Starting monitoring service").setContentIntent(pendingIntent)val notification: Notification = notificationBuilder.build()val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_NAME,NotificationManager.IMPORTANCE_DEFAULT)channel.description = NOTIFICATION_CHANNEL_DESCval notificationManager =getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManagernotificationManager.createNotificationChannel(channel)startForeground(1,notification) //必须使用此方法显示通知,不能使用notificationManager.notify,否则还是会报上面的错误}}override fun onBind(intent: Intent?): IBinder {throw UnsupportedOperationException("Not yet implemented")}
}

相关文章:

安卓截屏;前台服务

private var mediaProjectionManager: MediaProjectionManager? nullval REQUEST_MEDIA_PROJECTION 10001private var isstartservice true//启动MediaService服务fun startMediaService() {if (isstartservice) {startService(Intent(this, MediaService::class.java))iss…...

C++ PrimerPlus 复习 第八章 函数探幽

第一章 命令编译链接文件 make文件 第二章 进入c 第三章 处理数据 第四章 复合类型 &#xff08;上&#xff09; 第四章 复合类型 &#xff08;下&#xff09; 第五章 循环和关系表达式 第六章 分支语句和逻辑运算符 第七章 函数——C的编程模块&#xff08;上&#xff…...

JavaScript-Ajax-axios-Xhr

JS的异步请求 主要有xhr xmlHttpRequest 以及axios 下面给出代码以及详细用法&#xff0c;都写在了注释里 直接拿去用即可 测试中默认的密码为123456 账号admin 其他一律返回登录失败 代码实例 <!DOCTYPE html> <html lang"en"> <head><…...

怎样查看kafka写数据送到topic是否成功

要查看 Kafka 写数据是否成功送到主题&#xff08;topic&#xff09;&#xff0c;可以通过以下几种方法来进行确认&#xff1a; Kafka 生产者确认机制&#xff1a;Kafka 提供了生产者的确认机制&#xff0c;您可以在创建生产者时设置 acks 属性来控制确认级别。常见的确认级别包…...

腾讯mini项目-【指标监控服务重构】2023-08-16

今日已办 v1 验证 StageHandler 在处理消息时是否为单例&#xff0c;【错误尝试】 type StageHandler struct { }func (s StageHandler) Middleware1(h message.HandlerFunc) message.HandlerFunc {return func(msg *message.Message) ([]*message.Message, error) {log.Log…...

PTA:7-3 两个递增链表的差集

^两个递增链表的差集 题目输入样例输出样例 代码 题目 输入样例 5 1 3 5 7 9 3 2 3 5输出样例 3 1 7 9代码 #include <iostream> #include <list> #include <unordered_set> using namespace std; int main() {int n1, n2;cin >> n1;list<int&g…...

智能合约漏洞案例,DEI 漏洞复现

智能合约漏洞案例&#xff0c;DEI 漏洞复现 1. 漏洞简介 https://twitter.com/eugenioclrc/status/1654576296507088906 2. 相关地址或交易 https://explorer.phalcon.xyz/tx/arbitrum/0xb1141785b7b94eb37c39c37f0272744c6e79ca1517529fec3f4af59d4c3c37ef 攻击交易 3. …...

Attention is all you need 论文笔记

该论文引入Transformer&#xff0c;主要核心是自注意力机制&#xff0c;自注意力&#xff08;Self-Attention&#xff09;机制是一种可以考虑输入序列中所有位置信息的机制。 RNN介绍 引入RNN为了更好的处理序列信息&#xff0c;比如我 吃 苹果&#xff0c;前后的输入之间是有…...

Hdoop伪分布式集群搭建

文章目录 Hadoop安装部署前言1.环境2.步骤3.效果图 具体步骤&#xff08;一&#xff09;前期准备&#xff08;1&#xff09;ping外网&#xff08;2&#xff09;配置主机名&#xff08;3&#xff09;配置时钟同步&#xff08;4&#xff09;关闭防火墙 &#xff08;二&#xff09…...

java临时文件

临时文件 有时候&#xff0c;我们程序运行时需要产生中间文件&#xff0c;但是这些文件只是临时用途&#xff0c;并不做长久保存。 我们可以使用临时文件&#xff0c;不需要长久保存。 public static File createTempFile(String prefix, String suffix)prefix 前缀 suffix …...

C++中的<string>头文件 和 <cstring>头文件简介

C中的<string>头文件 和 <cstring>头文件简介 在C中<string> 和 <cstring> 是两个不同的头文件。 <string> 是C标准库中的头文件&#xff0c;定义了一个名为std::string的类&#xff0c;提供了对字符串的操作如size()、length()、empty() 及字…...

安装MySQL

Centos7下安装MySQL详细步骤_centos7安装mysql教程_欢欢李的博客-CSDN博客...

输入学生成绩,函数返回最大元素的数组下标,求最高分学生成绩(输入负数表示输入结束)

scanfscore()函数用于输入学生的成绩 int scanfscore(int score[N])//输入学生的成绩 {int i -1;do {i;printf("输入学生成绩:");scanf("%d", &score[i]);} while (score[i] > 0);return i; } findmax()用于寻找最大值 int findmax(int score[N…...

常用音频接口:TDM,PDM,I2S,PCM

常用音频接口&#xff1a;TDM&#xff0c;PDM&#xff0c;I2S&#xff0c;PCM_tdm音频_沙漠的甲壳虫的博客-CSDN博客 I2S/PCM接口及音频codec_音频pcm接口模块设计-CSDN博客 2个TDM8功放调试ing_周龙(AI湖湘学派)的博客-CSDN博客 数字音频接口时序----IIS、TDM、PCM、PDM_td…...

git clone报错Failed to connect to github.com port 443 after 21055 ms:

git 设置代理端口号 git config --global http.proxy http://127.0.0.1:10085 和 git config --global https.proxy http://127.0.0.1:10085 然后就可以成功git clone hugging face的数据集了 如果是https://huggingface.co/datasets/shibing624/medical/tree/main 那么…...

【操作系统】深入浅出死锁问题

死锁的概念 在多线程编程中&#xff0c;我们为了防止多线程竞争共享资源而导致数据错乱&#xff0c;都会在操作共享资源而导致数据错乱&#xff0c;都会在操作共享资源之前加上互斥锁&#xff0c;只有成功获得到锁的线程&#xff0c;才能操作共享资源&#xff0c;获取不到锁的…...

springboot实现webSocket服务端和客户端demo

1&#xff1a;pom导入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId><version>2.2.7.RELEASE</version></dependency>2&#xff1a;myWebSocketClien…...

代码走读: FFMPEG-ffplayer02

AVFrame int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame) 选取一个音频解码器 和 一个视频解码器分别介绍该解码器功能 音频G722 g722dec.c -> g722_decode_frame 通过 ff_get_buffer 给 传入的 frame 指针分配内存 g722_decode_…...

【数据结构】——排序算法的相关习题

目录 一、选择题题型一 &#xff08;插入排序&#xff09;1、直接插入排序2、折半插入排序3、希尔排序 题型二&#xff08;交换排序&#xff09;1、冒泡排序2、快速排序 题型三&#xff08;选择排序&#xff09;1、简单选择排序~2、堆排序 ~题型四&#xff08;归并排序&#xf…...

C高级day5(Makefile)

一、Xmind整理&#xff1a; 二、上课笔记整理&#xff1a; 1.#----->把带参宏的参数替换成字符串 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX(a,b) a>b?a:b #define STR(n) #n int main(int argc, const char *argv…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)

可以使用Sqliteviz这个网站免费编写sql语句&#xff0c;它能够让用户直接在浏览器内练习SQL的语法&#xff0c;不需要安装任何软件。 链接如下&#xff1a; sqliteviz 注意&#xff1a; 在转写SQL语法时&#xff0c;关键字之间有一个特定的顺序&#xff0c;这个顺序会影响到…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?

uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件&#xff0c;用于在原生应用中加载 HTML 页面&#xff1a; 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...

Pinocchio 库详解及其在足式机器人上的应用

Pinocchio 库详解及其在足式机器人上的应用 Pinocchio (Pinocchio is not only a nose) 是一个开源的 C 库&#xff0c;专门用于快速计算机器人模型的正向运动学、逆向运动学、雅可比矩阵、动力学和动力学导数。它主要关注效率和准确性&#xff0c;并提供了一个通用的框架&…...

回溯算法学习

一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

【 java 虚拟机知识 第一篇 】

目录 1.内存模型 1.1.JVM内存模型的介绍 1.2.堆和栈的区别 1.3.栈的存储细节 1.4.堆的部分 1.5.程序计数器的作用 1.6.方法区的内容 1.7.字符串池 1.8.引用类型 1.9.内存泄漏与内存溢出 1.10.会出现内存溢出的结构 1.内存模型 1.1.JVM内存模型的介绍 内存模型主要分…...

【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案

目录 使用 erase 返回值继续迭代使用索引进行遍历 我们知道类似 vector 的顺序迭代器被删除后&#xff0c;迭代器会失效&#xff0c;因为顺序迭代器在内存中是连续存储的&#xff0c;元素删除后&#xff0c;后续元素会前移。 但一些场景中&#xff0c;我们又需要在执行删除操作…...

windows系统MySQL安装文档

概览&#xff1a;本文讨论了MySQL的安装、使用过程中涉及的解压、配置、初始化、注册服务、启动、修改密码、登录、退出以及卸载等相关内容&#xff0c;为学习者提供全面的操作指导。关键要点包括&#xff1a; 解压 &#xff1a;下载完成后解压压缩包&#xff0c;得到MySQL 8.…...