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 { Log } from '@ohos/base/src/main/ets/utils/Log'; 17import WantConstant from '@ohos.ability.wantConstant'; 18import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants'; 19import { Constants } from '../../../common/model/common/Constants'; 20 21const TAG = "CameraGridItemComponent" 22 23// Camera entrance of the pick page 24@Component 25export struct CameraGridItemComponent { 26 bundleName: string = ""; 27 filterMediaType: number = MediaConstants.SELECT_TYPE_ALL; 28 resultUri: string = ""; 29 @Consume @Watch('onShow') isShow: boolean; 30 31 aboutToAppear(): void { 32 Log.info(TAG, `aboutToAppear`); 33 } 34 35 private onShow(): void { 36 Log.info(TAG, `onShow: isShow=${this.isShow}, uri=${this.resultUri}`); 37 if (this.isShow && this.resultUri?.length > 0) { 38 this.setPickResult(this.resultUri); 39 } 40 } 41 42 aboutToDisappear(): void { 43 Log.info(TAG, `aboutToDisappear`); 44 } 45 46 build() { 47 Column() { 48 Image($r('app.media.ic_public_camera_grid_item')) 49 .width($r('app.float.camera_icon_side')) 50 .height($r('app.float.camera_icon_side')) 51 .objectFit(ImageFit.Contain) 52 Text($r("app.string.camera_btn_text_photo")) 53 .fontSize($r('sys.float.ohos_id_text_size_body3')) 54 .fontFamily($r('app.string.id_text_font_family_regular')) 55 .fontColor($r('app.color.icon_secondary_color')) 56 .fontWeight(FontWeight.Regular) 57 .margin({ 58 top: $r("app.float.photo_grid_gap") 59 }) 60 } 61 .width("100%") 62 .aspectRatio(1) 63 .backgroundColor($r("app.color.recycle_album_bg_color")) 64 .justifyContent(FlexAlign.Center) 65 .alignItems(HorizontalAlign.Center) 66 .onClick((event: ClickEvent) => { 67 this.jumpCameraTakephoto().then((result) => { 68 Log.info(TAG, `resourceUri = ${result?.want?.parameters?.resourceUri}`); 69 this.resultUri = result?.want?.parameters?.resourceUri; 70 }).catch((err) => { 71 Log.error(TAG, `jumpCameraTakephoto err: ${err}`); 72 }); 73 }) 74 } 75 76 private async jumpCameraTakephoto(): Promise<any> { 77 // Currently, only photo pick is supported. 78 let action = this.filterMediaType == MediaConstants.SELECT_TYPE_VIDEO 79 ? WantConstant.Action.ACTION_IMAGE_CAPTURE 80 : WantConstant.Action.ACTION_IMAGE_CAPTURE; 81 let uri = this.filterMediaType == MediaConstants.SELECT_TYPE_VIDEO 82 ? Constants.CAMERA_TYPE_CAPTURE 83 : Constants.CAMERA_TYPE_CAPTURE; 84 let want = { 85 "action": action, 86 "bundleName": Constants.CAMERA_BUNDLE_NAME, 87 "abilityName": Constants.CAMERA_MAIN_ABILITY, 88 "parameters": { 89 uri: uri, 90 callBundleName: this.bundleName 91 } 92 }; 93 Log.debug(TAG, `jump camera want: ${JSON.stringify(want)}`); 94 let result = await globalThis.appContext .startAbilityForResult(want); 95 return result; 96 } 97 98 private setPickResult(uri: string): void { 99 if (uri == null || uri == undefined) { 100 Log.error(TAG, `no valid uri!`); 101 return; 102 } 103 let abilityResult = { 104 'resultCode': 0, 105 'want': { 106 'parameters': { 107 'select-item-list': [uri], 108 } 109 } 110 }; 111 globalThis.appContext.terminateSelfWithResult(abilityResult).then((result) => { 112 Log.info(TAG, `terminateSelf result: ${result}`); 113 }); 114 } 115} 116