• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 image from '@ohos.multimedia.image'
16import prompt from '@ohos.prompt'
17import jsQR from 'jsqr'
18import Logger from '../model/Logger'
19import MediaUtils from '../model/MediaUtils'
20import TitleBar from '../common/TitleBar'
21
22const TAG: string = '[ParasQRCode]'
23const QRCODE_SIZE: number = px2vp(250) // 二维码的大小
24
25@Entry
26@Component
27struct ParseQRCode {
28  private settings: RenderingContextSettings = new RenderingContextSettings(true)
29  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
30  private defaultQrcodeImage: ImageBitmap = new ImageBitmap("common/media/qrcode.png")
31  private mediaUitl: MediaUtils = new MediaUtils(getContext(this))
32  @State result: string = ''
33  @State uploadQrcodeImage: image.PixelMap | undefined = undefined
34  getDecode = async () => {
35    let data = this.context.getImageData(0, 0, QRCODE_SIZE, QRCODE_SIZE)
36    let time = Date.now()
37    let result = jsQR(data.data, data.width, data.height, { inversionAttempts: "dontInvert" })
38    if (result === null) {
39      this.result = '解析失败!'
40    } else {
41      this.result = `解析为:${result.data} , 耗时:${Date.now() - time}ms`
42    }
43  }
44
45  @Builder
46  OperationBtn(text: Resource, handleClick: () => void) {
47    Button({ type: ButtonType.Capsule }) {
48      Text(text)
49        .fontSize(30)
50        .fontColor(Color.White)
51    }
52    .width('90%')
53    .height(70)
54    .backgroundColor('#0D9FFB')
55    .margin(20)
56    .onClick(handleClick)
57  }
58
59  build() {
60    Column() {
61      TitleBar({ hasBackPress: true })
62      Scroll() {
63        Column() {
64          Canvas(this.context)
65            .width(QRCODE_SIZE)
66            .aspectRatio(1)
67            .backgroundColor('#F0F0F0')
68            .margin({ top: 40 })
69            .onReady(() => {
70              this.context.imageSmoothingEnabled = false
71              this.context.clearRect(0, 0, QRCODE_SIZE, QRCODE_SIZE)
72              if (this.uploadQrcodeImage) {
73                this.context.drawImage(this.uploadQrcodeImage, 0, 0, QRCODE_SIZE, QRCODE_SIZE)
74              } else {
75                this.context.drawImage(this.defaultQrcodeImage, 0, 0, QRCODE_SIZE, QRCODE_SIZE)
76              }
77            })
78          Text(this.result)
79            .fontSize(25)
80            .width('90%')
81            .margin({ top: 40, bottom: 40 })
82          this.OperationBtn($r('app.string.check_qrcode_file'), () => {
83            let mContext = getContext(this) as any
84            mContext.startAbilityForResult(
85              {
86                bundleName: "com.ohos.photos",
87                abilityName: "com.ohos.photos.MainAbility",
88                parameters: {
89                  uri: "singleselect"
90                }
91              }
92            ).then(async (data) => {
93              this.result = ''
94              let want = data['want']
95              if (want != null && want != undefined) {
96                let param = want['parameters']
97                if (param != null && param != undefined) {
98                  let result = param['select-item-list']
99                  let images = result.split(';')
100                  Logger.info(TAG, `this.images = ${JSON.stringify(images)}`)
101                  this.uploadQrcodeImage = await this.mediaUitl.getPixelMap(images[0])
102                  if (this.uploadQrcodeImage === null) {
103                    prompt.showToast({ message: '所选文件不是图片', duration: 1000 })
104                    return
105                  }
106                  this.context.clearRect(0, 0, QRCODE_SIZE, QRCODE_SIZE)
107                  this.context.drawImage(this.uploadQrcodeImage, 0, 0, QRCODE_SIZE, QRCODE_SIZE)
108                }
109              }
110            })
111          })
112
113          this.OperationBtn($r('app.string.parse_qrcode'), () => {
114            Logger.info(TAG, `paras`)
115            this.result = '解析中,请等待'
116            setTimeout(() => {
117              this.getDecode()
118            }, 10)
119          })
120        }
121      }
122      .layoutWeight(1)
123    }
124    .width('100%')
125    .height('100%')
126  }
127}