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 display from '@ohos.display' 16import image from '@ohos.multimedia.image' 17import screenshot from '@ohos.screenshot' 18import Logger from '../model/Logger' 19import MediaUtils from '../model/MediaUtils' 20import TitleBar from '../common/TitleBar' 21 22const TAG: string = '[CreateQRCode]' 23const QRCODE_SIZE: number = vp2px(300) // 生成二维码的大小 24const QRCODE_PADDING: number = vp2px(50) // 二维码保存时图片边距 25const QRCODE_TOP: number = vp2px(100) // 二维码保存时图片顶部距离屏幕顶部边缘距离 26 27@Entry 28@Component 29struct CreateQRCode { 30 private rectLeft: number = 0 31 private mediaUitl: MediaUtils = new MediaUtils(getContext(this) as any) 32 @State codeStr: string = '' 33 getScreen = () => { 34 console.info(`${TAG} getScreen,isFullScreen`) 35 let screenshotOptions: screenshot.ScreenshotOptions = { 36 screenRect: { 37 left: this.rectLeft, 38 top: QRCODE_TOP, 39 width: QRCODE_SIZE + 2 * QRCODE_PADDING, 40 height: QRCODE_SIZE + 2 * QRCODE_PADDING 41 }, 42 imageSize: { 43 width: QRCODE_SIZE + 2 * QRCODE_PADDING, 44 height: QRCODE_SIZE + 2 * QRCODE_PADDING 45 }, 46 rotation: 0, 47 displayId: 0 48 } 49 50 screenshot.save(screenshotOptions, (err, data: image.PixelMap) => { 51 if (err) { 52 Logger.info(TAG, `Failed to save the screenshot. Error:${JSON.stringify(err)}`) 53 } 54 Logger.info(TAG, 'save callback') 55 this.mediaUitl.savePicture(data) 56 }) 57 } 58 59 async aboutToAppear() { 60 let defaultDisplay = await display.getDefaultDisplay() 61 Logger.info(TAG, `defaultDisplay.width = ${defaultDisplay.width}`) 62 this.rectLeft = (defaultDisplay.width - QRCODE_SIZE) / 2 - QRCODE_PADDING 63 } 64 65 build() { 66 Column() { 67 TitleBar({ hasBackPress: true }) 68 Scroll() { 69 Column() { 70 QRCode(this.codeStr) 71 .width(px2vp(QRCODE_SIZE)) 72 .aspectRatio(1) 73 .margin(40) 74 TextInput({ placeholder: $r('app.string.input_placeholder'), text: this.codeStr }) 75 .key('createQRCodeInput') 76 .width('90%') 77 .height(60) 78 .placeholderColor(Color.Gray) 79 .fontSize(30) 80 .margin({ left: 15, right: 15, top: 20 }) 81 .backgroundColor(Color.White) 82 .onChange((value: string) => { 83 this.codeStr = value 84 }) 85 86 Button({ type: ButtonType.Capsule }) { 87 Text($r('app.string.save')) 88 .fontSize(30) 89 .fontColor(Color.White) 90 } 91 .key('save') 92 .width('90%') 93 .height(70) 94 .backgroundColor('#0D9FFB') 95 .margin(20) 96 .onClick(this.getScreen) 97 } 98 } 99 .layoutWeight(1) 100 } 101 .width('100%').height('100%') 102 .backgroundColor('#F5F5F5') 103 } 104}