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

电子说

1.2w人已加入

描述

展示全屏截图和屏幕局部截图。通过[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))

      }

    })

}

鸿蒙

审核编辑 黄宇

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

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分