• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ScreenshotManager from '@ohos.screenshot';
17import WindowMar from '@ohos.window';
18import ImageMar from '@ohos.multimedia.image';
19import mediaLibrary from '@ohos.multimedia.mediaLibrary';
20import FileIo from '@ohos.fileio';
21import display from '@ohos.display';
22import Constants from '../common/constants';
23import Log from '../../../../../../../../common/src/main/ets/default/Log';
24
25const TAG = 'ScreenShot-ScreenShotModel';
26
27const SCREEN_SHOT_PATH = 'Screenshots/';
28const SCREENSHOT_PREFIX = 'Screenshot';
29const PICTURE_TYPE = '.jpg';
30const SAVE_IMAGE_DELAY = 300;
31const OPTIONS_QUALITY = 100;
32const CREATE_WINDOW_DELAY = 300;
33
34export class ScreenShotModel {
35  captureImage: ImageMar.PixelMap;
36  imageFilename: string = '';
37
38  async shotScreen() {
39    Log.showInfo(TAG, 'shotScreen');
40    await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
41    ScreenshotManager.save().then(async (data) => {
42      Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`)
43      if (!!data) {
44        this.captureImage = data;
45        AppStorage.Set('captureImage', data);
46        this.saveImage(this.captureImage, {
47          format: ['image/jpeg'],
48          quality: OPTIONS_QUALITY,
49        });
50      }
51    }).catch((err) => {
52      Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`);
53    })
54  }
55
56  async saveImage(pixelMap, options) {
57    const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext);
58    Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`);
59    this.imageFilename = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE;
60    var packer = ImageMar.createImagePacker();
61    Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`);
62    this.showWindow();
63    await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
64    packer.packing(pixelMap, options).then((jpegData) => {
65      Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + '  value : ' + jpegData);
66      media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => {
67        if (rp != undefined) {
68          Log.showInfo(TAG, `getPublicDirectory successful:${JSON.stringify(rp)}`);
69          media.createAsset(mediaLibrary.MediaType.IMAGE, this.imageFilename, rp + SCREEN_SHOT_PATH, (createAssetErr, fileAsset) => {
70            if (fileAsset != undefined) {
71              Log.showInfo(TAG, `createAsset successful:${JSON.stringify(fileAsset.uri)}`);
72              fileAsset.open('rw').then((fd) => {
73                if (fd == undefined) {
74                  Log.showInfo(TAG, 'fileAsset open fail');
75                  return;
76                }
77                Log.showInfo(TAG, `open successful, fd = ${JSON.stringify(fd)}`);
78                FileIo.write(fd, jpegData).then(num => {
79                  Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`);
80                  fileAsset.close(fd).then(() => {
81                    Log.showInfo(TAG, 'FileIo close,successful');
82                  })
83                });
84              });
85            } else {
86              Log.showInfo(TAG, 'createAsset fail');
87            }
88          })
89        } else {
90          Log.showInfo(TAG, 'getPublicDirectory fail');
91        }
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, data) => {
109      Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`);
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) {
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;