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