android 11以上 截图工具类

android 11以上 截图工具类

以上方法,需要系统权限才能使用,而且必须集成系统的API才能使用。并非常规应用可以使用的。需要重要区分。例如:负一屏截图等。

工具类

kotlin 复制代码
package com.ivi.system.utils


import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.os.ServiceManager
import android.util.Log
import android.view.Display
import android.view.IWindowManager
import android.view.WindowManager
import android.view.WindowMetrics
import android.window.ScreenCapture
import com.ivi.base.utils.LogUtil


object ScreenShotUtil {
    private const val TAG = "ScreenShotUtil"
    private const val WIDTH = 1920
    private const val HEIGHT = 1080

    private var bmp: Bitmap? = null
    private var sourceCrop: Rect? = null
    private var captureArgs: ScreenCapture.CaptureArgs? = null

    private var windowManager: WindowManager? = null
    private var iWindowManager: IWindowManager? = null

    private var display: Display? = null

    private var mContext: Context? = null

    fun init(context: Context) {
        mContext = context
        windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

        iWindowManager = IWindowManager.Stub.asInterface(
            ServiceManager.getServiceOrThrow(Context.WINDOW_SERVICE)
        )

        val windowMetrics: WindowMetrics? = windowManager?.currentWindowMetrics

        display = context.display

        var width = windowMetrics?.bounds?.width()

        var height = windowMetrics?.bounds?.height()

        if (width == null)
            width = WIDTH
        if (height == null)
            height = HEIGHT

        sourceCrop = Rect(0, 0, width, height)
        captureArgs = ScreenCapture.CaptureArgs.Builder()
            .setSourceCrop(sourceCrop)
            .build()

    }

    fun screenshot(): Bitmap? {
        LogUtil.i(TAG, "screenshot: ")
        try {

            Log.d(TAG, "screenshot: start")

            val syncScreenCapture =
                ScreenCapture.createSyncCaptureListener() as ScreenCapture.SynchronousScreenCaptureListener
            display?.displayId?.let {
                iWindowManager?.captureDisplay(
                    it,
                    captureArgs,
                    syncScreenCapture
                )
            }

            val buffer: ScreenCapture.ScreenshotHardwareBuffer? = syncScreenCapture.buffer

            bmp = buffer?.asBitmap()

            Log.d(TAG, "screenshot: end")
            return bmp

        } catch (e: Exception) {
            LogUtil.e(TAG, "screenshot: Exception = $e")
        }
        return null
    }

    fun destroy() {
        windowManager = null
        iWindowManager = null
        display = null
    }

}

使用方法

kotlin 复制代码
ScreenShotUtil.init(context)
val bp = ScreenShotUtil.screenshot()
相关推荐
故渊at1 小时前
第二板块:Android 四大组件标准化学理 | 第八篇:Service 后台执行实体与优先级
android·gitee·service·前台服务·后台服务
会Tk矩阵群控的小木1 小时前
安卓群控系统对于游戏工作室实战教程
android·运维·游戏·adb·开源软件·个人开发
qeen872 小时前
【C++】类与对象之类的默认成员函数(二)
android·c语言·开发语言·c++·笔记·学习
故渊at2 小时前
第二板块:Android 四大组件标准化学理 | 第九篇:BroadcastReceiver 事件分发与有序广播
android·gitee·broadcast·广播·动态注册·静态注册
JohnnyDeng943 小时前
【Android】Room 数据库高级用法与性能调优:从查询瓶颈到毫秒级响应
android·性能优化·kotlin·room
zeqinjie3 小时前
Flutter 折叠屏 iPad / 宽屏适配实践
android·前端·flutter
ab_dg_dp3 小时前
Android 17+ 提取 AIDL 生成 Java 文件的实用脚本
android·java·python
Arrom4 小时前
DLNA 渲染端排障实战:从 20s 卡顿到 stale subscriber 的两周追凶之旅
android·java
_李小白4 小时前
【android opencv学习笔记】Day 32:直线检测之霍夫变换
android·opencv·学习