当前位置: 首页 > 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…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)

说明&#xff1a; 想象一下&#xff0c;你正在用eNSP搭建一个虚拟的网络世界&#xff0c;里面有虚拟的路由器、交换机、电脑&#xff08;PC&#xff09;等等。这些设备都在你的电脑里面“运行”&#xff0c;它们之间可以互相通信&#xff0c;就像一个封闭的小王国。 但是&#…...

什么是EULA和DPA

文章目录 EULA&#xff08;End User License Agreement&#xff09;DPA&#xff08;Data Protection Agreement&#xff09;一、定义与背景二、核心内容三、法律效力与责任四、实际应用与意义 EULA&#xff08;End User License Agreement&#xff09; 定义&#xff1a; EULA即…...

在WSL2的Ubuntu镜像中安装Docker

Docker官网链接: https://docs.docker.com/engine/install/ubuntu/ 1、运行以下命令卸载所有冲突的软件包&#xff1a; for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done2、设置Docker…...

人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式

今天是关于AI如何在教学中增强学生的学习体验&#xff0c;我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育&#xff0c;这并非炒作&#xff0c;而是已经发生的巨大变革。教育机构和教育者不能忽视它&#xff0c;试图简单地禁止学生使…...

站群服务器的应用场景都有哪些?

站群服务器主要是为了多个网站的托管和管理所设计的&#xff0c;可以通过集中管理和高效资源的分配&#xff0c;来支持多个独立的网站同时运行&#xff0c;让每一个网站都可以分配到独立的IP地址&#xff0c;避免出现IP关联的风险&#xff0c;用户还可以通过控制面板进行管理功…...

Razor编程中@Html的方法使用大全

文章目录 1. 基础HTML辅助方法1.1 Html.ActionLink()1.2 Html.RouteLink()1.3 Html.Display() / Html.DisplayFor()1.4 Html.Editor() / Html.EditorFor()1.5 Html.Label() / Html.LabelFor()1.6 Html.TextBox() / Html.TextBoxFor() 2. 表单相关辅助方法2.1 Html.BeginForm() …...

华为OD最新机试真题-数组组成的最小数字-OD统一考试(B卷)

题目描述 给定一个整型数组,请从该数组中选择3个元素 组成最小数字并输出 (如果数组长度小于3,则选择数组中所有元素来组成最小数字)。 输入描述 行用半角逗号分割的字符串记录的整型数组,0<数组长度<= 100,0<整数的取值范围<= 10000。 输出描述 由3个元素组成…...

k8s从入门到放弃之HPA控制器

k8s从入门到放弃之HPA控制器 Kubernetes中的Horizontal Pod Autoscaler (HPA)控制器是一种用于自动扩展部署、副本集或复制控制器中Pod数量的机制。它可以根据观察到的CPU利用率&#xff08;或其他自定义指标&#xff09;来调整这些对象的规模&#xff0c;从而帮助应用程序在负…...

ui框架-文件列表展示

ui框架-文件列表展示 介绍 UI框架的文件列表展示组件&#xff0c;可以展示文件夹&#xff0c;支持列表展示和图标展示模式。组件提供了丰富的功能和可配置选项&#xff0c;适用于文件管理、文件上传等场景。 功能特性 支持列表模式和网格模式的切换展示支持文件和文件夹的层…...

js 设置3秒后执行

如何在JavaScript中延迟3秒执行操作 在JavaScript中&#xff0c;要设置一个操作在指定延迟后&#xff08;例如3秒&#xff09;执行&#xff0c;可以使用 setTimeout 函数。setTimeout 是JavaScript的核心计时器方法&#xff0c;它接受两个参数&#xff1a; 要执行的函数&…...