update features/screenshot/src/main/ets/com/ohos/model/screenShotModel.ets.

Signed-off-by: 吕布布的耳朵 <shengli9@huawei.com>
This commit is contained in:
吕布布的耳朵 2023-05-23 08:07:11 +00:00 committed by Gitee
parent f05601e6e4
commit 9c5198a8f6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -12,102 +12,104 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Log from '../../../../../../../../common/src/main/ets/default/Log';
import ScreenshotManager from '@ohos.screenshot';
import WindowMar from '@ohos.window';
import WindowMar from '@ohos.window';
import ImageMar from '@ohos.multimedia.image';
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import FileIo from '@ohos.fileio';
import Constants from '../common/constants';
import display from '@ohos.display';
import Constants from '../common/constants';
import Log from '../../../../../../../../common/src/main/ets/default/Log';
const TAG = 'ScreenShot-ScreenShotModel';
const SCREEN_SHOT_PATH = "Screenshots/";
const SCREENSHOT_PREFIX = "Screenshot";
const PICTURE_TYPE = ".jpg";
const SCREEN_SHOT_PATH = 'Screenshots/';
const SCREENSHOT_PREFIX = 'Screenshot';
const PICTURE_TYPE = '.jpg';
const SAVE_IMAGE_DELAY = 300;
const OPTIONS_QUALITY = 100;
const CREATE_WINDOW_DELAY = 300;
export class ScreenShotModel {
captureImage: any;
captureImage: ImageMar.PixelMap;
imageFilename: string = '';
async shotScreen() {
Log.showInfo(TAG, `shotScreen`)
Log.showInfo(TAG, 'shotScreen');
await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
ScreenshotManager.save().then(async (data) => {
Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`)
if (!!data) {
this.captureImage = data;
AppStorage.Set("captureImage", data);
AppStorage.Set('captureImage', data);
this.saveImage(this.captureImage, {
format: ["image/jpeg"], quality: OPTIONS_QUALITY
format: ['image/jpeg'],
quality: OPTIONS_QUALITY,
});
}
}).catch((err) => {
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`)
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`);
})
}
async saveImage(pixelMap, options) {
const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext);
Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`);
this.imageFilename = SCREENSHOT_PREFIX + "_" + (new Date()).getTime() + PICTURE_TYPE;
this.imageFilename = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE;
var packer = ImageMar.createImagePacker();
Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`);
this.showWindow();
await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
packer.packing(pixelMap, options).then((jpegData) => {
Log.showInfo(TAG, `packing jpegData type:` + jpegData.constructor.name + ' value : ' + jpegData);
Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData);
media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => {
if (rp != undefined) {
Log.showInfo(TAG, `getPublicDirectory successfull:${JSON.stringify(rp)}`);
Log.showInfo(TAG, `getPublicDirectory successful:${JSON.stringify(rp)}`);
media.createAsset(mediaLibrary.MediaType.IMAGE, this.imageFilename, rp + SCREEN_SHOT_PATH, (createAssetErr, fileAsset) => {
if (fileAsset != undefined) {
Log.showInfo(TAG, `createAsset successfull:${JSON.stringify(fileAsset.uri)}`);
fileAsset.open('Rw').then((fd) => {
Log.showInfo(TAG, `createAsset successful:${JSON.stringify(fileAsset.uri)}`);
fileAsset.open('rw').then((fd) => {
if (fd == undefined) {
Log.showInfo(TAG, `fileAsset open fail`);
Log.showInfo(TAG, 'fileAsset open fail');
return;
}
Log.showInfo(TAG, `open successful, fd = ${JSON.stringify(fd)}`);
FileIo.write(fd, jpegData).then(num => {
Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`);
fileAsset.close(fd).then(() => {
Log.showInfo(TAG, `FileIo close,successfull`);
Log.showInfo(TAG, 'FileIo close,successful');
})
});
});
} else {
Log.showInfo(TAG, `createAsset fail`);
Log.showInfo(TAG, 'createAsset fail');
}
})
} else {
Log.showInfo(TAG, `getPublicDirectory fail`);
Log.showInfo(TAG, 'getPublicDirectory fail');
}
})
});
}
showWindow() {
Log.showInfo(TAG, `showWindow`);
Log.showInfo(TAG, 'showWindow');
WindowMar.find(Constants.WIN_NAME).then((win) => {
win.show(() => {
Log.showInfo(TAG, `window show`);
Log.showInfo(TAG, 'window show');
})
})
}
dismiss(): void {
dismiss(): void {
Log.showInfo(TAG, 'dismiss');
//close ability
globalThis.shotScreenContext.terminateSelf((err, data) => {
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`)
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`);
WindowMar.find(Constants.WIN_NAME).then((win) => {
win.destroy(() => {
Log.showInfo(TAG, `destroy the window finish`);
Log.showInfo(TAG, 'destroy the window finish');
})
})
})
@ -115,10 +117,10 @@ export class ScreenShotModel {
openAbility(wantData) {
Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`);
globalThis.shotScreenContext.startAbility(wantData)
globalThis.shotScreenContext.startAbility(wantData);
}
}
let screenShotModel = new ScreenShotModel();
export default screenShotModel as ScreenShotModel
export default screenShotModel as ScreenShotModel;