1e41f4b71Sopenharmony_ci# Camera Photographing (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciPhotographing is an important function of the camera application. Based on the complex logic of the camera hardware, the camera module provides APIs for you to set information such as resolution, flash, focal length, photo quality, and rotation angle. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci1. Import the NDK, which provides camera-related attributes and methods. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci ```c++ 12e41f4b71Sopenharmony_ci // Include the NDK header files. 13e41f4b71Sopenharmony_ci #include "hilog/log.h" 14e41f4b71Sopenharmony_ci #include "ohcamera/camera.h" 15e41f4b71Sopenharmony_ci #include "ohcamera/camera_input.h" 16e41f4b71Sopenharmony_ci #include "ohcamera/capture_session.h" 17e41f4b71Sopenharmony_ci #include "ohcamera/photo_output.h" 18e41f4b71Sopenharmony_ci #include "ohcamera/preview_output.h" 19e41f4b71Sopenharmony_ci #include "ohcamera/video_output.h" 20e41f4b71Sopenharmony_ci #include "ohcamera/camera_manager.h" 21e41f4b71Sopenharmony_ci ``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci2. Link the dynamic library in the CMake script. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci ```txt 26e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 27e41f4b71Sopenharmony_ci ``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci3. Obtain the surface ID. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci Call **createImageReceiver()** of the image module to create an **ImageReceiver** instance, and call **getReceivingSurfaceId()** of the instance to obtain the surface ID. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci4. Create a photo output stream. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci Based on the surface ID passed in, call **OH_CameraManager_GetSupportedCameraOutputCapability()** to obtain the photo output streams supported by the current device, and then call **OH_CameraManager_CreatePhotoOutput()** to pass in a supported output stream and the surface ID obtained in step 3 to create a photo output stream. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci ```c++ 38e41f4b71Sopenharmony_ci NDKCamera::NDKCamera(char *str) 39e41f4b71Sopenharmony_ci { 40e41f4b71Sopenharmony_ci Camera_Manager* cameraManager = nullptr; 41e41f4b71Sopenharmony_ci Camera_Device* cameras = nullptr; 42e41f4b71Sopenharmony_ci Camera_OutputCapability* cameraOutputCapability = nullptr; 43e41f4b71Sopenharmony_ci Camera_PhotoOutput* photoOutput = nullptr; 44e41f4b71Sopenharmony_ci Camera_CaptureSession* captureSession = nullptr; 45e41f4b71Sopenharmony_ci const Camera_Profile* photoProfile = nullptr; 46e41f4b71Sopenharmony_ci uint32_t size = 0; 47e41f4b71Sopenharmony_ci uint32_t cameraDeviceIndex = 0; 48e41f4b71Sopenharmony_ci char* photoSurfaceId = str; 49e41f4b71Sopenharmony_ci Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 50e41f4b71Sopenharmony_ci if (cameraManager == nullptr || ret != CAMERA_OK) { 51e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 54e41f4b71Sopenharmony_ci if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 55e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], &cameraOutputCapability); 58e41f4b71Sopenharmony_ci if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 59e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "GetSupportedCameraOutputCapability failed."); 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci photoProfile = cameraOutputCapability->photoProfiles[0]; 62e41f4b71Sopenharmony_ci if (photoProfile == nullptr) { 63e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "Get photoProfiles failed."); 64e41f4b71Sopenharmony_ci } 65e41f4b71Sopenharmony_ci ret = OH_CameraManager_CreatePhotoOutput(cameraManager, photoProfile, photoSurfaceId, &photoOutput); 66e41f4b71Sopenharmony_ci if (photoOutput == nullptr || ret != CAMERA_OK) { 67e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePhotoOutput failed."); 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci ``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci5. Set camera parameters. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci You can set camera parameters to adjust photographing functions, including the flash, zoom ratio, and focal length. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci ```c++ 77e41f4b71Sopenharmony_ci // Check whether the camera has flash. 78e41f4b71Sopenharmony_ci Camera_FlashMode flashMode = FLASH_MODE_AUTO; 79e41f4b71Sopenharmony_ci bool hasFlash = false; 80e41f4b71Sopenharmony_ci ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession); 81e41f4b71Sopenharmony_ci if (captureSession == nullptr || ret != CAMERA_OK) { 82e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed."); 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci ret = OH_CaptureSession_HasFlash(captureSession, &hasFlash); 85e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 86e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_HasFlash failed."); 87e41f4b71Sopenharmony_ci } 88e41f4b71Sopenharmony_ci if (hasFlash) { 89e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "hasFlash success"); 90e41f4b71Sopenharmony_ci } else { 91e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "hasFlash fail"); 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci // Check whether a flash mode is supported. 94e41f4b71Sopenharmony_ci bool isSupported = false; 95e41f4b71Sopenharmony_ci ret = OH_CaptureSession_IsFlashModeSupported(captureSession, flashMode, &isSupported); 96e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 97e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFlashModeSupported failed."); 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci if (isSupported) { 100e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "isFlashModeSupported success"); 101e41f4b71Sopenharmony_ci // Set the flash mode. 102e41f4b71Sopenharmony_ci ret = OH_CaptureSession_SetFlashMode(captureSession, flashMode); 103e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 104e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetFlashMode success."); 105e41f4b71Sopenharmony_ci } else { 106e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFlashMode failed. %{public}d ", ret); 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci // Obtain the flash mode in use. 109e41f4b71Sopenharmony_ci ret = OH_CaptureSession_GetFlashMode(captureSession, &flashMode); 110e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 111e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFlashMode success. flashMode: %{public}d ", flashMode); 112e41f4b71Sopenharmony_ci } else { 113e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFlashMode failed. %d ", ret); 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } else { 116e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "isFlashModeSupported fail"); 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci // Check whether the continuous auto focus is supported. 120e41f4b71Sopenharmony_ci Camera_FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO; 121e41f4b71Sopenharmony_ci bool isFocusModeSupported = false; 122e41f4b71Sopenharmony_ci ret = OH_CaptureSession_IsFocusModeSupported(captureSession, focusMode, &isFocusModeSupported); 123e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 124e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFocusModeSupported failed."); 125e41f4b71Sopenharmony_ci } 126e41f4b71Sopenharmony_ci if (isFocusModeSupported) { 127e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "isFocusModeSupported success"); 128e41f4b71Sopenharmony_ci ret = OH_CaptureSession_SetFocusMode(captureSession, focusMode); 129e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 130e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFocusMode failed. %{public}d ", ret); 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci ret = OH_CaptureSession_GetFocusMode(captureSession, &focusMode); 133e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 134e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFocusMode success. focusMode%{public}d ", focusMode); 135e41f4b71Sopenharmony_ci } else { 136e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFocusMode failed. %d ", ret); 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci } else { 139e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "isFocusModeSupported fail"); 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci // Obtain the zoom ratio range supported by the camera. 143e41f4b71Sopenharmony_ci float minZoom; 144e41f4b71Sopenharmony_ci float maxZoom; 145e41f4b71Sopenharmony_ci ret = OH_CaptureSession_GetZoomRatioRange(captureSession, &minZoom, &maxZoom); 146e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 147e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatioRange failed."); 148e41f4b71Sopenharmony_ci } else { 149e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatioRange success. minZoom: %{public}f, maxZoom:%{public}f", 150e41f4b71Sopenharmony_ci minZoom, maxZoom); 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci // Set a zoom ratio. 153e41f4b71Sopenharmony_ci ret = OH_CaptureSession_SetZoomRatio(captureSession, maxZoom); 154e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 155e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetZoomRatio success."); 156e41f4b71Sopenharmony_ci } else { 157e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetZoomRatio failed. %{public}d ", ret); 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci // Obtain the zoom ratio of the camera. 160e41f4b71Sopenharmony_ci ret = OH_CaptureSession_GetZoomRatio(captureSession, &maxZoom); 161e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 162e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatio success. zoom: %{public}f ", maxZoom); 163e41f4b71Sopenharmony_ci } else { 164e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatio failed. %{public}d ", ret); 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci ``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci6. Trigger photographing. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci Call **OH_PhotoOutput_Capture()**. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci ```c++ 173e41f4b71Sopenharmony_ci ret = OH_PhotoOutput_Capture(photoOutput); 174e41f4b71Sopenharmony_ci if (ret == CAMERA_OK) { 175e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_PhotoOutput_Capture success "); 176e41f4b71Sopenharmony_ci } else { 177e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_Capture failed. %d ", ret); 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci## Status Listening 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the status of the photo output stream, including the start of the photo stream, the start and end of the photo frame, and the errors of the photo output stream. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci- Register the **'onFrameStart'** event to listen for photographing start events. This event can be registered when a **PhotoOutput** instance is created and is triggered when the bottom layer starts exposure for photographing for the first time. The capture ID is returned. 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci ```c++ 188e41f4b71Sopenharmony_ci ret = OH_PhotoOutput_RegisterCallback(photoOutput, GetPhotoOutputListener()); 189e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 190e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_RegisterCallback failed."); 191e41f4b71Sopenharmony_ci } 192e41f4b71Sopenharmony_ci ``` 193e41f4b71Sopenharmony_ci ```c++ 194e41f4b71Sopenharmony_ci void PhotoOutputOnFrameStart(Camera_PhotoOutput* photoOutput) 195e41f4b71Sopenharmony_ci { 196e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PhotoOutputOnFrameStart"); 197e41f4b71Sopenharmony_ci } 198e41f4b71Sopenharmony_ci void PhotoOutputOnFrameShutter(Camera_PhotoOutput* photoOutput, Camera_FrameShutterInfo* info) 199e41f4b71Sopenharmony_ci { 200e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PhotoOutputOnFrameShutter"); 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci PhotoOutput_Callbacks* GetPhotoOutputListener() 203e41f4b71Sopenharmony_ci { 204e41f4b71Sopenharmony_ci static PhotoOutput_Callbacks photoOutputListener = { 205e41f4b71Sopenharmony_ci .onFrameStart = PhotoOutputOnFrameStart, 206e41f4b71Sopenharmony_ci .onFrameShutter = PhotoOutputOnFrameShutter, 207e41f4b71Sopenharmony_ci .onFrameEnd = PhotoOutputOnFrameEnd, 208e41f4b71Sopenharmony_ci .onError = PhotoOutputOnError 209e41f4b71Sopenharmony_ci }; 210e41f4b71Sopenharmony_ci return &photoOutputListener; 211e41f4b71Sopenharmony_ci } 212e41f4b71Sopenharmony_ci ``` 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci- Register the **'onFrameEnd'** event to listen for photographing end events. This event can be registered when a **PhotoOutput** instance is created. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci ```c++ 217e41f4b71Sopenharmony_ci void PhotoOutputOnFrameEnd(Camera_PhotoOutput* photoOutput, int32_t frameCount) 218e41f4b71Sopenharmony_ci { 219e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PhotoOutput frameCount = %{public}d", frameCount); 220e41f4b71Sopenharmony_ci } 221e41f4b71Sopenharmony_ci ``` 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci- Register the **'onError'** event to listen for photo output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1). 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci ```c++ 226e41f4b71Sopenharmony_ci void PhotoOutputOnError(Camera_PhotoOutput* photoOutput, Camera_ErrorCode errorCode) 227e41f4b71Sopenharmony_ci { 228e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PhotoOutput errorCode = %{public}d", errorCode); 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci ``` 231