鸿蒙NEXT实战开发:【截屏】
温馨提示:这篇文章已超过402天没有更新,请注意相关的内容是否还可用!
展示全屏截图和屏幕局部截图。通过[screenshot]模块实现屏幕截图 ,通过[window]模块实现隐私窗口切换,通过[display]模块查询当前隐私窗口。
效果预览
| 全屏截图 | 局部截图选择区域 | 局部截图 |
|---|---|---|
![]() | ![]() | ![]() |
使用说明:
- 点击右上角图标打开弹窗,选择截屏,展示全屏截图;选择局部截屏,选择截屏区域,点击右下角完成,展示局部截屏;
- 点击滑块切换窗口隐私模式,隐私模式下截屏会弹出提示,拒绝截屏。
具体实现
本示例通过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知识已更新←前往
- [WindowPrivacy.ets]
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!




