HarmonyOS 一个普通应用,如何在代码里实现屏幕截图操作,返回image.PixelMap?

HarmonyOS 一个普通应用,如何在代码里实现屏幕截图操作,返回image.PixelMap?

HarmonyOS
2024-10-09 12:29:29
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

参考以下代码,可以实现截图:

import window from '@ohos.window';  
import { BusinessError } from '@kit.BasicServicesKit';  
import { image } from '@kit.ImageKit';  
import { photoAccessHelper } from '@kit.MediaLibraryKit';  
import fs from '@ohos.file.fs';  
import { promptAction } from '@kit.ArkUI';  
@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 }  
        })  
      SaveButton({  
        icon: SaveIconStyle.FULL_FILLED,  
        text: SaveDescription.SAVE_IMAGE,  
        buttonType: ButtonType.Capsule  
      })  
        .onClick(async (event, result) => {  
          // Get pixelMap  
          window.getLastWindow(getContext(this), (err: BusinessError, data) => {  
            const errCode: number = err.code;  
            if (errCode) {  
              return;  
            }  
            let windowClass = data;  
            windowClass.snapshot(async (err: BusinessError, pixelMap: image.PixelMap) => {  
              const errCode: number = err.code;  
              if (errCode) {  
                return;  
              }  
              console.info('Succeeded in snapshotting window. Pixel bytes number: ' + pixelMap.getPixelBytesNumber());  
              let scaleX: number = 0.5;  
              let scaleY: number = 0.5;  
              // Save Pic  
              if (result === SaveButtonOnClickResult.SUCCESS) {  
                const helper = photoAccessHelper.getPhotoAccessHelper(getContext(this));  
                try {  
                  const uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png');  
                  const file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);  
                  const imagePackerApi: image.ImagePacker = image.createImagePacker();  
                  const packOpts: image.PackingOption = {  
                    format: 'image/png',  
                    quality: 100,  
                  };  
                  imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {  
                    promptAction.showToast({  
                      message: 'Succeeded save',  
                      duration: 1500  
                    })  
                  }).catch((error: BusinessError) => {  
                    console.error(`Failed to packToFile. Error code is ${error.code}, message is ${error.message}`);  
                  });  
                } catch (error) {  
                  const err: BusinessError = error as BusinessError;  
                  console.error(`Failed to save photo. Error code is ${err.code}, message is ${err.message}`);  
                }  
              } else {  
                promptAction.showToast({  
                  message: 'No permission.',  
                  duration: 1500  
                })  
              }  
            });  
          });  
        })  
    }  
    .height('100%')  
    .width('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-09 15:50:15
相关问题
HarmonyOS image.PixelMap的拉伸变形
29浏览 • 1回复 待解决
鸿蒙如何实现一个本地定时通知?
183浏览 • 0回复 待解决
HarmonyOS 如何返回一个颜色?
312浏览 • 1回复 待解决
如何一个Component画到Pixelmap
1909浏览 • 1回复 待解决
HarmonyOS 怎么把两PixelMap合成一个
68浏览 • 2回复 待解决
如何一个Component画到Pixelmap上呢?
2649浏览 • 1回复 待解决
如何一个文件进行读写操作
496浏览 • 1回复 待解决