HarmonyOS Scan Kit 自定义扫码返回1000500001

import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, scanCore, customScan } from '@kit.ScanKit';

let options: scanBarcode.ScanOptions = {
  scanTypes: [scanCore.ScanType.ALL],
  enableMultiMode: true,
  enableAlbum: true
};

@Entry
@Component
struct customScanPage {
  // 设置预览流高度,默认单位:vp
  @State cameraHeight: number = 640
  // 设置预览流宽度,默认单位:vp
  @State cameraWidth: number = 360
  private mXComponentController: XComponentController = new XComponentController();

  build() {
    Stack() {
      XComponent({
        id: 'componentId',
        type: 'surface',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called.');
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          customScan.init(options);
          customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
            hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
          }).catch((error: BusinessError) => {
            hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
          });
        })// 预览流宽、高,默认单位vp,支持px、lpx、vp
        .height(this.cameraHeight)
        .width(this.cameraWidth)
        .position({ x: 0, y: 0 })
    }
    .alignContent(Alignment.Bottom)
    .height('100%')
    .width('100%')
    .position({ x: 0, y: 0 })
  }
}

代码为示例代码

问题:

相机权限授权了在跳转此页面进行扫码展示,但是进入就会报错Code: 1000500001, message: The internal error.

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

API参考里面给的是部分的代码不是整个端到端的流程。当前代码的问题是没有申请相机的权限。 可以参考自定义扫码指南查看

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/scan-customscan-V5

几个要点:

  1. module.json5 中添加camera的权限声明

  2. 拉起自定义扫码界面时需要动态申请相机的权限,等待用户授权

  3. 用户授权后在启动相机流进行扫码

  4. 获取扫码结果 附权限声明module.json5:

"requestPermissions":[{ 
  "name" : "ohos.permission.CAMERA", 
  "reason": "$string:reason", 
  "usedScene": { 
    "abilities": 
    [ "FormAbility" ], 
    "when":"inuse" 
  } 
}],
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 自定义咨询
96浏览 • 1回复 待解决
HarmonyOS 如何自定义页面
55浏览 • 1回复 待解决
HarmonyOS customScan.start报错1000500001
117浏览 • 1回复 待解决
HarmonyOS 自定义的恢复态API
64浏览 • 1回复 待解决
自定义界面预览画面出现拉伸
1870浏览 • 1回复 待解决
Scan Kit无法识别多个
1742浏览 • 1回复 待解决
HarmonyOS 自定义功能
27浏览 • 1回复 待解决