HarmonyOS C++侧创建相机输出流失败?

OH_CameraManager_CreatePhotoOutput(Camera_Manager* cameraManager, const Camera_Profile* profile, const char* surfaceId, Camera_PhotoOutput** photoOutput); 

创建输出流提示 Failed to get photoOutput surface,但是预览流正常。

NDKCamera::NDKCamera(char *previewId, char *photoId)
{
  Camera_Manager* cameraManager = nullptr;
  Camera_Device* cameras = nullptr;
  Camera_CaptureSession* captureSession = nullptr;
  Camera_OutputCapability* cameraOutputCapability = nullptr;
  const Camera_Profile* previewProfile = nullptr;
  const Camera_Profile* photoProfile = nullptr;
  Camera_PreviewOutput* previewOutput = nullptr;
  Camera_PhotoOutput* photoOutput = nullptr;
  Camera_Input* cameraInput = nullptr;
  uint32_t size = 0;
  uint32_t cameraDeviceIndex = 0;
  char* previewSurfaceId = previewId;
  char* photoSurfaceId = photoId;
  // 创建CameraManager对象

  OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed.previewId %{public}s photoId %{public}s",previewSurfaceId,photoSurfaceId);
  Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
  if (cameraManager == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed.ret : %{public}d", ret);
  }
  // 监听相机状态变化
  ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
  }

  // 获取相机列表
  ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
  if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
  }

  // 创建相机输入流
  ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
  if (cameraInput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
  }

  // 监听cameraInput错误信息
  ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
  }

  // 打开相机
  ret = OH_CameraInput_Open(cameraInput);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed.");
  }

  // 获取相机设备支持的输出流能力
  ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
    &cameraOutputCapability);
  if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
  }

  if (cameraOutputCapability->previewProfilesSize < 0) {
    OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
  }
  previewProfile = cameraOutputCapability->previewProfiles[0];

  if (cameraOutputCapability->photoProfilesSize < 0) {
    OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null");
  }
  photoProfile = cameraOutputCapability->photoProfiles[0];

  // 创建预览输出流,其中参数 previewSurfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface
  ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
  if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
  }else {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput success.previewSurfaceId : %{public}s",previewSurfaceId);
  }

  // 监听预览输出错误信息
  ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener());
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
  }

  // 创建拍照输出流,其中参数 photoSurfaceId 参考上文 ImageReceiver获取的surface
  ret = OH_CameraManager_CreatePhotoOutput(cameraManager, photoProfile, photoSurfaceId, &photoOutput);
  if (photoProfile == nullptr || photoOutput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePhotoOutput failed.ret : %{public}d, photoSurfaceId : %{public}s", ret, photoSurfaceId);
  }

  //创建会话
  ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
  if (captureSession == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.ret : %{public}d",ret);
  }

  // 监听session错误信息
  ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.ret : %{public}d",ret);
  }

  // 开始配置会话
  ret = OH_CaptureSession_BeginConfig(captureSession);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.ret : %{public}d",ret);
  }

  // 向会话中添加相机输入流
  ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.ret : %{public}d",ret);
  }

  // 向会话中添加预览输出流
  /*ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.ret : %{public}d",ret);
  }*/

  // 向会话中添加拍照输出流
  ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.ret : %{public}d",ret);
  }

  // 提交会话配置
  ret = OH_CaptureSession_CommitConfig(captureSession);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.ret : %{public}d",ret);
  }

  // 启动会话
  ret = OH_CaptureSession_Start(captureSession);
  if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.ret : %{public}d",ret);
  }
}
阅读 573
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进