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 */ 15import screenshot from '@ohos.screenshot' 16import image from '@ohos.multimedia.image' 17import { TitleBar } from '../common/TitleBar' 18import MediaUtils from '../model/MediaUtils' 19import Logger from '../model/Logger' 20 21const TAG = '[Screenshot]' 22 23@Entry 24@Component 25struct Index { 26 getScreen = (isFullScreen: boolean) => { 27 let screenshotOptions: screenshot.ScreenshotOptions = { 28 screenRect: { left: 0, top: 0, width: 400, height: 400 }, 29 imageSize: { width: 400, height: 400 }, 30 rotation: 0, 31 displayId: 0 32 } 33 if (isFullScreen) { 34 screenshotOptions = { 35 rotation: 0 36 } 37 } 38 try { 39 screenshot.save(screenshotOptions, (err, data: image.PixelMap) => { 40 if (err) { 41 Logger.info(TAG, `Failed to save the screenshot. Error:${JSON.stringify(err)}`) 42 } 43 Logger.info(TAG, 'save callback') 44 MediaUtils.savePicture(data, getContext(this) as any) 45 }) 46 } catch (err) { 47 Logger.error(`save failed, code is ${err.code}, message is ${err.message}`) 48 } 49 } 50 51 @Builder 52 CustomBtn(text: Resource, isFullScreen: boolean) { 53 Button() { 54 Text(text) 55 .fontColor(Color.White) 56 .fontSize(20) 57 } 58 .key(isFullScreen + 'FullScreenShot') 59 .type(ButtonType.Capsule) 60 .backgroundColor('#0D9FFB') 61 .size({ width: '90%', height: 50 }) 62 .margin(10) 63 .backgroundColor('#0D9FFB') 64 .onClick(() => { 65 console.info('[Screenshot] onClick,screenshot') 66 this.getScreen(isFullScreen) 67 }) 68 } 69 70 build() { 71 Column() { 72 TitleBar() 73 Scroll() { 74 Column() { 75 Image($r('app.media.image')) 76 .width('95%') 77 .height('35%') 78 .margin({ top: 10 }) 79 .backgroundColor('#E5E5E5') 80 .objectFit(ImageFit.Contain) 81 82 this.CustomBtn($r('app.string.btn_fullscreen'), true) 83 this.CustomBtn($r('app.string.btn_fixedsize'), false) 84 } 85 } 86 .layoutWeight(1) 87 } 88 .width('100%') 89 .height('100%') 90 } 91}