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

审核编辑 黄宇

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

    关注

    55

    文章

    1651

    浏览量

    42129
收藏 人收藏

    评论

    相关推荐

    今日看点丨华为 HarmonyOS NEXT 鸿蒙星河版第四季度商用;博世计划2026年底前裁员1200人

    1. 华为 HarmonyOS NEXT 鸿蒙星河版第四季度商用,第二季度启动开发者 Beta   在鸿蒙生态千帆启航仪式,华为宣布 HarmonyOS
    发表于 01-19 10:47 858次阅读

    鸿蒙千帆起】《钢岚》成为首款基于HarmonyOS NEXT开发的战棋新游

    近日,紫龙游戏旗下 BlackJack 工作室全新战棋旗舰作品《钢岚》在华为游戏中心首发上线,并宣布《钢岚》完成鸿蒙原生应用开发,成为基于 HarmonyOS NEXT 开发的首款战棋
    发表于 12-28 10:24

    鸿蒙Harmony是如何影响Android工程师的呢?

    。 程序员机遇 其实很多人已经看到鸿蒙的趋势了。那么,Android开发以及其他开发人员如何学习鸿蒙鸿蒙
    发表于 01-14 22:14

    鸿蒙千帆起】高德地图携手HarmonyOS NEXT,开启智能出行新篇章

    2024 年 1 月 18 日下午,华为举办了鸿蒙生态千帆启航仪式,对外宣布 HarmonyOS NEXT 星河预览版现已开放申请,同时,首批 200+鸿蒙原生应用加速开发
    发表于 02-02 11:09

    鸿蒙开发者预览版如何?

    在24年的华为鸿蒙发布会中表示。预览版已经向开发者开放申请,首批支持的机型有三款分别为华为 Mate 60、华为Mate 60 Pro、华为Mate X5。 其HarmonyOS NEXT去除
    发表于 02-17 21:54

    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

    OpenHarmony设备的5种方式

    本文转载自《OpenHarmony设备的5种方式 》,作者westinyang ​​ 方式1:系统控制中心 ● 顶部下滑在控制中心里点击 ● 这种方式最普遍,截图后可到相册查看
    发表于 08-29 14:49

    STM32开发板能吗?

    STM32开发板能嘛?
    发表于 10-17 07:15

    华为开发者大会分论坛HarmonyOS测试技术与实战-鸿蒙智联认证生态设备测试挑战

    HDC 2021华为开发者大会分论坛HarmonyOS测试技术与实战-鸿蒙智联认证生态设备测试挑战
    的头像 发表于 10-23 16:40 1534次阅读
    华为<b class='flag-5'>开发</b>者大会分论坛HarmonyOS测试技术与<b class='flag-5'>实战</b>-<b class='flag-5'>鸿蒙</b>智联认证生态设备测试挑战

    解读《鸿蒙·NEXT星空版》华为注资70亿,将每月开发岗增涨10万+

    华为将携手伙伴,依托鸿蒙生态学堂、高校共同培养鸿蒙人才、城市发布鸿蒙人才培养政策等方式,每月培养 10 万+鸿蒙开发者。
    的头像 发表于 01-26 09:43 340次阅读
    解读《<b class='flag-5'>鸿蒙</b>·<b class='flag-5'>NEXT</b>星空版》华为注资70亿,将每月<b class='flag-5'>开发</b>岗增涨10万+

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

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

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

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

    《详解:鸿蒙NEXT开发核心技术》

    我们现在都知道鸿蒙作为一个国产的全栈自研系统,经过国家主推后。已经引起人们很大的关注,其中作为开发者来说;许多一线大厂已经与其华为鸿蒙展开原生应用的合作了,目前了解到已经有200+家。而之后出现了很多的高薪
    的头像 发表于 03-13 23:00 357次阅读
    《详解:<b class='flag-5'>鸿蒙</b><b class='flag-5'>NEXT</b><b class='flag-5'>开发</b>核心技术》