1# 预览(C/C++) 2 3预览是启动相机后看见的画面,通常在拍照和录像前执行。 4 5## 开发步骤 6 7详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/_o_h___camera.md)。 8 91. 导入NDK接口,接口中提供了相机相关的属性和方法,导入方法如下。 10 11 ```c++ 12 // 导入NDK接口头文件 13 #include "hilog/log.h" 14 #include "ohcamera/camera.h" 15 #include "ohcamera/camera_input.h" 16 #include "ohcamera/capture_session.h" 17 #include "ohcamera/photo_output.h" 18 #include "ohcamera/preview_output.h" 19 #include "ohcamera/video_output.h" 20 #include "ohcamera/camera_manager.h" 21 ``` 22 232. 在CMake脚本中链接相关动态库。 24 25 ```txt 26 target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 27 ``` 28 293. 获取SurfaceId。 30 31 XComponent组件为预览流提供的SurfaceId,而XComponent的能力由UI提供,相关介绍可参考[XComponent组件参考](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md)。 32 334. 根据传入的SurfaceId,通过[OH_CameraManager_GetSupportedCameraOutputCapability()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_getsupportedcameraoutputcapability)方法获取当前设备支持的预览能力。通过[OH_CameraManager_CreatePreviewOutput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_createpreviewoutput)方法创建预览输出流,其中,OH_CameraManager_CreatePreviewOutput()方法中的参数分别是cameraManager指针,previewProfiles数组中的第一项,步骤三中获取的surfaceId,以及返回的previewOutput指针。 34 35 ```c++ 36 NDKCamera::NDKCamera(char *str) 37 { 38 Camera_Manager *cameraManager = nullptr; 39 Camera_Device* cameras = nullptr; 40 Camera_OutputCapability* cameraOutputCapability = nullptr; 41 Camera_PreviewOutput* previewOutput = nullptr; 42 const Camera_Profile* previewProfile = nullptr; 43 uint32_t size = 0; 44 uint32_t cameraDeviceIndex = 0; 45 char* previewSurfaceId = str; 46 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 47 if (cameraManager == nullptr || ret != CAMERA_OK) { 48 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 49 } 50 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 51 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 52 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 53 } 54 ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 55 &cameraOutputCapability); 56 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 57 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 58 } 59 if (cameraOutputCapability->previewProfilesSize < 0) { 60 OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null"); 61 } 62 previewProfile = cameraOutputCapability->previewProfiles[0]; 63 64 ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput); 65 if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) { 66 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed."); 67 } 68 } 69 ``` 70 715. 使能。当session完成CommitConfig后通过调用[OH_CaptureSession_Start()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_start)方法输出预览流,接口调用失败会返回相应错误码,错误码类型参见[Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1)。 72 73 ```c++ 74 ret = OH_CaptureSession_Start(); 75 if (ret != CAMERA_OK) { 76 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); 77 } 78 ``` 79 806. 通过[OH_CaptureSession_Stop()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_stop)方法停止预览流,接口调用失败会返回相应错误码,错误码类型参见[Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1)。 81 82 ```c++ 83 ret = OH_CaptureSession_Stop(previewOutput); 84 if (ret != CAMERA_OK) { 85 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed."); 86 } 87 ``` 88 89## 状态监听 90 91在相机应用开发过程中,可以随时监听预览输出流状态,包括预览流启动、预览流结束、预览流输出错误。 92 93- 通过注册固定的frameStart回调函数获取监听预览启动结果,previewOutput创建成功时即可监听,预览第一次曝光时触发,有该事件返回结果则认为预览流已启动。 94 95 ```c++ 96 ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener()); 97 if (ret != CAMERA_OK) { 98 OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed."); 99 } 100 ``` 101 ```c++ 102 void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput) 103 { 104 OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart"); 105 } 106 PreviewOutput_Callbacks* GetPreviewOutputListener(void) 107 { 108 static PreviewOutput_Callbacks previewOutputListener = { 109 .onFrameStart = PreviewOutputOnFrameStart, 110 .onFrameEnd = PreviewOutputOnFrameEnd, 111 .onError = PreviewOutputOnError 112 }; 113 return &previewOutputListener; 114 } 115 ``` 116 117- 通过注册固定的frameEnd回调函数获取监听预览结束结果,previewOutput创建成功时即可监听,预览完成最后一帧时触发,有该事件返回结果则认为预览流已结束。 118 119 ```c++ 120 void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount) 121 { 122 OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount); 123 } 124 ``` 125 126- 通过注册固定的error回调函数获取监听预览输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1)。 127 128 ```c++ 129 void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode) 130 { 131 OH_LOG_INFO(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode); 132 } 133 ```