1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cpp/camera_manager.h"
17 
18 #define LOG_TAG "DEMO:"
19 #define LOG_DOMAIN 0x3200
20 
21 namespace OHOS_NDK_CAMERA {
22 NDKCamera* NDKCamera::ndkCamera_ = nullptr;
23 std::mutex NDKCamera::mtx_;
24 const uint32_t NDKCamera::width_ = 1920;
25 const uint32_t NDKCamera::height_ = 1080;
26 
NDKCamera(char * str,uint32_t focusMode,uint32_t cameraDeviceIndex)27 NDKCamera::NDKCamera(char* str, uint32_t focusMode, uint32_t cameraDeviceIndex)
28     : previewSurfaceId_(str), cameras_(nullptr), focusMode_(focusMode),
29       cameraDeviceIndex_(cameraDeviceIndex),
30       cameraOutputCapability_(nullptr), cameraInput_(nullptr),
31       captureSession_(nullptr), size_(0),
32       isCameraMuted_(nullptr), profile_(nullptr),
33       photoSurfaceId_(nullptr), previewOutput_(nullptr), photoOutput_(nullptr),
34       metaDataObjectType_(nullptr), metadataOutput_(nullptr), isExposureModeSupported_(false),
35       isFocusModeSupported_(false), exposureMode_(EXPOSURE_MODE_LOCKED),
36       minExposureBias_(0), maxExposureBias_(0), step_(0),
37       videoProfile_(nullptr), videoOutput_(nullptr)
38 {
39     valid_ = false;
40     ReleaseCamera();
41     Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager_);
42     if (cameraManager_ == nullptr || ret != CAMERA_OK) {
43         OH_LOG_ERROR(LOG_APP, "Get CameraManager failed.");
44     }
45 
46     ret = OH_CameraManager_CreateCaptureSession(cameraManager_, &captureSession_);
47     if (captureSession_ == nullptr || ret != CAMERA_OK) {
48         OH_LOG_ERROR(LOG_APP, "Create captureSession failed.");
49     }
50     CaptureSessionRegisterCallback();
51     GetSupportedCameras();
52     GetSupportedOutputCapability();
53     CreatePreviewOutput();
54     CreateCameraInput();
55     CameraInputOpen();
56     CameraManagerRegisterCallback();
57     SessionFlowFn();
58     valid_ = true;
59 }
60 
~NDKCamera()61 NDKCamera::~NDKCamera()
62 {
63     valid_ = false;
64     OH_LOG_INFO(LOG_APP, "~NDKCamera");
65     Camera_ErrorCode ret = CAMERA_OK;
66 
67     if (cameraManager_) {
68         OH_LOG_ERROR(LOG_APP, "Release OH_CameraManager_DeleteSupportedCameras. enter");
69         ret = OH_CameraManager_DeleteSupportedCameras(cameraManager_, cameras_, size_);
70         if (ret != CAMERA_OK) {
71             OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
72         } else {
73             OH_LOG_ERROR(LOG_APP, "Release OH_CameraManager_DeleteSupportedCameras. ok");
74         }
75 
76         ret = OH_CameraManager_DeleteSupportedCameraOutputCapability(cameraManager_, cameraOutputCapability_);
77         if (ret != CAMERA_OK) {
78             OH_LOG_ERROR(LOG_APP, "Delete CameraOutputCapability failed.");
79         } else {
80             OH_LOG_ERROR(LOG_APP, "Release OH_CameraManager_DeleteSupportedCameraOutputCapability. ok");
81         }
82 
83         ret = OH_Camera_DeleteCameraManager(cameraManager_);
84         if (ret != CAMERA_OK) {
85             OH_LOG_ERROR(LOG_APP, "Delete CameraManager failed.");
86         } else {
87             OH_LOG_ERROR(LOG_APP, "Release OH_Camera_DeleteCameraManager. ok");
88         }
89         cameraManager_ = nullptr;
90     }
91     OH_LOG_INFO(LOG_APP, "~NDKCamera exit");
92 }
93 
ReleaseCamera(void)94 Camera_ErrorCode NDKCamera::ReleaseCamera(void)
95 {
96     OH_LOG_INFO(LOG_APP, "ReleaseCamera enter.");
97     if (previewOutput_) {
98         (void)PreviewOutputStop();
99         (void)PreviewOutputRelease();
100     }
101     if (photoOutput_) {
102         (void)PhotoOutputRelease();
103     }
104     if (captureSession_) {
105         (void)SessionRealese();
106     }
107     if (cameraInput_) {
108         (void)CameraInputClose();
109     }
110     OH_LOG_INFO(LOG_APP, "ReleaseCamera exit.");
111     return CAMERA_OK;
112 }
ReleaseSession(void)113 Camera_ErrorCode NDKCamera::ReleaseSession(void)
114 {
115     OH_LOG_INFO(LOG_APP, "ReleaseSession enter.");
116     (void)PreviewOutputStop();
117     (void)PhotoOutputRelease();
118     (void)SessionRealese();
119     OH_LOG_INFO(LOG_APP, "ReleaseSession exit.");
120     return CAMERA_OK;
121 }
SessionRealese(void)122 Camera_ErrorCode NDKCamera::SessionRealese(void)
123 {
124     OH_LOG_INFO(LOG_APP, "SessionRealese enter.");
125     Camera_ErrorCode ret = OH_CaptureSession_Release(captureSession_);
126     captureSession_ = nullptr;
127     OH_LOG_INFO(LOG_APP, "SessionRealese exit.");
128     return ret;
129 }
130 
HasFlashFn(uint32_t mode)131 Camera_ErrorCode NDKCamera::HasFlashFn(uint32_t mode)
132 {
133     Camera_FlashMode flashMode = static_cast<Camera_FlashMode>(mode);
134     // 检测是否有闪关灯
135     bool hasFlash = false;
136     Camera_ErrorCode ret = OH_CaptureSession_HasFlash(captureSession_, &hasFlash);
137     if (captureSession_ == nullptr || ret != CAMERA_OK) {
138         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_HasFlash failed.");
139     }
140     if (hasFlash) {
141         OH_LOG_INFO(LOG_APP, "hasFlash succeeed");
142     } else {
143         OH_LOG_ERROR(LOG_APP, "hasFlash failed");
144     }
145 
146     // 检测闪光灯模式是否支持
147     bool isSupported = false;
148     ret = OH_CaptureSession_IsFlashModeSupported(captureSession_, flashMode, &isSupported);
149     if (ret != CAMERA_OK) {
150         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFlashModeSupported failed.");
151     }
152     if (isSupported) {
153         OH_LOG_INFO(LOG_APP, "isFlashModeSupported succeed");
154     } else {
155         OH_LOG_ERROR(LOG_APP, "isFlashModeSupported failed");
156     }
157 
158     // 设置闪光灯模式
159     ret = OH_CaptureSession_SetFlashMode(captureSession_, flashMode);
160     if (ret == CAMERA_OK) {
161         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetFlashMode success.");
162     } else {
163         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFlashMode failed, ret = %{public}d.", ret);
164     }
165 
166     // 获取当前设备的闪光灯模式
167     ret = OH_CaptureSession_GetFlashMode(captureSession_, &flashMode);
168     if (ret == CAMERA_OK) {
169         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFlashMode success. flashMode:%{public}d ", flashMode);
170     } else {
171         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFlashMode failed, ret = %{public}d.", ret);
172     }
173     return ret;
174 }
175 
IsVideoStabilizationModeSupportedFn(uint32_t mode)176 Camera_ErrorCode NDKCamera::IsVideoStabilizationModeSupportedFn(uint32_t mode)
177 {
178     Camera_VideoStabilizationMode videoMode = static_cast<Camera_VideoStabilizationMode>(mode);
179     // 查询是否支持指定的视频防抖模式
180     bool isSupported = false;
181     Camera_ErrorCode ret = OH_CaptureSession_IsVideoStabilizationModeSupported(
182         captureSession_, videoMode, &isSupported);
183     if (ret != CAMERA_OK) {
184         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsVideoStabilizationModeSupported failed.");
185     }
186     if (isSupported) {
187         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_IsVideoStabilizationModeSupported succeed");
188     } else {
189         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsVideoStabilizationModeSupported failed");
190     }
191 
192     // 设置视频防抖
193     ret = OH_CaptureSession_SetVideoStabilizationMode(captureSession_, videoMode);
194     if (ret == CAMERA_OK) {
195         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetVideoStabilizationMode success.");
196     } else {
197         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetVideoStabilizationMode failed, ret = %{public}d.", ret);
198     }
199 
200     ret = OH_CaptureSession_GetVideoStabilizationMode(captureSession_, &videoMode);
201     if (ret == CAMERA_OK) {
202         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetVideoStabilizationMode success. videoMode:%{public}f ", videoMode);
203     } else {
204         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetVideoStabilizationMode failed, ret = %{public}d.", ret);
205     }
206     return ret;
207 }
208 
setZoomRatioFn(uint32_t zoomRatio)209 Camera_ErrorCode NDKCamera::setZoomRatioFn(uint32_t zoomRatio)
210 {
211     float zoom = float(zoomRatio);
212     // 获取支持的变焦范围
213     float minZoom;
214     float maxZoom;
215     Camera_ErrorCode ret = OH_CaptureSession_GetZoomRatioRange(captureSession_, &minZoom, &maxZoom);
216     if (captureSession_ == nullptr || ret != CAMERA_OK) {
217         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatioRange failed.");
218     } else {
219         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatioRange success. minZoom: %{public}f, maxZoom:%{public}f",
220             minZoom, maxZoom);
221     }
222 
223     // 设置变焦
224     ret = OH_CaptureSession_SetZoomRatio(captureSession_, zoom);
225     if (ret == CAMERA_OK) {
226         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetZoomRatio success.");
227     } else {
228         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetZoomRatio failed, ret = %{public}d.", ret);
229     }
230 
231     // 获取当前设备的变焦值
232     ret = OH_CaptureSession_GetZoomRatio(captureSession_, &zoom);
233     if (ret == CAMERA_OK) {
234         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatio success. zoom:%{public}f ", zoom);
235     } else {
236         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatio failed, ret = %{public}d.", ret);
237     }
238     return ret;
239 }
240 
SessionBegin(void)241 Camera_ErrorCode NDKCamera::SessionBegin(void)
242 {
243     Camera_ErrorCode ret =  OH_CaptureSession_BeginConfig(captureSession_);
244     if (ret == CAMERA_OK) {
245         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_BeginConfig success.");
246     } else {
247         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed, ret = %{public}d.", ret);
248     }
249     return ret;
250 }
251 
SessionCommitConfig(void)252 Camera_ErrorCode NDKCamera::SessionCommitConfig(void)
253 {
254     Camera_ErrorCode ret =  OH_CaptureSession_CommitConfig(captureSession_);
255     if (ret == CAMERA_OK) {
256         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_CommitConfig success.");
257     } else {
258         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed, ret = %{public}d.", ret);
259     }
260     return ret;
261 }
262 
SessionStart(void)263 Camera_ErrorCode NDKCamera::SessionStart(void)
264 {
265     Camera_ErrorCode ret =  OH_CaptureSession_Start(captureSession_);
266     if (ret == CAMERA_OK) {
267         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Start success.");
268     } else {
269         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed, ret = %{public}d.", ret);
270     }
271     return ret;
272 }
273 
SessionStop(void)274 Camera_ErrorCode NDKCamera::SessionStop(void)
275 {
276     Camera_ErrorCode ret =  OH_CaptureSession_Stop(captureSession_);
277     if (ret == CAMERA_OK) {
278         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success.");
279     } else {
280         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed, ret = %{public}d.", ret);
281     }
282     return ret;
283 }
284 
SessionFlowFn(void)285 Camera_ErrorCode NDKCamera::SessionFlowFn(void)
286 {
287     OH_LOG_INFO(LOG_APP, "Start SessionFlowFn IN.");
288     // 开始配置会话
289     OH_LOG_INFO(LOG_APP, "Session beginConfig.");
290     Camera_ErrorCode ret =  OH_CaptureSession_BeginConfig(captureSession_);
291 
292     // 把CameraInput加入到会话
293     OH_LOG_INFO(LOG_APP, "Session addInput.");
294     ret = OH_CaptureSession_AddInput(captureSession_, cameraInput_);
295 
296     // 把previewOutput加入到会话
297     OH_LOG_INFO(LOG_APP, "Session add Preview Output.");
298     ret = OH_CaptureSession_AddPreviewOutput(captureSession_, previewOutput_);
299 
300     // 把photoOutput加入到会话
301     OH_LOG_INFO(LOG_APP, "Session add Photo Output.");
302 
303     // 提交配置信息
304     OH_LOG_INFO(LOG_APP, "Session commitConfig");
305     ret = OH_CaptureSession_CommitConfig(captureSession_);
306 
307     // 开始会话工作
308     OH_LOG_INFO(LOG_APP, "Session start");
309     ret = OH_CaptureSession_Start(captureSession_);
310     OH_LOG_INFO(LOG_APP, "Session success");
311 
312     // 启动对焦
313     OH_LOG_INFO(LOG_APP, "IsFocusMode start");
314     ret = IsFocusMode(focusMode_);
315     OH_LOG_INFO(LOG_APP, "IsFocusMode success");
316     return ret;
317 }
318 
CreateCameraInput(void)319 Camera_ErrorCode NDKCamera::CreateCameraInput(void)
320 {
321     OH_LOG_INFO(LOG_APP, "CreateCameraInput enter.");
322     Camera_ErrorCode ret = OH_CameraManager_CreateCameraInput(cameraManager_, &cameras_[cameraDeviceIndex_],
323         &cameraInput_);
324     if (cameraInput_ == nullptr || ret != CAMERA_OK) {
325         OH_LOG_ERROR(LOG_APP, "CreateCameraInput failed.");
326         return ret;
327     }
328     OH_LOG_INFO(LOG_APP, "CreateCameraInput exit.");
329     CameraInputRegisterCallback();
330     return ret;
331 }
332 
CameraInputOpen(void)333 Camera_ErrorCode NDKCamera::CameraInputOpen(void)
334 {
335     OH_LOG_INFO(LOG_APP, "CameraInputOpen enter.");
336     Camera_ErrorCode ret = OH_CameraInput_Open(cameraInput_);
337     if (ret != CAMERA_OK) {
338         OH_LOG_ERROR(LOG_APP, "CameraInput_Open failed.");
339         return ret;
340     }
341     OH_LOG_INFO(LOG_APP, "CameraInputOpen exit.");
342     return ret;
343 }
344 
CameraInputClose(void)345 Camera_ErrorCode NDKCamera::CameraInputClose(void)
346 {
347     OH_LOG_INFO(LOG_APP, "CameraInput_Close enter.");
348     Camera_ErrorCode ret = OH_CameraInput_Close(cameraInput_);
349     if (ret != CAMERA_OK) {
350         OH_LOG_ERROR(LOG_APP, "CameraInput_Close failed.");
351         return ret;
352     }
353     OH_LOG_INFO(LOG_APP, "CameraInput_Close exit.");
354     return ret;
355 }
356 
CameraInputRelease(void)357 Camera_ErrorCode NDKCamera::CameraInputRelease(void)
358 {
359     OH_LOG_INFO(LOG_APP, "CameraInputRelease enter.");
360     Camera_ErrorCode ret = OH_CameraInput_Release(cameraInput_);
361     if (ret != CAMERA_OK) {
362         OH_LOG_ERROR(LOG_APP, "CameraInput_Release failed.");
363         return ret;
364     }
365     OH_LOG_INFO(LOG_APP, "CameraInputRelease exit.");
366     return ret;
367 }
368 
GetSupportedCameras(void)369 Camera_ErrorCode NDKCamera::GetSupportedCameras(void)
370 {
371     Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameras(cameraManager_, &cameras_, &size_);
372     if (cameras_ == nullptr || &size_ == nullptr || ret != CAMERA_OK) {
373         OH_LOG_ERROR(LOG_APP, "Get supported cameras failed.");
374         return CAMERA_INVALID_ARGUMENT;
375     }
376     return ret;
377 }
378 
GetSupportedOutputCapability(void)379 Camera_ErrorCode NDKCamera::GetSupportedOutputCapability(void)
380 {
381     Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager_,
382         &cameras_[cameraDeviceIndex_], &cameraOutputCapability_);
383     if (cameraOutputCapability_ == nullptr || ret != CAMERA_OK) {
384         OH_LOG_ERROR(LOG_APP, "GetSupportedCameraOutputCapability failed.");
385         return CAMERA_INVALID_ARGUMENT;
386     }
387     return ret;
388 }
389 
CreatePreviewOutput(void)390 Camera_ErrorCode NDKCamera::CreatePreviewOutput(void)
391 {
392     for (int index = 0; index < cameraOutputCapability_->previewProfilesSize; index ++) {
393         if (cameraOutputCapability_->previewProfiles[index]->size.height == height_ &&
394             cameraOutputCapability_->previewProfiles[index]->size.width == width_) {
395             profile_ = cameraOutputCapability_->previewProfiles[index];
396             break;
397             }
398     }
399     if (profile_ == nullptr) {
400         OH_LOG_ERROR(LOG_APP, "Get previewProfiles failed.");
401         return CAMERA_INVALID_ARGUMENT;
402     }
403     Camera_ErrorCode ret = OH_CameraManager_CreatePreviewOutput(cameraManager_, profile_, previewSurfaceId_,
404         &previewOutput_);
405     if (previewSurfaceId_ == nullptr || previewOutput_ == nullptr || ret != CAMERA_OK) {
406         OH_LOG_ERROR(LOG_APP, "CreatePreviewOutput failed.");
407         return CAMERA_INVALID_ARGUMENT;
408     }
409     PreviewOutputRegisterCallback();
410     return ret;
411 }
412 
CreatePhotoOutput(char * photoSurfaceId)413 Camera_ErrorCode NDKCamera::CreatePhotoOutput(char* photoSurfaceId)
414 {
415     for (int index = 0; index < cameraOutputCapability_->photoProfilesSize; index++) {
416         if (cameraOutputCapability_->photoProfiles[index]->size.height == height_ &&
417             cameraOutputCapability_->photoProfiles[index]->size.width == width_) {
418             profile_ = cameraOutputCapability_->photoProfiles[index];
419             break;
420         }
421     }
422     if (profile_ == nullptr) {
423         OH_LOG_ERROR(LOG_APP, "Get photoProfiles failed.");
424         return CAMERA_INVALID_ARGUMENT;
425     }
426 
427     if (photoSurfaceId == nullptr) {
428         OH_LOG_ERROR(LOG_APP, "CreatePhotoOutput failed.");
429         return CAMERA_INVALID_ARGUMENT;
430     }
431 
432     Camera_ErrorCode ret = OH_CameraManager_CreatePhotoOutput(cameraManager_, profile_, photoSurfaceId, &photoOutput_);
433     PhotoOutputRegisterCallback();
434     return ret;
435 }
436 
CreateVideoOutput(char * videoId)437 Camera_ErrorCode NDKCamera::CreateVideoOutput(char* videoId)
438 {
439     for (size_t index = 0; index < cameraOutputCapability_->videoProfilesSize; index++) {
440         if (cameraOutputCapability_->videoProfiles[index]->size.height == height_ &&
441             cameraOutputCapability_->videoProfiles[index]->size.width == width_) {
442             videoProfile_ = cameraOutputCapability_->videoProfiles[index];
443             break;
444         }
445     }
446     if (videoProfile_ == nullptr) {
447         OH_LOG_ERROR(LOG_APP, "Get videoProfiles failed.");
448         return CAMERA_INVALID_ARGUMENT;
449     }
450     Camera_ErrorCode ret = OH_CameraManager_CreateVideoOutput(cameraManager_, videoProfile_, videoId, &videoOutput_);
451     if (videoId == nullptr || videoOutput_ == nullptr || ret != CAMERA_OK) {
452         OH_LOG_ERROR(LOG_APP, "CreateVideoOutput failed.");
453         return ret;
454     }
455 
456     return CAMERA_OK;
457 }
458 
AddVideoOutput(void)459 Camera_ErrorCode NDKCamera::AddVideoOutput(void)
460 {
461     Camera_ErrorCode ret = OH_CaptureSession_AddVideoOutput(captureSession_, videoOutput_);
462     if (ret == CAMERA_OK) {
463         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput success.");
464     } else {
465         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed, ret = %{public}d.", ret);
466     }
467     return ret;
468 }
469 
AddPhotoOutput()470 Camera_ErrorCode NDKCamera::AddPhotoOutput()
471 {
472     Camera_ErrorCode ret = OH_CaptureSession_AddPhotoOutput(captureSession_, photoOutput_);
473     if (ret == CAMERA_OK) {
474         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput success.");
475     } else {
476         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed, ret = %{public}d.", ret);
477     }
478     return ret;
479 }
480 
CreateMetadataOutput(void)481 Camera_ErrorCode NDKCamera::CreateMetadataOutput(void)
482 {
483     metaDataObjectType_ = cameraOutputCapability_->supportedMetadataObjectTypes[0];
484     if (metaDataObjectType_ == nullptr) {
485         OH_LOG_ERROR(LOG_APP, "Get metaDataObjectType failed.");
486         return CAMERA_INVALID_ARGUMENT;
487     }
488     Camera_ErrorCode ret = OH_CameraManager_CreateMetadataOutput(cameraManager_, metaDataObjectType_,
489         &metadataOutput_);
490     if (metadataOutput_ == nullptr || ret != CAMERA_OK) {
491         OH_LOG_ERROR(LOG_APP, "CreateMetadataOutput failed.");
492         return ret;
493     }
494     MetadataOutputRegisterCallback();
495     return CAMERA_OK;
496 }
497 
IsCameraMuted(void)498 Camera_ErrorCode NDKCamera::IsCameraMuted(void)
499 {
500     Camera_ErrorCode ret = OH_CameraManager_IsCameraMuted(cameraManager_, isCameraMuted_);
501     if (isCameraMuted_ == nullptr || ret != CAMERA_OK) {
502         OH_LOG_ERROR(LOG_APP, "IsCameraMuted failed.");
503     }
504     return ret;
505 }
506 
PreviewOutputStop(void)507 Camera_ErrorCode NDKCamera::PreviewOutputStop(void)
508 {
509     OH_LOG_INFO(LOG_APP, "PreviewOutputStop enter.");
510     Camera_ErrorCode ret = OH_PreviewOutput_Stop(previewOutput_);
511     if (ret != CAMERA_OK) {
512         OH_LOG_ERROR(LOG_APP, "PreviewOutputStop failed.");
513     }
514     return ret;
515 }
516 
PreviewOutputRelease(void)517 Camera_ErrorCode NDKCamera::PreviewOutputRelease(void)
518 {
519     OH_LOG_INFO(LOG_APP, "PreviewOutputRelease enter.");
520     Camera_ErrorCode ret = OH_PreviewOutput_Release(previewOutput_);
521     if (ret != CAMERA_OK) {
522         OH_LOG_ERROR(LOG_APP, "PreviewOutputRelease failed.");
523     }
524     return ret;
525 }
526 
PhotoOutputRelease(void)527 Camera_ErrorCode NDKCamera::PhotoOutputRelease(void)
528 {
529     OH_LOG_INFO(LOG_APP, "PhotoOutputRelease enter.");
530     Camera_ErrorCode ret = OH_PhotoOutput_Release(photoOutput_);
531     if (ret != CAMERA_OK) {
532         OH_LOG_ERROR(LOG_APP, "PhotoOutputRelease failed.");
533     }
534     return ret;
535 }
StartVideo(char * videoId,char * photoId)536 Camera_ErrorCode NDKCamera::StartVideo(char* videoId, char* photoId)
537 {
538     OH_LOG_INFO(LOG_APP, "StartVideo begin.");
539     Camera_ErrorCode ret = SessionStop();
540     if (ret == CAMERA_OK) {
541         OH_LOG_INFO(LOG_APP, "Session stop success.");
542     } else {
543         OH_LOG_ERROR(LOG_APP, "Session stop failed, ret = %{public}d.", ret);
544     }
545     ret = SessionBegin();
546     if (ret == CAMERA_OK) {
547         OH_LOG_INFO(LOG_APP, "Session begin success.");
548     } else {
549         OH_LOG_ERROR(LOG_APP, "Session begin failed, ret = %{public}d.", ret);
550     }
551     (void)OH_CaptureSession_RemovePhotoOutput(captureSession_, photoOutput_);
552     (void)CreatePhotoOutput(photoId);
553     (void)AddPhotoOutput();
554     (void)CreateVideoOutput(videoId);
555     (void)AddVideoOutput();
556     (void)SessionCommitConfig();
557     (void)SessionStart();
558     (void)VideoOutputRegisterCallback();
559     return ret;
560 }
561 
VideoOutputStart(void)562 Camera_ErrorCode NDKCamera::VideoOutputStart(void)
563 {
564     OH_LOG_INFO(LOG_APP, "VideoOutputStart enter.");
565     Camera_ErrorCode ret = OH_VideoOutput_Start(videoOutput_);
566     if (ret == CAMERA_OK) {
567         OH_LOG_INFO(LOG_APP, "OH_VideoOutput_Start success.");
568     } else {
569         OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed, ret = %{public}d.", ret);
570     }
571     return ret;
572 }
573 
StartPhoto(char * mSurfaceId)574 Camera_ErrorCode NDKCamera::StartPhoto(char* mSurfaceId)
575 {
576     Camera_ErrorCode ret = CAMERA_OK;
577     if (takePictureTimes == 0) {
578         ret = SessionStop();
579         OH_LOG_INFO(LOG_APP, "Start photo, SessionStop, ret = %{public}d.", ret);
580         ret = SessionBegin();
581         OH_LOG_INFO(LOG_APP, "Start photo, SessionBegin, ret = %{public}d.", ret);
582 
583         OH_LOG_DEBUG(LOG_APP, "Start photo begin.");
584         ret = CreatePhotoOutput(mSurfaceId);
585         OH_LOG_INFO(LOG_APP, "Start photo, CreatePhotoOutput ret = %{public}d.", ret);
586         ret = OH_CaptureSession_AddPhotoOutput(captureSession_, photoOutput_);
587         OH_LOG_INFO(LOG_APP, "Start photo, AddPhotoOutput ret = %{public}d.", ret);
588         ret = SessionCommitConfig();
589 
590         OH_LOG_INFO(LOG_APP, "Start photo, SessionCommitConfig ret = %{public}d.", ret);
591         ret = SessionStart();
592         OH_LOG_INFO(LOG_APP, "Start photo, SessionStart ret = %{public}d.", ret);
593     }
594     ret = TakePicture();
595     OH_LOG_INFO(LOG_APP, "Start photo, TakePicture ret = %{public}d.", ret);
596     if (ret != CAMERA_OK) {
597         OH_LOG_ERROR(LOG_APP, "StartPhoto failed.");
598         return ret;
599     }
600     takePictureTimes++;
601     return ret;
602 }
603 
604 // exposure mode
IsExposureModeSupportedFn(uint32_t mode)605 Camera_ErrorCode NDKCamera::IsExposureModeSupportedFn(uint32_t mode)
606 {
607     OH_LOG_DEBUG(LOG_APP, "IsExposureModeSupportedFn enter.");
608     exposureMode_ = static_cast<Camera_ExposureMode>(mode);
609     Camera_ErrorCode ret = OH_CaptureSession_IsExposureModeSupported(captureSession_, exposureMode_,
610         &isExposureModeSupported_);
611     if (&isExposureModeSupported_ == nullptr || ret != CAMERA_OK) {
612         OH_LOG_ERROR(LOG_APP, "IsExposureModeSupported failed.");
613         return ret;
614     }
615     ret = OH_CaptureSession_SetExposureMode(captureSession_, exposureMode_);
616     if (ret != CAMERA_OK) {
617         OH_LOG_ERROR(LOG_APP, "SetExposureMode failed.");
618         return ret;
619     }
620     OH_LOG_INFO(LOG_APP, "SetExposureMode succeed.");
621     ret = OH_CaptureSession_GetExposureMode(captureSession_, &exposureMode_);
622     if (&exposureMode_ == nullptr || ret != CAMERA_OK) {
623         OH_LOG_ERROR(LOG_APP, "GetExposureMode failed.");
624         return ret;
625     }
626     OH_LOG_DEBUG(LOG_APP, "IsExposureModeSupportedFn end.");
627     return ret;
628 }
629 
IsMeteringPoint(int x,int y)630 Camera_ErrorCode NDKCamera::IsMeteringPoint(int x, int y)
631 {
632     OH_LOG_DEBUG(LOG_APP, "IsMeteringPoint enter.");
633     Camera_ErrorCode ret = OH_CaptureSession_GetExposureMode(captureSession_, &exposureMode_);
634     if (&exposureMode_ == nullptr || ret != CAMERA_OK) {
635         OH_LOG_ERROR(LOG_APP, "GetExposureMode failed.");
636         return ret;
637     }
638     Camera_Point exposurePoint;
639     exposurePoint.x = x;
640     exposurePoint.y = y;
641     ret = OH_CaptureSession_SetMeteringPoint(captureSession_, exposurePoint);
642     if (ret != CAMERA_OK) {
643         OH_LOG_ERROR(LOG_APP, "SetMeteringPoint failed.");
644         return ret;
645     }
646     OH_LOG_INFO(LOG_APP, "SetMeteringPoint succeed.");
647     ret = OH_CaptureSession_GetMeteringPoint(captureSession_, &exposurePoint);
648     if (ret != CAMERA_OK) {
649         OH_LOG_ERROR(LOG_APP, "GetMeteringPoint failed.");
650         return ret;
651     }
652     OH_LOG_DEBUG(LOG_APP, "IsMeteringPoint end.");
653     return ret;
654 }
655 
IsExposureBiasRange(int exposureBias)656 Camera_ErrorCode NDKCamera::IsExposureBiasRange(int exposureBias)
657 {
658     OH_LOG_DEBUG(LOG_APP, "IsExposureBiasRange enter.");
659     float exposureBiasValue = (float)exposureBias;
660     Camera_ErrorCode ret = OH_CaptureSession_GetExposureBiasRange(captureSession_, &minExposureBias_,
661         &maxExposureBias_, &step_);
662     if (&minExposureBias_ == nullptr || &maxExposureBias_ == nullptr || &step_ == nullptr || ret != CAMERA_OK) {
663         OH_LOG_ERROR(LOG_APP, "GetExposureBiasRange failed.");
664         return ret;
665     }
666     ret = OH_CaptureSession_SetExposureBias(captureSession_, exposureBiasValue);
667     if (ret != CAMERA_OK) {
668         OH_LOG_ERROR(LOG_APP, "SetExposureBias failed.");
669         return ret;
670     }
671     OH_LOG_INFO(LOG_APP, "SetExposureBias succeed.");
672     ret = OH_CaptureSession_GetExposureBias(captureSession_, &exposureBiasValue);
673     if (&exposureBiasValue == nullptr || ret != CAMERA_OK) {
674         OH_LOG_ERROR(LOG_APP, "GetExposureBias failed.");
675         return ret;
676     }
677     OH_LOG_DEBUG(LOG_APP, "IsExposureBiasRange end.");
678     return ret;
679 }
680 
681 // focus mode
IsFocusModeSupported(uint32_t mode)682 Camera_ErrorCode NDKCamera::IsFocusModeSupported(uint32_t mode)
683 {
684     Camera_FocusMode focusMode = static_cast<Camera_FocusMode>(mode);
685     Camera_ErrorCode ret = OH_CaptureSession_IsFocusModeSupported(captureSession_, focusMode, &isFocusModeSupported_);
686     if (&isFocusModeSupported_ == nullptr || ret != CAMERA_OK) {
687         OH_LOG_ERROR(LOG_APP, "IsFocusModeSupported failed.");
688     }
689     return ret;
690 }
691 
IsFocusMode(uint32_t mode)692 Camera_ErrorCode NDKCamera::IsFocusMode(uint32_t mode)
693 {
694     OH_LOG_DEBUG(LOG_APP, "IsFocusMode enter.");
695     Camera_FocusMode focusMode = static_cast<Camera_FocusMode>(mode);
696     Camera_ErrorCode ret = OH_CaptureSession_IsFocusModeSupported(captureSession_, focusMode, &isFocusModeSupported_);
697     if (&isFocusModeSupported_ == nullptr || ret != CAMERA_OK) {
698         OH_LOG_ERROR(LOG_APP, "IsFocusModeSupported failed.");
699         return ret;
700     }
701     ret = OH_CaptureSession_SetFocusMode(captureSession_, focusMode);
702     if (ret != CAMERA_OK) {
703         OH_LOG_ERROR(LOG_APP, "SetFocusMode failed.");
704         return ret;
705     }
706     OH_LOG_INFO(LOG_APP, "SetFocusMode succeed.");
707     ret = OH_CaptureSession_GetFocusMode(captureSession_, &focusMode);
708     if (&focusMode == nullptr || ret != CAMERA_OK) {
709         OH_LOG_ERROR(LOG_APP, "GetFocusMode failed.");
710         return ret;
711     }
712     OH_LOG_DEBUG(LOG_APP, "IsFocusMode end.");
713     return ret;
714 }
715 
IsFocusPoint(float x,float y)716 Camera_ErrorCode NDKCamera::IsFocusPoint(float x, float y)
717 {
718     OH_LOG_DEBUG(LOG_APP, "IsFocusPoint enter.");
719     Camera_Point focusPoint;
720     focusPoint.x = x;
721     focusPoint.y = y;
722     Camera_ErrorCode ret = OH_CaptureSession_SetFocusPoint(captureSession_, focusPoint);
723     if (ret != CAMERA_OK) {
724         OH_LOG_ERROR(LOG_APP, "SetFocusPoint failed.");
725         return ret;
726     }
727     OH_LOG_INFO(LOG_APP, "SetFocusPoint succeed.");
728     ret = OH_CaptureSession_GetFocusPoint(captureSession_, &focusPoint);
729     if (&focusPoint == nullptr || ret != CAMERA_OK) {
730         OH_LOG_ERROR(LOG_APP, "GetFocusPoint failed.");
731         return ret;
732     }
733     OH_LOG_DEBUG(LOG_APP, "IsFocusPoint end.");
734     return ret;
735 }
GetVideoFrameWidth(void)736 int32_t NDKCamera::GetVideoFrameWidth(void)
737 {
738     videoProfile_ = cameraOutputCapability_->videoProfiles[0];
739     if (videoProfile_ == nullptr) {
740         OH_LOG_ERROR(LOG_APP, "Get videoProfiles failed.");
741         return CAMERA_INVALID_ARGUMENT;
742     }
743     return videoProfile_->size.width;
744 }
745 
GetVideoFrameHeight(void)746 int32_t NDKCamera::GetVideoFrameHeight(void)
747 {
748     videoProfile_ = cameraOutputCapability_->videoProfiles[0];
749     if (videoProfile_ == nullptr) {
750         OH_LOG_ERROR(LOG_APP, "Get videoProfiles failed.");
751         return CAMERA_INVALID_ARGUMENT;
752     }
753     return videoProfile_->size.height;
754 }
755 
GetVideoFrameRate(void)756 int32_t NDKCamera::GetVideoFrameRate(void)
757 {
758     videoProfile_ = cameraOutputCapability_->videoProfiles[0];
759     if (videoProfile_ == nullptr) {
760         OH_LOG_ERROR(LOG_APP, "Get videoProfiles failed.");
761         return CAMERA_INVALID_ARGUMENT;
762     }
763     return videoProfile_->range.min;
764 }
765 
VideoOutputStop(void)766 Camera_ErrorCode NDKCamera::VideoOutputStop(void)
767 {
768     OH_LOG_INFO(LOG_APP, "enter VideoOutputStop.");
769     Camera_ErrorCode ret = OH_VideoOutput_Stop(videoOutput_);
770     if (ret != CAMERA_OK) {
771         OH_LOG_ERROR(LOG_APP, "VideoOutputStop failed.");
772     }
773     return ret;
774 }
775 
VideoOutputRelease(void)776 Camera_ErrorCode NDKCamera::VideoOutputRelease(void)
777 {
778     OH_LOG_INFO(LOG_APP, "enter VideoOutputRelease.");
779     Camera_ErrorCode ret = OH_VideoOutput_Release(videoOutput_);
780     if (ret != CAMERA_OK) {
781         OH_LOG_ERROR(LOG_APP, "VideoOutputRelease failed.");
782     }
783     return ret;
784 }
785 
TakePicture(void)786 Camera_ErrorCode NDKCamera::TakePicture(void)
787 {
788     Camera_ErrorCode ret = OH_PhotoOutput_Capture(photoOutput_);
789     if (ret != CAMERA_OK) {
790         OH_LOG_ERROR(LOG_APP, "takePicture OH_PhotoOutput_Capture ret = %{public}d.", ret);
791     }
792     return ret;
793 }
794 
TakePictureWithPhotoSettings(Camera_PhotoCaptureSetting photoSetting)795 Camera_ErrorCode NDKCamera::TakePictureWithPhotoSettings(Camera_PhotoCaptureSetting photoSetting)
796 {
797     Camera_ErrorCode ret = OH_PhotoOutput_Capture_WithCaptureSetting(photoOutput_, photoSetting);
798     if (ret != CAMERA_OK) {
799         OH_LOG_ERROR(LOG_APP, "takePicture TakePictureWithPhotoSettings ret = %{public}d.", ret);
800         return CAMERA_INVALID_ARGUMENT;
801     } else {
802         OH_LOG_INFO(LOG_APP, "TakePictureWithPhotoSettings get quality %{public}d, rotation %{public}d, "
803             "mirror %{public}d, latitude, %{public}d, longitude %{public}d, altitude %{public}d",
804             photoSetting.quality, photoSetting.rotation, photoSetting.mirror, photoSetting.location->latitude,
805             photoSetting.location->longitude, photoSetting.location->altitude);
806     }
807     return CAMERA_OK;
808 }
809 
810 // CameraManager Callback
CameraManagerStatusCallback(Camera_Manager * cameraManager,Camera_StatusInfo * status)811 void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
812 {
813     OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback");
814 }
815 
GetCameraManagerListener(void)816 CameraManager_Callbacks* NDKCamera::GetCameraManagerListener(void)
817 {
818     static CameraManager_Callbacks cameraManagerListener = {
819         .onCameraStatus = CameraManagerStatusCallback
820     };
821     return &cameraManagerListener;
822 }
823 
CameraManagerRegisterCallback(void)824 Camera_ErrorCode NDKCamera::CameraManagerRegisterCallback(void)
825 {
826     Camera_ErrorCode ret = OH_CameraManager_RegisterCallback(cameraManager_, GetCameraManagerListener());
827     if (ret != CAMERA_OK) {
828         OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
829     }
830     return ret;
831 }
832 
833 // CameraInput Callback
OnCameraInputError(const Camera_Input * cameraInput,Camera_ErrorCode errorCode)834 void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
835 {
836     OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode);
837 }
838 
GetCameraInputListener(void)839 CameraInput_Callbacks* NDKCamera::GetCameraInputListener(void)
840 {
841     static CameraInput_Callbacks cameraInputCallbacks = {
842         .onError = OnCameraInputError
843     };
844     return &cameraInputCallbacks;
845 }
846 
CameraInputRegisterCallback(void)847 Camera_ErrorCode NDKCamera::CameraInputRegisterCallback(void)
848 {
849     Camera_ErrorCode ret = OH_CameraInput_RegisterCallback(cameraInput_, GetCameraInputListener());
850     if (ret != CAMERA_OK) {
851         OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
852     }
853     return ret;
854 }
855 
856 // PreviewOutput Callback
PreviewOutputOnFrameStart(Camera_PreviewOutput * previewOutput)857 void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput)
858 {
859     OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart");
860 }
861 
PreviewOutputOnFrameEnd(Camera_PreviewOutput * previewOutput,int32_t frameCount)862 void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount)
863 {
864     OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount);
865 }
866 
PreviewOutputOnError(Camera_PreviewOutput * previewOutput,Camera_ErrorCode errorCode)867 void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode)
868 {
869     OH_LOG_INFO(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode);
870 }
871 
GetPreviewOutputListener(void)872 PreviewOutput_Callbacks* NDKCamera::GetPreviewOutputListener(void)
873 {
874     static PreviewOutput_Callbacks previewOutputListener = {
875         .onFrameStart = PreviewOutputOnFrameStart,
876         .onFrameEnd = PreviewOutputOnFrameEnd,
877         .onError = PreviewOutputOnError
878     };
879     return &previewOutputListener;
880 }
881 
PreviewOutputRegisterCallback(void)882 Camera_ErrorCode NDKCamera::PreviewOutputRegisterCallback(void)
883 {
884     Camera_ErrorCode ret = OH_PreviewOutput_RegisterCallback(previewOutput_, GetPreviewOutputListener());
885     if (ret != CAMERA_OK) {
886         OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
887     }
888     return ret;
889 }
890 
891 // PhotoOutput Callback
PhotoOutputOnFrameStart(Camera_PhotoOutput * photoOutput)892 void PhotoOutputOnFrameStart(Camera_PhotoOutput* photoOutput)
893 {
894     OH_LOG_INFO(LOG_APP, "PhotoOutputOnFrameStart");
895 }
896 
PhotoOutputOnFrameShutter(Camera_PhotoOutput * photoOutput,Camera_FrameShutterInfo * info)897 void PhotoOutputOnFrameShutter(Camera_PhotoOutput* photoOutput, Camera_FrameShutterInfo* info)
898 {
899     OH_LOG_INFO(LOG_APP, "PhotoOutputOnFrameShutter");
900 }
901 
PhotoOutputOnFrameEnd(Camera_PhotoOutput * photoOutput,int32_t frameCount)902 void PhotoOutputOnFrameEnd(Camera_PhotoOutput* photoOutput, int32_t frameCount)
903 {
904     OH_LOG_INFO(LOG_APP, "PhotoOutput frameCount = %{public}d", frameCount);
905 }
906 
PhotoOutputOnError(Camera_PhotoOutput * photoOutput,Camera_ErrorCode errorCode)907 void PhotoOutputOnError(Camera_PhotoOutput* photoOutput, Camera_ErrorCode errorCode)
908 {
909     OH_LOG_INFO(LOG_APP, "PhotoOutput errorCode = %{public}d", errorCode);
910 }
911 
GetPhotoOutputListener(void)912 PhotoOutput_Callbacks* NDKCamera::GetPhotoOutputListener(void)
913 {
914     static PhotoOutput_Callbacks photoOutputListener = {
915         .onFrameStart = PhotoOutputOnFrameStart,
916         .onFrameShutter = PhotoOutputOnFrameShutter,
917         .onFrameEnd = PhotoOutputOnFrameEnd,
918         .onError = PhotoOutputOnError
919     };
920     return &photoOutputListener;
921 }
922 
PhotoOutputRegisterCallback(void)923 Camera_ErrorCode NDKCamera::PhotoOutputRegisterCallback(void)
924 {
925     Camera_ErrorCode ret = OH_PhotoOutput_RegisterCallback(photoOutput_, GetPhotoOutputListener());
926     if (ret != CAMERA_OK) {
927         OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_RegisterCallback failed.");
928     }
929     return ret;
930 }
931 
932 // VideoOutput Callback
VideoOutputOnFrameStart(Camera_VideoOutput * videoOutput)933 void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
934 {
935     OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
936 }
937 
VideoOutputOnFrameEnd(Camera_VideoOutput * videoOutput,int32_t frameCount)938 void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
939 {
940     OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
941 }
942 
VideoOutputOnError(Camera_VideoOutput * videoOutput,Camera_ErrorCode errorCode)943 void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
944 {
945     OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
946 }
947 
GetVideoOutputListener(void)948 VideoOutput_Callbacks* NDKCamera::GetVideoOutputListener(void)
949 {
950     static VideoOutput_Callbacks videoOutputListener = {
951         .onFrameStart = VideoOutputOnFrameStart,
952         .onFrameEnd = VideoOutputOnFrameEnd,
953         .onError = VideoOutputOnError
954     };
955     return &videoOutputListener;
956 }
957 
VideoOutputRegisterCallback(void)958 Camera_ErrorCode NDKCamera::VideoOutputRegisterCallback(void)
959 {
960     Camera_ErrorCode ret = OH_VideoOutput_RegisterCallback(videoOutput_, GetVideoOutputListener());
961     if (ret != CAMERA_OK) {
962         OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
963     }
964     return ret;
965 }
966 
967 // Metadata Callback
OnMetadataObjectAvailable(Camera_MetadataOutput * metadataOutput,Camera_MetadataObject * metadataObject,uint32_t size)968 void OnMetadataObjectAvailable(Camera_MetadataOutput* metadataOutput,
969     Camera_MetadataObject* metadataObject, uint32_t size)
970 {
971     OH_LOG_INFO(LOG_APP, "size = %{public}d", size);
972 }
973 
OnMetadataOutputError(Camera_MetadataOutput * metadataOutput,Camera_ErrorCode errorCode)974 void OnMetadataOutputError(Camera_MetadataOutput* metadataOutput, Camera_ErrorCode errorCode)
975 {
976     OH_LOG_INFO(LOG_APP, "OnMetadataOutput errorCode = %{public}d", errorCode);
977 }
978 
GetMetadataOutputListener(void)979 MetadataOutput_Callbacks* NDKCamera::GetMetadataOutputListener(void)
980 {
981     static MetadataOutput_Callbacks metadataOutputListener = {
982         .onMetadataObjectAvailable = OnMetadataObjectAvailable,
983         .onError = OnMetadataOutputError
984     };
985     return &metadataOutputListener;
986 }
987 
MetadataOutputRegisterCallback(void)988 Camera_ErrorCode NDKCamera::MetadataOutputRegisterCallback(void)
989 {
990     Camera_ErrorCode ret = OH_MetadataOutput_RegisterCallback(metadataOutput_, GetMetadataOutputListener());
991     if (ret != CAMERA_OK) {
992         OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_RegisterCallback failed.");
993     }
994     return ret;
995 }
996 
997 // Session Callback
CaptureSessionOnFocusStateChange(Camera_CaptureSession * Session,Camera_FocusState focusState)998 void CaptureSessionOnFocusStateChange(Camera_CaptureSession* Session, Camera_FocusState focusState)
999 {
1000     OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange");
1001 }
1002 
CaptureSessionOnError(Camera_CaptureSession * Session,Camera_ErrorCode errorCode)1003 void CaptureSessionOnError(Camera_CaptureSession* Session, Camera_ErrorCode errorCode)
1004 {
1005     OH_LOG_INFO(LOG_APP, "CaptureSession errorCode = %{public}d", errorCode);
1006 }
1007 
GetCaptureSessionRegister(void)1008 CaptureSession_Callbacks* NDKCamera::GetCaptureSessionRegister(void)
1009 {
1010     static CaptureSession_Callbacks captureSessionCallbacks = {
1011         .onFocusStateChange = CaptureSessionOnFocusStateChange,
1012         .onError = CaptureSessionOnError
1013     };
1014     return &captureSessionCallbacks;
1015 }
1016 
CaptureSessionRegisterCallback(void)1017 Camera_ErrorCode NDKCamera::CaptureSessionRegisterCallback(void)
1018 {
1019     Camera_ErrorCode ret = OH_CaptureSession_RegisterCallback(captureSession_, GetCaptureSessionRegister());
1020     if (ret != CAMERA_OK) {
1021         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
1022     }
1023     return ret;
1024 }
1025 } // OHOS_NDK_CAMERA