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()
相关推荐
Dovis(誓平步青云)5 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
alexhilton6 小时前
让架构边界变成可执行的测试
android·kotlin·android jetpack
hai_android7 小时前
SendChannel 与 ReceiveChannel 通信机制
android
数据治理自习室9 小时前
AI 应用评测体系(GraphRAG)
android·大数据·人工智能·kotlin
爱笑鱼10 小时前
Android 系统启动机制(五):system_server 是 init 启动的,还是 Zygote fork 出来的?
android
智购科技无人售货机工厂11 小时前
2026自动售货机防拆机物理安全设计:从安全螺丝到结构互锁的工程实践~YH
android·网络·驱动开发·python·单片机·安全·云原生
开开心心就好11 小时前
电子教鞭工具支持画框写字插图片功能齐全
android·开发语言·前端·javascript·人工智能·pdf·html
Dovis(誓平步青云)11 小时前
拍视频前先把镜头想清楚:做一个分镜取景辅助器
android·java·服务器·javascript·人工智能
峥嵘life12 小时前
Android16 系统 APEX 模块说明
android·大数据·开发语言
2601_9620654913 小时前
PHP For 循环
android·java·php