鸿蒙NEXT实战开发:【截屏】

展示全屏截图和屏幕局部截图。通过[screenshot]模块实现屏幕截图 ,通过[window]模块实现隐私窗口切换,通过[display]模块查询当前隐私窗口。

效果预览

全屏截图局部截图选择区域局部截图

使用说明:

  1. 点击右上角图标打开弹窗,选择截屏,展示全屏截图;选择局部截屏,选择截屏区域,点击右下角完成,展示局部截屏;
  2. 点击滑块切换窗口隐私模式,隐私模式下截屏会弹出提示,拒绝截屏。

具体实现

本示例通过screenshot接口实现屏幕截图 ,通过window接口实现隐私窗口切换,通过display接口查询当前隐私窗口。

  • 源码链接:[Screenshot.ets]
/*

 * Copyright (c) 2022 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import screenshot from '@ohos.screenshot'

import { Logger } from './Logger'

import { getCurrentWindow } from './WindowPrivacy'



// 屏幕截图 默认参数screenshotOptions为空时 截全屏

export function getScreenshot(screenshotOption = {}) {

  return screenshot.save(screenshotOption)

}



// 设置全屏展示 isFullScreen: boolean

export function setFullScreen(context: Context, isFullScreen: boolean) {

  getCurrentWindow(context)

    .then(res => {

      res.setFullScreen(isFullScreen, (err) => {

        if (err.code) {

          Logger.error('failed set full-screen mode cause: ' + JSON.stringify(err))

          return

        }

        Logger.info('success set full-screen mode')

      })

    })

}
  • [WindowPrivacy.ets]
/*

 * Copyright (c) 2022 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import window from '@ohos.window'

import display from '@ohos.display'

import { ResponseData } from '../models/ResponseData'

import { Logger } from './Logger'



// 获取当前窗口

export function getCurrentWindow(context: Context) {

  return window.getTopWindow(context)

}



// 判断隐私窗口

export function hasPrivate(): ResponseData {

  let currentDisplay = null

  try {

    currentDisplay = display.getDefaultDisplaySync()

  } catch (exception) {

    return { status: 'failed', errorMessage: JSON.stringify(exception) }

  }

  if (currentDisplay === null) {

    return { status: 'failed', errorMessage: 'get current display failed' }

  }

  let ret = undefined

  try {

    ret = display.hasPrivateWindow(currentDisplay.id)

  } catch (exception) {

    return { status: 'failed', errorMessage: JSON.stringify(exception) }

  }

  if (ret === undefined) {

    return { status: 'failed', errorMessage: 'ret is undefined' }

  }

  return ret ? { status: 'success', errorMessage: '', result: true } :

    { status: 'success', errorMessage: '', result: false }

}





// 设置隐私窗口

export function setWindowPrivacyMode(context: Context, windowPrivacyMode: boolean) {

  let currentWindow = null

  getCurrentWindow(context)

    .then(res => {

      currentWindow = res

      try {

        currentWindow.setWindowPrivacyMode(windowPrivacyMode, (err) => {

          if (err.code) {

            Logger.error('set window privacy mode failed cause: ' + JSON.stringify(err))

            return

          }

          Logger.info(`set window privacy mode success ${windowPrivacyMode}`)

        })

      } catch (exception) {

        Logger.info('set window mode failed cause: ' + JSON.stringify(exception))

      }

    })

}

鸿蒙OpenHarmony知识已更新←前往

062b7e9f99042d0461e287c5c0ee8749.png


http://www.niftyadmin.cn/n/5412097.html

相关文章

计算机视觉基础知识(二)---数字图像

像素 像素是分辨率的单位;构成位图图像的最基本单元;每个像素都有自己的颜色; 图像分辨率 单位英寸内的像素点数;单位为PPI(Pixels Per Inch),为像素每英寸;PPI表示每英寸对角线上所拥有的像素数目:,x:长度像素数目,y:宽度像素数目,Z:屏幕大小;屏幕尺寸(大小)指的是对角线长…

激光在SIC晶圆制造中的应用

碳化硅是一种性能优异的第三代半导体材料,具有光学性能良好、化学惰性大、物理特性优良的特点,包括带隙宽、击穿电压高、热导率高和耐高温性能强等优点,常作为新一代高频、高功率器件的衬底材料,广泛应用在高端制造业领域&#xf…

Python基于opencv的人脸识别上课签到考勤系统,附源码

博主介绍:✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 🍅文末获取源码联系🍅 👇🏻 精彩专栏推荐订阅👇…

HTML和CSS (前端共三篇)【详解】

目录 一、前端开发介绍 二、HTML入门 三、HTML基础标签 四、CSS样式修饰 五、HTML表格标签 六、HTML表单标签 一、前端开发介绍 web应用有BS和CS架构两种,其中我们主要涉及的是BS架构。而BS架构里,B(Browser浏览器)是客户端的…

Android Split APK是什么

Android Split APK是一项应用程序分发和安装的技术,可以将大型应用程序拆分为多个较小的模块,以便用户可以选择性地下载和安装所需的模块,而无需一次性下载整个应用程序。这种技术旨在提高用户体验、减少下载时间和节省存储空间。 Android S…

基于SpringBoot的CSGO赛事管理系统(程序+数据库+文档)

** 🍅点赞收藏关注 → 私信领取本源代码、数据库🍅 本人在Java毕业设计领域有多年的经验,陆续会更新更多优质的Java实战项目,希望你能有所收获,少走一些弯路。🍅关注我不迷路🍅** 一、研究背景…

论文阅读笔记 | Limited-Reference Image Quality Assessment: Paradigms and Discussions

文章目录 文章题目发表年限期刊/会议名称动机主要思想或方法架构实验结果 文章链接:https://dl.acm.org/doi/10.1145/3581783.3613436 文章题目 Limited-Reference Image Quality Assessment: Paradigms and Discussions 发表年限 2023 期刊/会议名称 MM’23: …

【Unity】【VR开发】写一个用手枪发射子弹的脚本

【背景】 用Unity XR Interaction Toolkits做VR开发,需要Grab手枪后按下Trigger按键发射子弹。 【分析】 为了能够使用Unity提供的VR开发包,需要在引用部分追加:Using unityengin.xr.interaction.toolkit在这个脚本中,我们需要三个公共变量。 第一个公共变量命名为bullet…