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()
相关推荐
粤M温同学2 小时前
Android Studio 中安装 CodeBuddy AI助手
android·ide·android studio
阿拉斯攀登2 小时前
【RK3576 安卓 JNI/NDK 系列 08】RK3576 实战(二):JNI 调用 I2C 驱动读取传感器数据
android·安卓ndk入门·jni方法签名·java调用c++·rk3576底层开发·rk3576 i2c开发
赶路人儿4 小时前
常见的mcp配置
android·adb
符哥20084 小时前
充电桩 WiFi 局域网配网(Android/Kotlin)流程、指令及实例说明文档
android·开发语言·kotlin
没有了遇见5 小时前
Android 项目架构之<用户信息模块>
android
Georgewu6 小时前
如何判断应用在鸿蒙卓易通或者出境易环境下?
android·harmonyos
localbob7 小时前
Pico 4XVR 1.10.13安装包下载与安装教程 ico 4XVR最新版下载、4XVR 1.10.13 APK安装包、Pico VR看电影软件、4XVR完整版安装教程、Pico 4播放器推荐、V
android·vr·vr播放器·vr眼镜播放器下载·pico 4xvr·4xvr下载·pico 4xvr最新版安装包
峥嵘life7 小时前
Android16 EDLA【CTS】CtsConnectivityMultiDevicesTestCases存在fail项
android·学习
大傻^7 小时前
SpringAI2.0 Null Safety 实战:JSpecify 注解体系与 Kotlin 互操作
android·开发语言·人工智能·kotlin·springai