HarmonyOS鸿蒙NEXT可以监听切屏获取当前的截屏照片吗

HarmonyOS鸿蒙NEXT可以监听切屏获取当前的截屏照片吗?

4 回复

参考一下demo,看能否满足需求:

import { image } from '@kit.ImageKit'
import { componentSnapshot } from '@kit.ArkUI'

@CustomDialog
struct CustomDialogComponent{
  controller?:CustomDialogController
  //将这里用@Prop或者@Link,不传buffer,也不行
  @Link pixelMap: image.PixelMap|undefined /*= undefined*/
  @Prop text: string|undefined

  build(){
    Column() {
      Text(this.text).width('100%').height(50).textAlign(TextAlign.Center).fontColor(Color.Green)
      Image(this.pixelMap).width(200).height(500).objectFit(ImageFit.Contain)

      Button('关闭').width(100).height(45).backgroundColor('#ffcc0000').onClick(() =>{
        this.controller?.close()
      }).margin({top:10})
    }
  }
}

@Entry
@Component
struct SnapShotExample{
  @State items:string[] = ['1', '2', '3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32']

  @State pixMap:image.PixelMap|undefined = undefined
  @State mText:string = '我是内容'
  dialogController:CustomDialogController = new CustomDialogController({builder:CustomDialogComponent({pixelMap:this.pixMap, text:this.mText})})

  build(){
    Column() {
      Text('我是标题')
        .width('100%').height(100).textAlign(TextAlign.Center).fontColor(Color.Black).backgroundColor(Color.Gray)
      Scroll() {
        Column() {
          ForEach(this.items, (item: string) =>{
            Text('我是内容' + item).width('100%').height(50).fontColor(Color.Red)
          })
        }.width('100%')
      }.width('100%').layoutWeight(1).id('mainScroll')
      Blank().width('100%').height(1).backgroundColor(Color.Black)
      //这个作为对比测试,是可以的
      Image(this.pixMap).width('100%').layoutWeight(0.5).objectFit(ImageFit.Contain)

      Button('点击截图').width(100).height(45).onClick(() =>{
        componentSnapshot.get('mainScroll', {scale:0.8, waitUntilRenderFinished:true}).then((pixMap:image.PixelMap)=>{
          //如果直接用pixMap方式,只要这一句,对比测试也是可以的
          this.pixMap = pixMap

          this.dialogController.open()

          this.mText = '截图成功'
        }).catch((err:Error)=>{
          console.info('截图失败:'+JSON.stringify(err))
        })
      })
    }.width('100%').height('100%')
  }
}

更多关于HarmonyOS鸿蒙NEXT可以监听切屏获取当前的截屏照片吗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


鸿蒙可以监听切屏获取当前的截屏照片,首先要分成2步,

第一步是打开监听截屏的功能,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/screenproperty-guideline-V5#监听屏幕状态变化

第二步是监听到截屏后获取截屏内容,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#snapshot9-1

最后,写了个简单的demo:

@Entry
@Component
struct DisplayDemo {
  context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;

  aboutToAppear(): void {
    this.display()
  }

  build() {
    Column() {
      Button("click to generate UI snapshot")
        .onClick(() => {

        }).margin(10)
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Center)
  }

  display() {
    let callback2: Callback<boolean> = async (captureStatus: boolean) => {
      console.info('Listening capture status: ' + captureStatus);
      if (captureStatus) {
        let windowClass = await window.getLastWindow(this.context);
        let promise = windowClass.snapshot();
        promise.then((pixelMap: image.PixelMap) => {
          console.info('Succeeded in snapshotting window. Pixel bytes number: ' + pixelMap.getPixelBytesNumber());
          pixelMap.release(); // PixelMap使用完后及时释放内存
        }).catch((err: BusinessError) => {
          console.error(`Failed to snapshot window. Cause code: ${err.code}, message: ${err.message}`);
        });
      }
    };
    // 此处以监听显示设备的增加为例
    display.on('captureStatusChange', callback2);
  }
}

在HarmonyOS NEXT中,可以通过ScreenCapture模块监听截屏事件并获取当前截屏。使用@ohos.screenCapture API,调用on('screenCapture')监听截屏动作,触发后可通过getLastScreenCapture获取最新截屏文件路径。需要声明ohos.permission.CAPTURE_SCREEN权限。具体实现涉及注册监听器和处理文件路径回调。

在HarmonyOS NEXT中,应用可以通过权限声明和系统API实现截屏监听功能。具体实现要点如下:

  1. 需要申请权限:
  • ohos.permission.CAPTURE_SCREEN (需系统权限)
  • ohos.permission.READ_MEDIA
  1. 主要API:
  • 使用@ohos.screen模块的on(‘screenCapture’)监听截屏事件
  • 通过媒体库接口获取最新截屏图片
  1. 注意事项:
  • 该功能涉及用户隐私,需用户明确授权
  • 系统级应用才能获取完整功能
  • 需在module.json5中正确声明权限

建议查阅最新版HarmonyOS SDK文档中关于屏幕捕获和媒体库访问的相关API说明。

回到顶部