• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 Logger from '../../utils/Logger';
17import CameraModel from '../../model/CameraModel';
18import grantPermission from '../../utils/PermissionUtils';
19import display from '@ohos.display';
20import router from '@ohos.router';
21import emitter from '@ohos.events.emitter';
22import animator from '@ohos.animator';
23import { BusinessError } from '@ohos.base';
24
25const TAG: string = '[CameraPage]';
26enum CameraMode {
27  MODE_PHOTO = 0, // 拍照模式
28  MODE_VIDEO = 1 // 录像模式
29};
30let innerEventText: emitter.InnerEvent = {
31  eventId: 1,
32  priority: emitter.EventPriority.HIGH
33};
34
35@Entry
36@Component
37struct CameraPage {
38  private surfaceId: string = '-1';
39  private mXComponentController: XComponentController = new XComponentController();
40  private cameraModel: CameraModel = new CameraModel();
41  private windowWidth: number = 300;
42  private windowHeight: number = 300;
43  @State imageThumbnail: string = '';
44  @State videoThumbnail: Resource | undefined = undefined;
45  @State currentModel: number = CameraMode.MODE_PHOTO;
46  @State isRecording: boolean = false;
47  @State textMove: number = 45;
48  @State isPointShow: boolean = true;
49  @State isTitleShow: boolean = true;
50  @State timeShow: boolean = false;
51  @State translateY: number = 80;
52  @State count: number = 0;
53  @State duration: number = 2500;
54  @State resultInit: string = '';
55  @State text: string = '';
56  @State isShow: boolean = false;
57
58  async aboutToAppear() {
59    this.isShow = true;
60    this.windowWidth = display.getDefaultDisplaySync().width;
61    this.windowHeight = display.getDefaultDisplaySync().height;
62    Logger.info(TAG, `windowWidth  ${JSON.stringify(this.windowWidth)}`);
63    Logger.info(TAG, `windowHeight  ${JSON.stringify(this.windowHeight)}`);
64    await grantPermission().then(res => {
65      Logger.info(TAG, `权限申请成功  ${JSON.stringify(res)}`);
66      if (this.surfaceId) {
67        this.cameraModel.initCamera(this.surfaceId);
68      }
69    }).catch((rej: BusinessError) => {
70      Logger.info(TAG, `权限申请失败  ${JSON.stringify(rej)}`);
71    })
72    this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
73    Logger.info(TAG, `aboutToAppear,surfaceId=${this.surfaceId}`);
74  }
75
76  onPageShow() {
77    this.isShow = true;
78    if (!this.isShow) {
79      this.cameraModel.initCamera(this.surfaceId);
80    }
81    Logger.info(TAG, `onPageShow,onPageShow=${this.count}`);
82    this.text = '';
83    setTimeout(() => {
84      this.animationStart();
85    }, 1000)
86  }
87
88  onPageHide() {
89    Logger.info(TAG, `onPageHide,=${this.count}`);
90    // this.cameraModel.cameraRelease();
91    this.count = 0;
92    this.isShow = false;
93  }
94
95  async aboutToDisappear() {
96    emitter.off(innerEventText.eventId);
97    this.cameraModel.cameraRelease();
98    Logger.info(TAG, 'aboutToDisappear,}');
99  }
100
101  animationStart() {
102    Logger.info(TAG, `animationScan start`);
103    if (this.isShow) {
104      Logger.info(TAG, `animationScan 1`);
105      this.translateY = 20
106      animateTo({
107        duration: 2000,
108        curve: Curve.EaseOut,
109        iterations: -1,
110        playMode: PlayMode.Normal,
111        onFinish: () => {
112          console.info('play end')
113        }
114      }, () => {
115        this.translateY = 400
116      })
117    } else {
118      Logger.info(TAG, `animationScan 2`);
119      animateTo({}, () => {
120        this.translateY = 80
121      })
122    }
123  }
124
125  build() {
126    if (this.isShow) {
127      Stack() {
128        Stack({ alignContent: Alignment.Top }) {
129          XComponent({
130            id: 'componentId',
131            type: 'surface',
132            controller: this.mXComponentController
133          })
134            .onLoad(() => {
135              Logger.info(TAG, 'onLoad is called');
136              this.mXComponentController.setXComponentSurfaceSize({
137                surfaceWidth: this.windowWidth,
138                surfaceHeight: this.windowHeight
139              });
140              this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
141              Logger.info(TAG, `onLoad surfaceId: ${this.surfaceId}`);
142              this.cameraModel?.initCamera(this.surfaceId);
143              emitter.once(innerEventText, (res) => {
144                this.text = res.data && res.data.text;
145                Logger.info(TAG, `this.text==on==${this.text}`);
146                if (this.text !== '' && this.count === 0) {
147                  this.count++;
148                  Logger.info(TAG, `this.count,this.count=${this.count}`);
149                  AppStorage.setOrCreate<string>('text', this.text);
150                  router.pushUrl({
151                    url: 'pages/messagePage/Message'
152                  });
153                  setTimeout(() => {
154                    this.animationStart();
155                  }, 1000)
156                }
157              });
158            })
159            .width('100%')
160            .height('100%')
161            .layoutWeight(1)
162
163          Image($r('app.media.ic_lattice'))
164            .width('100%')
165            .height('95%')
166
167          Row() {
168            Image($r('app.media.icon'))
169              .width(26)
170              .height(26)
171              .margin({ left: 20 })
172              .onClick(() => {
173                Logger.info(TAG, 'back===11=');
174                router.back();
175                Logger.info(TAG, 'back===Get=');
176                this.cameraModel.cameraRelease();
177                Logger.info(TAG, 'back===cameraRelease=');
178                Logger.info(TAG, 'back===back=');
179              })
180            Blank()
181            Image($r('app.media.icon'))
182              .width(32)
183              .height(32)
184              .margin({ right: 20 })
185          }
186          .margin({ top: 20 })
187          .width('100%')
188
189          Image($r('app.media.ic_dot_scanning'))
190            .margin({ top: 80 })
191            .width('100%')
192            .translate({ y: this.translateY })
193        }
194        .width('100%')
195        .height('100%')
196
197        Column() {
198          Row() {
199            Text($r('app.string.text_description'))
200              .fontColor(Color.White)
201              .fontSize(16)
202          }
203
204          Row() {
205            Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) {
206              Image($r('app.media.icon'))
207                .width(40)
208                .height(40)
209
210              Image($r('app.media.icon'))
211                .width(45)
212                .height(45)
213                .opacity(0)
214
215              Image($r("app.media.icon"))
216                .width(45)
217                .height(45)
218            }
219          }
220          .margin({ top: 20 })
221
222          Row() {
223            Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) {
224              Text($r('app.string.camera_sweep'))
225                .fontSize(16)
226                .fontColor(Color.White)
227
228              Text($r('app.string.camera_pictures'))
229                .fontSize(16)
230                .fontColor(Color.White)
231
232              Text($r('app.string.camera_pai'))
233                .fontSize(16)
234                .fontColor(Color.White)
235            }
236            .align(Alignment.Bottom)
237            .height('100%')
238            .width('100%')
239          }
240          .margin({ top: 30 })
241        }
242        .width('100%')
243        .height('20%')
244        .position({ x: 0, y: '80%' })
245      }
246      .width('100%')
247      .height('100%')
248    }
249  }
250}