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