HarmonyOS 鸿蒙Next大佬们麻烦看看,为什么下面这个扫一扫的demo报1000500001,现象是黑屏呀

HarmonyOS 鸿蒙Next大佬们麻烦看看,为什么下面这个扫一扫的demo报1000500001,现象是黑屏呀 MatePad用Api12,DevEco Studio版本是5.0.5.300,扫一扫demo,报1000500001错误,下面是代码,我该怎么操作?

import { Logger } from '../common/utils/Logger';
import { customScan, scanBarcode } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

@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(() => {
          Logger.info('[Scan Sample] '+ 'onLoad is called')
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          Logger.info( 'viewControl '+`onLoad surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          try {
            customScan.start(viewControl, (error: BusinessError, scanResult: Array<scanBarcode.ScanResult>) => {
              if (error) {
                Logger.error( '[Scan Sample] start failed , error: '+ JSON.stringify(error))
                return;
              }
              Logger.info( '[Scan Sample] callback scan result: '+ JSON.stringify(scanResult))
            });
          } catch (error) {
            Logger.error( '[Scan Sample] start failed , error: '+ JSON.stringify(error))
          }
        })
          // 预览流宽、高,默认单位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 })
  }
}

更多关于HarmonyOS 鸿蒙Next大佬们麻烦看看,为什么下面这个扫一扫的demo报1000500001,现象是黑屏呀的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

楼主提供一下全量日志看看

另外确认一下是否有动态申请相机权限,是否有调用customScan.init

更多关于HarmonyOS 鸿蒙Next大佬们麻烦看看,为什么下面这个扫一扫的demo报1000500001,现象是黑屏呀的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS Next中扫描功能报错1000500001并出现黑屏现象,可能与相机权限未正确配置或相机资源未正常初始化有关。首先,确保在config.json文件中正确声明了相机权限:

{
  "module": {
    "reqPermissions": [
      {
        "name": "ohos.permission.CAMERA",
        "reason": "Required for scanning QR codes"
      }
    ]
  }
}

其次,检查代码中是否在onPageShow生命周期中正确初始化了相机资源,并确保相机预览视图已正确设置。如果相机资源未正确初始化或预览视图未绑定,可能导致黑屏。示例代码如下:

onPageShow() {
  const context = getContext(this);
  const cameraManager = context.getSystemService(Context.CAMERA_SERVICE);
  const camera = cameraManager.getCamera();
  camera.setPreviewDisplay(this.$element('previewView'));
  camera.startPreview();
}

此外,确保设备支持相机功能,并且相机硬件无故障。如果问题仍然存在,可能是系统或SDK的兼容性问题。

回到顶部