0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

jf_46214456 ? 来源:jf_46214456 ? 作者:jf_46214456 ? 2024-03-07 16:02 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

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

效果预览

image.png

使用说明:

  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))

      }

    })

}

062b7e9f99042d0461e287c5c0ee8749.png

审核编辑 黄宇

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 鸿蒙
    +关注

    关注

    60

    文章

    2644

    浏览量

    44260
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    2024款鸿蒙OS 最新HarmonyOS Next_HarmonyOS4.0系列教程分享

    实战,还包含了HarmonyOS 仿小米App实战。详情参考大纲或者目录介绍。 根据华为鸿蒙生态推广部门介绍,HarmonyOS4和未来的HarmonyOS5(HarmonyOS Next
    发表于 02-28 10:29

    鸿蒙实战项目开发:【短信服务】

    、OpenHarmony 多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发鸿蒙项目实战等等)鸿蒙(Harmony NE
    发表于 03-03 21:29

    名单公布!【书籍评测活动NO.47】HarmonyOS NEXT启程:零基础构建纯血鸿蒙应用

    要在2000万 。这一呼吁引起了全球开发者的关注,也意味着市场对HarmonyOS开发人才的需求将快速增长。 什么是纯血鸿蒙 HarmonyOS NEXT,也被称为纯血
    发表于 10-14 14:36

    鸿蒙Flutter实战:07混合开发

    # 鸿蒙Flutter实战:混合开发 鸿蒙Flutter混合开发主要有两种形式。 ## 1.基于har 将flutter module
    发表于 10-23 16:00

    名单公布!【书籍评测活动NO.56】极速探索HarmonyOS NEXT:纯血鸿蒙应用开发实践

    的各个关键领域。另外,书中还提供了基于HarmonyOS NEXT 的完整实战项目和3个特色案例,并附带了全套的源代码。 本书适合鸿蒙应用开发工程师、移动应用
    发表于 01-20 16:53

    鸿蒙系统截图怎么 在哪里操作

    华为鸿蒙操作系统发布已经过去将近一个月了,那么,华为鸿蒙系统截图怎么鸿蒙系统
    的头像 发表于 06-25 09:57 ?9268次阅读

    鸿蒙系统怎么 5种方法的详细介绍

    鸿蒙系统作为一个新系统,里面的玩法肯定也会与之前系统有一定的差别,很多小伙伴就不太清楚鸿蒙系统怎么
    的头像 发表于 06-28 12:03 ?6.5w次阅读

    鸿蒙系统功能在哪里

    华为鸿蒙系统发布后,目前为止很多小伙伴都升级了鸿蒙系统,是手机使用过程中一个很基础的功能,那么鸿蒙系统
    的头像 发表于 07-09 15:44 ?1.2w次阅读

    屏幕功能

    屏幕功能
    发表于 05-26 15:25 ?15次下载

    淘宝与华为合作将基于HarmonyOS NEXT启动鸿蒙原生应用开发

    1月25日,淘宝与华为举办鸿蒙合作签约仪式,宣布将基于HarmonyOS NEXT启动鸿蒙原生应用开发
    的头像 发表于 01-26 16:14 ?1534次阅读

    华为宣布HarmonyOS NEXT鸿蒙星河版开发者预览面向开发者开放申请

    华为宣布HarmonyOS NEXT鸿蒙星河版开发者预览面向开发者开放申请,这意味着鸿蒙生态进入第二阶段,将加速千行百业的应用
    的头像 发表于 01-29 16:42 ?1858次阅读
    华为宣布HarmonyOS <b class='flag-5'>NEXT</b><b class='flag-5'>鸿蒙</b>星河版<b class='flag-5'>开发</b>者预览面向<b class='flag-5'>开发</b>者开放申请

    鸿蒙开发教程

    去年8 月份华为发布会上,华为发布了HarmonyOS NEXT预览版,宣布不再兼容安卓应用。大家期待的纯血鸿蒙终于要来临了,next 预览版本现在已经开放申请渠道了,Next
    的头像 发表于 01-31 17:11 ?1432次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>教程

    华为pockets怎么

    华为Pocket S的方法有以下几种。
    的头像 发表于 03-06 16:06 ?3364次阅读

    鸿蒙NEXT南向开发案例:【智能台灯】

    鸿蒙南向开发之智能台灯案例
    的头像 发表于 04-03 18:00 ?1463次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>NEXT</b>南向<b class='flag-5'>开发</b>案例:【智能台灯】

    HarmonyOS开发实例:【手势

    基于手势处理和能力,介绍了手势的实现过程。样例主要包括以下功能
    的头像 发表于 04-11 22:38 ?1224次阅读
    HarmonyOS<b class='flag-5'>开发</b>实例:【手势<b class='flag-5'>截</b><b class='flag-5'>屏</b>】