使用Scan Kit(统一扫码服务)调用扫码

使用Scan Kit(统一扫码服务)调用扫码

HarmonyOS
2024-08-07 09:19:44
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hmogy
// BarcodePage.ets 默认界面扫码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { scanCore, scanBarcode } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG = 'ScanKit scanBarcode';
let typeStr: Array<string> =
  ['FORMAT_UNKNOWN', 'AZTEC_CODE', 'CODABAR_CODE', 'CODE39_CODE', 'CODE93_CODE', 'CODE128_CODE', 'DATAMATRIX_CODE',
    'EAN8_CODE', 'EAN13_CODE',
    'ITF14_CODE', 'PDF417_CODE', 'QR_CODE', 'UPC_A_CODE', 'UPC_E_CODE', ''];

@Entry
@Component
struct ScanBarcodePage {
  @State inputValue: string = ''
  @State scanType: number = -1

  build() {
    Column() {
      Button('默认界面扫码')
        .backgroundColor($r('sys.color.ohos_id_color_button_normal'))
        .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
        .align(Alignment.Center)
        .type(ButtonType.Capsule)
        .margin({ bottom: 12 })
        .width('90%')
        .height(40)
        .margin({ top: 5, bottom: 5 })
        .onClick(() => {
          // 定义扫码参数options
          let options: scanBarcode.ScanOptions = {
            scanTypes: [scanCore.ScanType.ALL],
            enableMultiMode: true,
            enableAlbum: true
          };
          try {
            scanBarcode.startScanForResult(getContext(this), options).then((result: scanBarcode.ScanResult) => {
              // 收到正确的扫码返回
              this.scanType = result.scanType;
              this.inputValue = result.originalValue;
              hilog.debug(0x0001, TAG,
                `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);
            }).catch((error: BusinessError) => {
              if (error.code == scanCore.ScanErrorCode.SCAN_SERVICE_CANCELED) {
                hilog.info(0x0001, TAG, `Disabling the Scanning Service.`);
              } else {
                hilog.error(0x0001, TAG, `Failed to start the scanning service. Code: ${error.code}`);
              }
            });
          } catch (error) {
            hilog.error(0x0001, TAG, `Failed to start scan. Code: ${error.code}`);
          }
        })

      CustomTextBox({ scanType: typeStr[this.scanType], inputValue: this.inputValue })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

@Component
export struct CustomTextBox {
  @Prop inputValue: string = ''
  @Prop scanType: string = ''

  build() {
    Column() {
      Column() {
        Text(this.scanType)
          .fontSize(20)
          .fontColor(Color.Black)
          .fontWeight(FontWeight.Normal)
          .align(Alignment.Center)
          .width('90%')
          .margin({ top: 5, bottom: 5 })
      }

      Column() {
        Text(this.inputValue)
          .fontSize(20)
          .fontColor(Color.Black)
          .fontWeight(FontWeight.Normal)
          .align(Alignment.Center)
          .width('90%')
          .margin({ top: 5, bottom: 5 })
      }
    }
  }
}
分享
微博
QQ
微信
回复
2024-08-07 11:26:59
相关问题
相册识别多失败
1537浏览 • 1回复 待解决
ArkTs怎么实现一扫功能?
4165浏览 • 1回复 待解决
JS如何实现手机功能?
2645浏览 • 1回复 待解决
Scan Kit无法识别多个
1511浏览 • 1回复 待解决
HarmonyOS 需要实现音效播放问题
124浏览 • 1回复 待解决
自定义界面预览画面出现拉伸
1627浏览 • 1回复 待解决
Arkts开发 api9 中怎么实现功能?
2664浏览 • 1回复 待解决
编译后的app包如何可以安装?
52浏览 • 1回复 待解决
HarmonyOS 如何获取mnc
92浏览 • 1回复 待解决