HarmonyOS 长截屏分享功能实现

因部分界面超过一屏,如何实现长截屏功能?

HarmonyOS
2025-01-09 15:02:26
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

窗口截屏的API:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#snapshot9

示例参考:

import { common } from '@kit.AbilityKit';
import { image } from '@kit.ImageKit';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(()=>{
          let windowClass = (getContext(this) as common.UIAbilityContext).windowStage.getMainWindowSync()
          let shotPromise = windowClass.snapshot();
          shotPromise.then((pixelMap: image.PixelMap) => {
            const path: string = (getContext(this) as common.UIAbilityContext).cacheDir + "/shot_pixel_map.jpg";
            let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
            const imagePackerApi = image.createImagePacker();
            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };

            imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {
              // 直接打包进文件
              let pathJson: Record<string, string> = {
                "path": path
              }
              let buildSuccess: Record<string, string | object> = {
                "code": '1',
                "message": '成功',
                "data": pathJson
              }
              console.log(JSON.stringify(buildSuccess))
              pixelMap.release(); // PixelMap使用完后及时释放内存
            }).catch((error: BusinessError) => {
              console.error('Failed to snapshot window. And the error is: ' + error);
            })
          }).catch((err: BusinessError) => {
            console.error(`Failed to snapshot window. Cause code: ${err.code}, message: ${err.message}`);
          });
        })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
2025-01-09 18:36:44
相关问题
HarmonyOS功能如何实现
461浏览 • 1回复 待解决
求告知如何实现功能
801浏览 • 1回复 待解决
求大佬告知如何实现功能
2310浏览 • 1回复 待解决
HarmonyOS 应用内防功能
327浏览 • 1回复 待解决
如何实现功能,有人知道吗?
2269浏览 • 1回复 待解决
HarmonyOS 不被或者录如何实现
348浏览 • 1回复 待解决
HarmonyOS如何实现当前禁止
1176浏览 • 1回复 待解决
HarmonyOS如何实现当前禁止
375浏览 • 0回复 待解决
HarmonyOS 分享功能如何实现
544浏览 • 1回复 待解决
鸿蒙如何实现分享功能
18392浏览 • 2回复 待解决
使用功能系统后,有白线
7037浏览 • 2回复 待解决
HarmonyOS
531浏览 • 1回复 待解决
如何、录、投
2536浏览 • 1回复 待解决
HarmonyOS 如何禁用、录
617浏览 • 1回复 待解决
原来下滑出现的功能哪去了?
7179浏览 • 1回复 待解决
HarmonyOS 如何防防录
586浏览 • 1回复 待解决