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()
相关推荐
浪潮IT馆2 小时前
Android Studio 非最新版本下载安装教程
android·ide·android studio
xiaozongt19893 小时前
MasterGo + Claude Code 生成 Android 布局:完整实践指南
android
Anhty4 小时前
2026最新免费手机音频处理工具 !!
android·功能测试·ios·智能手机·音视频
2501_915909066 小时前
iOS应用从开发到上架App Store的完整发布流程与步骤指南
android·ios·小程序·https·uni-app·webview
狗凯之家源码网7 小时前
AI 步数修改提交系统源码应用场景与落地指南
android·数据库·人工智能·运动步数
ZHOUPUYU7 小时前
PHP 9.0 前瞻:下一代 PHP 会带来哪些新功能?
android·开发语言·php
JMchen7 小时前
性能优化——让Native代码飞起来
android·c++·性能优化
ZHOUPUYU8 小时前
PHP 8.2 的核心高级特性,包括只读类、类型系统增强
android·性能优化·php
ZHOUPUYU8 小时前
PHP 8.0 高级技术实战:从新特性到性能优化
android·性能优化·php
不爱说话郭德纲8 小时前
从“点点点”到一键出包:我把 uni-app x Android 离线打包做成了脚本
android·前端·uni-app