1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { BusinessError } from '@ohos.base'; 17import ScreenshotManager from '@ohos.screenshot'; 18import WindowMar from '@ohos.window'; 19import ImageMar from '@ohos.multimedia.image'; 20import Constants from '../common/constants'; 21import Log from '../../../../../../../../common/src/main/ets/default/Log'; 22import Want from '@ohos.app.ability.Want'; 23import userFileManager from '@ohos.filemanagement.userFileManager'; 24import common from '@ohos.app.ability.common'; 25import file from '@ohos.file.fs'; 26 27const TAG = 'ScreenShot-ScreenShotModel'; 28 29const SCREEN_SHOT_PATH = 'Screenshots/'; 30const SCREENSHOT_PREFIX = 'Screenshot'; 31const PICTURE_TYPE = '.jpg'; 32const SAVE_IMAGE_DELAY = 300; 33const OPTIONS_QUALITY = 100; 34const CREATE_WINDOW_DELAY = 300; 35 36export class ScreenShotModel { 37 private captureImage: ImageMar.PixelMap | undefined = undefined; 38 private imageFileName: string = ''; 39 40 async shotScreen() { 41 Log.showInfo(TAG, 'shotScreen'); 42 await new Promise<number>((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY)); 43 ScreenshotManager.save().then(async (data) => { 44 Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`); 45 if (!!data) { 46 this.captureImage = data; 47 AppStorage.Set('captureImage', data); 48 this.saveImage(this.captureImage, { 49 format: 'image/jpeg', 50 quality: OPTIONS_QUALITY, 51 }); 52 } 53 }).catch((err: BusinessError) => { 54 Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`); 55 }); 56 } 57 58 async saveImage(pixelMap: ImageMar.PixelMap, options: ImageMar.PackingOption) { 59 let fileAsset: userFileManager.FileAsset | undefined = undefined; 60 let fd: number | undefined = undefined; 61 62 const context = globalThis.shotScreenContext as common.ServiceExtensionContext; 63 try { 64 const userFileMgr = userFileManager.getUserFileMgr(context); 65 this.imageFileName = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE; 66 Log.showInfo(TAG, `imageName: ${this.imageFileName}`); 67 68 const packer = ImageMar.createImagePacker(); 69 this.showWindow(); 70 await new Promise<number>((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY)); 71 const packedImg = await packer.packing(pixelMap, options); 72 73 const createOption: userFileManager.PhotoCreateOptions = { 74 subType: userFileManager.PhotoSubType.SCREENSHOT, 75 }; 76 fileAsset = await userFileMgr.createPhotoAsset(this.imageFileName, createOption); 77 78 AppStorage.setOrCreate('imageUri', fileAsset.uri); 79 Log.showInfo(TAG, `fileAsset uri: ${fileAsset.uri}`); 80 81 fd = await fileAsset.open('w'); 82 await file.write(fd, packedImg); 83 await file.fsync(fd); 84 AppStorage.setOrCreate('imageFilename', this.imageFileName); 85 Log.showInfo(TAG, `SaveImage success`); 86 } catch (error) { 87 Log.showInfo(TAG, `SaveImage failed, cause: ${error}`); 88 } finally { 89 if (fileAsset && fd !== undefined) { 90 await fileAsset.close(fd); 91 Log.showInfo(TAG, `FileIo close, successful`); 92 } 93 } 94 } 95 96 showWindow() { 97 Log.showInfo(TAG, 'showWindow'); 98 WindowMar.find(Constants.WIN_NAME).then((win) => { 99 win.show(() => { 100 Log.showInfo(TAG, 'window show'); 101 }); 102 }); 103 } 104 105 dismiss(): void { 106 Log.showInfo(TAG, 'dismiss'); 107 //close ability 108 globalThis.shotScreenContext.terminateSelf((err: BusinessError) => { 109 Log.showInfo(TAG, `terminateSelf finish err:${err}`); 110 WindowMar.find(Constants.WIN_NAME).then((win) => { 111 win.destroy(() => { 112 Log.showInfo(TAG, 'destroy the window finish'); 113 }); 114 }); 115 }); 116 } 117 118 openAbility(wantData: Want) { 119 Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`); 120 globalThis.shotScreenContext.startAbility(wantData); 121 } 122} 123 124let screenShotModel = new ScreenShotModel(); 125 126export default screenShotModel as ScreenShotModel;