调用相机报错HCameraServiceProxy CreateCameraDevice failed, error: 13

frank_code 显示全部楼层 发表于 2024-9-10 14:07:27

【问题描述】

  1. 介绍问题现象和发生的背景 OpenHarmony api10,调用相机报错

    image.png

  2. 相关的代码

    
    import camera from '@ohos.multimedia.camera'
    import common from '@ohos.app.ability.common';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    const PERMISSIONS: Array<string> = [
      'ohos.permission.CAMERA']
    let previewWidth = 1920;
    let previewHeight =1080;
    @Entry
    @Component
    struct Index {
      private mXComponentController: XComponentController = new XComponentController()
      private surfaceId: string = '-1'
    
      async initCamera(surfaceId: string){
        //动态获取隐私权限
        let context = getContext(this) as common.Context
        // await context.requestPermissionsFromUser(PERMISSIONS)
        // console.log('grantPermission,requestPermissionsFromUser');
        // 创建CameraManager对象
        let cameraManager = await camera.getCameraManager(context)
        if (!cameraManager) {
          console.error('Failed to get the CameraManager instance');
        }
        // 获取相机列表
        let cameraArray = await cameraManager.getSupportedCameras()
        if (!cameraArray) {
          console.error('Failed to get the cameras');
        }
        for (let index = 0; index < cameraArray.length; index++) {
          console.log('cameraId : ' + cameraArray[index].cameraId)                          // 获取相机ID
          console.log('cameraPosition : ' + cameraArray[index].cameraPosition)              // 获取相机位置
          console.log('cameraType : ' + cameraArray[index].cameraType)                      // 获取相机类型
          console.log('connectionType : ' + cameraArray[index].connectionType)              // 获取相机连接类型
        }
    
        // 创建相机输入流
        let cameraInput = await cameraManager.createCameraInput(cameraArray[2])
    
        //  打开相机
        await cameraInput.open();
    
        // 获取相机设备支持的输出流能力
        let cameraOutputCap = await cameraManager.getSupportedOutputCapability(cameraArray[2]);
        if (!cameraOutputCap) {
          console.error("outputCapability outputCapability == null || undefined")
        } else {
          console.log("outputCapability: " + JSON.stringify(cameraOutputCap));
        }
    
        //获取相机支持的输出能力--支持的预览配置信息
        let previewProfilesArray = cameraOutputCap.previewProfiles;
        if (!previewProfilesArray) {
          console.error("createOutput previewProfilesArray == null || undefined")
        }else{
          console.log("previewProfiles:"+JSON.stringify(previewProfilesArray[0]))
          previewWidth = previewProfilesArray[0].size.width;
          previewHeight = previewProfilesArray[0].size.height;
        }
    
        // 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
        let previewOutput = await cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId)
        if (!previewOutput) {
          console.error("Failed to create the PreviewOutput instance.")
        }else{
          console.log("create the PreviewOutput instance succ.")
        }
    
        //创建会话
        let captureSession = await cameraManager.createCaptureSession()
        if (!captureSession) {
          console.error('Failed to create the CaptureSession instance.');
          return;
        }
        console.log('Callback returned with the CaptureSession instance.' + captureSession);
    
        // 开始配置会话
        try {
          captureSession.beginConfig();
        } catch (error) {
          let err = error as BusinessError;
          console.error('Failed to beginConfig. errorCode = ' + err.code);
        }
    
        // 向会话中添加相机输入流
        try {
          captureSession.addInput(cameraInput);
        } catch (error) {
          let err = error as BusinessError;
          console.error('Failed to addInput. errorCode = ' + err.code);
        }
    
        // 向会话中添加预览输出流
        try {
          captureSession.addOutput(previewOutput);
        } catch (error) {
          let err = error as BusinessError;
          console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code);
        }
    
        // // 向会话中添加拍照输出流
        // try {
        //   captureSession.addOutput(photoOutput);
        // } catch (error) {
        //   let err = error as BusinessError;
        //   console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code);
        // }
    
        // 提交会话配置
        await captureSession.commitConfig();
    
        // 启动会话
        await captureSession.start().then(() => {
          console.info('Promise returned to indicate the session start success.');
        });
      }
    
      build() {
        Flex() {
          XComponent({                                                                     // 创建XComponent
            id: '',
            type: 'surface',
            libraryname: '',
            controller: this.mXComponentController
          })
            .onLoad(() => {                                                                  // 设置onload回调
              // 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
              this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:previewWidth,surfaceHeight:previewHeight})
              // 获取Surface ID
              this.surfaceId = this.mXComponentController.getXComponentSurfaceId()
              this.initCamera(this.surfaceId)
            })
            .width('100%')                                                                 // 设置XComponent宽度
            .height('100%')                                                                // 设置XComponent高度
        }
      }
    
    }
  3. 我想要达到的结果

    期望调用相机成功,不报错

【运行环境】

硬件:RK3588 ROM版本:OpenHarmony 4.0 DevEvoStudio版本:5.0.0 SDK版本:API10

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论3

frank_code

沙发 发表于 2024-9-10 15:28:13
已解决,授权失败出现的

kuaile

板凳 发表于 2024-9-26 16:01:35
当前最新为api12,请在api12上验证是否有问题。api10跨度太大当前部分接口和错误码都已改变或者修复。

frank_code

发表于 2024-9-27 16:59  IP属地: 湖北省

回复 kuaile: 目前暂时没有api12环境

【1 条回复】

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部