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 router from '@system.router' 17import fileshare from '@ohos.fileshare' 18import wantConstant from '@ohos.ability.wantConstant' 19 20import { Log } from '../../../../../../common/src/main/ets/default/utils/Log' 21 22@Entry 23@Component 24struct ThirdPreviewView { 25 private TAG: string = '[ThirdPreviewView]:' 26 private photoWidth: string 27 private photoHeight: string 28 private photoUri: string 29 private videoUri: string 30 private mode: string 31 private callBundleName: string 32 @State controls: boolean = false 33 @State isShowVideoButton: boolean = true 34 myVideoController: VideoController = new VideoController() 35 36 aboutToAppear() { 37 let routerParams = router.getParams() 38 this.photoWidth = routerParams.width.toString() 39 Log.info(`${this.TAG} aboutToAppear photoWidth= ${this.photoWidth} E`) 40 this.photoHeight = routerParams.height.toString() 41 Log.info(`${this.TAG} aboutToAppear photoHeight= ${this.photoHeight} E`) 42 this.photoUri = routerParams.uri.toString() 43 Log.info(`${this.TAG} aboutToAppear photoUri= ${this.photoUri} E`) 44 this.mode = routerParams.mode.toString() 45 Log.info(`${this.TAG} aboutToAppear mode= ${this.mode} E`) 46 this.videoUri = routerParams.videoUri.toString() 47 Log.info(`${this.TAG} aboutToAppear videoUri= ${this.videoUri} E`) 48 this.callBundleName = routerParams.callBundleName.toString() 49 Log.info(`${this.TAG} aboutToAppear callBundleName= ${this.callBundleName} E`) 50 Log.info(`${this.TAG} aboutToAppear X`) 51 } 52 53 backCalledApp(resourceUri) { 54 Log.info(`${this.TAG} backCalledApp E`) 55 let that = this 56 fileshare.grantUriPermission(resourceUri, this.callBundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION) 57 .then(() => { 58 Log.info(`${this.TAG} grantUriPermission success`) 59 that.terminateSelfWithResult(resourceUri) 60 }).catch(function (error) { 61 Log.error(`${this.TAG} grantUriPermission error= ${error} `) 62 }) 63 Log.info(`${this.TAG} backCalledApp X`) 64 } 65 66 terminateSelfWithResult(resourceUri) { 67 Log.info(`${this.TAG} terminateSelfWithResult start`) 68 let abilityResult = { 69 "resultCode": 200, 70 "want": { 71 "parameters": { 72 "resourceUri": resourceUri, 73 "width": this.photoWidth, 74 "height": this.photoHeight 75 }, 76 "bundleName": "com.ohos.camera", 77 "abilityName": "com.ohos.camera.MainAbility" 78 } 79 }; 80 81 globalThis.cameraAbilityContext.terminateSelfWithResult(abilityResult, (error, data) => { 82 if (error) { 83 Log.error(`${this.TAG} Operation failed. Cause: ${error}`); 84 return; 85 } 86 Log.info(`${this.TAG} Operation succeeded: ${data}`); 87 }); 88 } 89 90 private getVideoPlayIcon() { 91 if (vp2px(1) >= 1 && vp2px(1) < 2) { 92 return $r('app.media.ic_video_play_btn_hdpi') 93 } else if (vp2px(1) == 2) { 94 return $r('app.media.ic_video_play_btn_xhdpi') 95 } else if (vp2px(1) == 3) { 96 return $r('app.media.ic_video_play_btn_xxhdpi') 97 } else { 98 return $r('app.media.ic_video_play_btn_xxxhdpi') 99 } 100 } 101 102 build() { 103 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 104 Stack() { 105 if (this.mode === "PHOTO") { 106 Column() { 107 Image(this.photoUri) 108 .width('100%') 109 .height('100%') 110 } 111 .width(this.photoWidth) 112 .height(this.photoHeight) 113 } else { 114 Video({ 115 src: this.videoUri, 116 previewUri: `${this.videoUri}/thumbnail/${this.photoWidth.split("px")[0]}/${this.photoHeight.split("px")[0]}`, 117 controller: this.myVideoController 118 }) 119 .controls(this.controls) 120 .objectFit(ImageFit.Contain) 121 .width(this.photoWidth) 122 .height(this.photoHeight) 123 .onClick(() => { 124 this.controls = !this.controls 125 }) 126 .onFinish(() => { 127 this.controls = true; 128 }) 129 .zIndex(1) 130 if(this.isShowVideoButton) { 131 Column() { 132 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 133 Image(this.getVideoPlayIcon()).objectFit(ImageFit.Contain).width(56).height(56) 134 .onClick(() => { 135 this.myVideoController.start() 136 this.isShowVideoButton = false 137 }) 138 } 139 }.zIndex(2) 140 } 141 } 142 } 143 .width(this.photoWidth) 144 .height(this.photoHeight) 145 Flex({ 146 direction: FlexDirection.Row, 147 alignItems: ItemAlign.Center, 148 justifyContent: FlexAlign.SpaceBetween 149 }) { 150 Image($r('app.media.ic_system_cancel')) 151 .width(24) 152 .aspectRatio(1) 153 .onClick(() => { 154 router.back() 155 }) 156 Image($r('app.media.ic_system_confirm')) 157 .width(24) 158 .aspectRatio(1) 159 .onClick(() => { 160 this.backCalledApp(this.mode === "PHOTO" ? this.photoUri : this.videoUri) 161 }) 162 }.width('100%').height(48).margin({ top: '24vp' }) 163 .padding({ top: '12vp', left: '24vp', right: '24vp' }) 164 .position({x:0, y:0}) 165 }.width('100%').height('100%').backgroundColor('#000') 166 } 167}