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

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

HarmonyOS
2024-08-07 09:19:44
1980浏览
收藏 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 })
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
分享
微博
QQ
微信
回复
2024-08-07 11:26:59
相关问题
HarmonyOS 服务权限问题
941浏览 • 1回复 待解决
HarmonyOS 系统一扫问题
619浏览 • 1回复 待解决
ArkTs怎么实现一扫功能?
6063浏览 • 1回复 待解决
相册识别多失败
2682浏览 • 1回复 待解决
HarmonyOS 一扫功能的Demo
732浏览 • 1回复 待解决
HarmonyOS 自定义一扫功能
787浏览 • 1回复 待解决
HarmonyOS 直达问题
819浏览 • 1回复 待解决
HarmonyOS 需要demo
912浏览 • 1回复 待解决
HarmonyOS H5调用原生功能
1034浏览 • 1回复 待解决
Scan Kit无法识别多个
2421浏览 • 1回复 待解决
HarmonyOS “模块”通用能力
900浏览 • 1回复 待解决
HarmonyOS 是否有功能
781浏览 • 1回复 待解决