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%')
  }
}
  • 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.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
分享
微博
QQ
微信
回复
2025-01-09 18:36:44
相关问题
HarmonyOS功能如何实现
697浏览 • 1回复 待解决
求告知如何实现功能
974浏览 • 1回复 待解决
HarmonyOS 应用内防功能
529浏览 • 1回复 待解决
求大佬告知如何实现功能
2484浏览 • 1回复 待解决
如何实现功能,有人知道吗?
2443浏览 • 1回复 待解决
HarmonyOS 不被或者录如何实现
556浏览 • 1回复 待解决
HarmonyOS 分享功能如何实现
751浏览 • 1回复 待解决
HarmonyOS如何实现当前禁止
579浏览 • 0回复 待解决
HarmonyOS如何实现当前禁止
1367浏览 • 1回复 待解决
鸿蒙如何实现分享功能
18826浏览 • 2回复 待解决
使用功能系统后,有白线
7202浏览 • 2回复 待解决
HarmonyOS
764浏览 • 1回复 待解决
HarmonyOS 如何禁用、录
855浏览 • 1回复 待解决
如何、录、投
2998浏览 • 1回复 待解决
原来下滑出现的功能哪去了?
7324浏览 • 1回复 待解决
HarmonyOS 如何禁止和录
1207浏览 • 1回复 待解决