1 /*
2 * Copyright (c) 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 <js_native_api.h>
17 #include "camera_manager.h"
18
19 #define LOG_TAG "DEMO:"
20 #define LOG_DOMAIN 0x3200
21
22 using namespace OHOS_CAMERA_SAMPLE;
23 static NDKCamera* ndkCamera_ = nullptr;
24 const int32_t ARGS_TWO = 2;
25 struct Capture_Setting {
26 int32_t quality;
27 int32_t rotation;
28 int32_t location;
29 bool mirror;
30 int32_t latitude;
31 int32_t longitude;
32 int32_t altitude;
33 };
34
SetZoomRatio(napi_env env,napi_callback_info info)35 static napi_value SetZoomRatio(napi_env env, napi_callback_info info)
36 {
37 size_t requireArgc = 2;
38 size_t argc = 2;
39 napi_value args[2] = {nullptr};
40 napi_value result;
41
42 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
43
44 napi_valuetype valuetype0;
45 napi_typeof(env, args[0], &valuetype0);
46
47 int32_t zoomRatio;
48 napi_get_value_int32(env, args[0], &zoomRatio);
49
50 OH_LOG_ERROR(LOG_APP, "SetZoomRatio : %{public}d", zoomRatio);
51
52 ndkCamera_->setZoomRatioFn(zoomRatio);
53 napi_create_int32(env, argc, &result);
54 return result;
55 }
56
HasFlash(napi_env env,napi_callback_info info)57 static napi_value HasFlash(napi_env env, napi_callback_info info)
58 {
59 OH_LOG_ERROR(LOG_APP, "HasFlash");
60 size_t requireArgc = 2;
61 size_t argc = 2;
62 napi_value args[2] = {nullptr};
63 napi_value result;
64
65 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
66
67 napi_valuetype valuetype0;
68 napi_typeof(env, args[0], &valuetype0);
69
70 int32_t flashMode;
71 napi_get_value_int32(env, args[0], &flashMode);
72
73 OH_LOG_ERROR(LOG_APP, "HasFlash flashMode : %{public}d", flashMode);
74
75 ndkCamera_->HasFlashFn(flashMode);
76 napi_create_int32(env, argc, &result);
77 return result;
78 }
79
IsVideoStabilizationModeSupported(napi_env env,napi_callback_info info)80 static napi_value IsVideoStabilizationModeSupported(napi_env env, napi_callback_info info)
81 {
82 OH_LOG_ERROR(LOG_APP, "IsVideoStabilizationModeSupportedFn");
83 size_t requireArgc = 2;
84 size_t argc = 2;
85 napi_value args[2] = {nullptr};
86 napi_value result;
87
88 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
89
90 napi_valuetype valuetype0;
91 napi_typeof(env, args[0], &valuetype0);
92
93 int32_t videoMode;
94 napi_get_value_int32(env, args[0], &videoMode);
95
96 OH_LOG_ERROR(LOG_APP, "IsVideoStabilizationModeSupportedFn videoMode : %{public}d", videoMode);
97
98 ndkCamera_->IsVideoStabilizationModeSupportedFn(videoMode);
99 napi_create_int32(env, argc, &result);
100 return result;
101 }
102
InitCamera(napi_env env,napi_callback_info info)103 static napi_value InitCamera(napi_env env, napi_callback_info info)
104 {
105 OH_LOG_ERROR(LOG_APP, "InitCamera Start");
106 size_t requireArgc = 3;
107 size_t argc = 3;
108 napi_value args[3] = {nullptr};
109 napi_value result;
110 size_t typeLen = 0;
111 char* surfaceId = nullptr;
112
113 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
114
115 napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen);
116 surfaceId = new char[typeLen + 1];
117 napi_get_value_string_utf8(env, args[0], surfaceId, typeLen + 1, &typeLen);
118
119 napi_valuetype valuetype1;
120 napi_typeof(env, args[1], &valuetype1);
121
122 int32_t focusMode;
123 napi_get_value_int32(env, args[1], &focusMode);
124
125 uint32_t cameraDeviceIndex;
126 napi_get_value_uint32(env, args[ARGS_TWO], &cameraDeviceIndex);
127
128 OH_LOG_ERROR(LOG_APP, "InitCamera focusMode : %{public}d", focusMode);
129 OH_LOG_ERROR(LOG_APP, "InitCamera surfaceId : %{public}s", surfaceId);
130 OH_LOG_ERROR(LOG_APP, "InitCamera cameraDeviceIndex : %{public}d", cameraDeviceIndex);
131
132 if (ndkCamera_) {
133 OH_LOG_ERROR(LOG_APP, "ndkCamera_ is not null");
134 delete ndkCamera_;
135 ndkCamera_ = nullptr;
136 }
137 ndkCamera_ = new NDKCamera(surfaceId, focusMode, cameraDeviceIndex);
138 OH_LOG_ERROR(LOG_APP, "InitCamera End");
139 napi_create_int32(env, argc, &result);
140 return result;
141 }
142
ReleaseCamera(napi_env env,napi_callback_info info)143 static napi_value ReleaseCamera(napi_env env, napi_callback_info info)
144 {
145 OH_LOG_ERROR(LOG_APP, "ReleaseCamera Start");
146 size_t requireArgc = 2;
147 size_t argc = 2;
148 napi_value args[2] = {nullptr};
149 napi_value result;
150 size_t typeLen = 0;
151 char* surfaceId = nullptr;
152
153 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
154
155 ndkCamera_->ReleaseCamera();
156 if (ndkCamera_) {
157 OH_LOG_ERROR(LOG_APP, "ndkCamera_ is not null");
158 delete ndkCamera_;
159 ndkCamera_ = nullptr;
160 }
161 OH_LOG_ERROR(LOG_APP, "ReleaseCamera End");
162 napi_create_int32(env, argc, &result);
163 return result;
164 }
ReleaseSession(napi_env env,napi_callback_info info)165 static napi_value ReleaseSession(napi_env env, napi_callback_info info)
166 {
167 OH_LOG_ERROR(LOG_APP, "ReleaseCamera Start");
168 size_t requireArgc = 2;
169 size_t argc = 2;
170 napi_value args[2] = {nullptr};
171 napi_value result;
172 size_t typeLen = 0;
173 char* surfaceId = nullptr;
174
175 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
176
177 ndkCamera_->ReleaseSession();
178
179 OH_LOG_ERROR(LOG_APP, "ReleaseCamera End");
180 napi_create_int32(env, argc, &result);
181 return result;
182 }
StartPhotoOrVideo(napi_env env,napi_callback_info info)183 static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info)
184 {
185 OH_LOG_INFO(LOG_APP, "StartPhotoOrVideo Start");
186 Camera_ErrorCode ret = CAMERA_OK;
187 size_t requireArgc = 3;
188 size_t argc = 3;
189 napi_value args[3] = {nullptr};
190 napi_value result;
191 size_t typeLen = 0;
192 size_t videoIdLen = 0;
193 size_t photoIdLen = 0;
194 char* modeFlag = nullptr;
195 char* videoId = nullptr;
196 char* photoId = nullptr;
197
198 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
199
200 napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen);
201 modeFlag = new char[typeLen + 1];
202 napi_get_value_string_utf8(env, args[0], modeFlag, typeLen + 1, &typeLen);
203
204 napi_get_value_string_utf8(env, args[1], nullptr, 0, &videoIdLen);
205 videoId = new char[videoIdLen + 1];
206 napi_get_value_string_utf8(env, args[1], videoId, videoIdLen + 1, &videoIdLen);
207
208 napi_get_value_string_utf8(env, args[ARGS_TWO], nullptr, 0, &photoIdLen);
209 photoId = new char[photoIdLen + 1];
210 napi_get_value_string_utf8(env, args[ARGS_TWO], photoId, photoIdLen + 1, &photoIdLen);
211
212 if (!strcmp(modeFlag, "photo")) {
213 OH_LOG_ERROR(LOG_APP, "StartPhoto surfaceId %{public}s", photoId);
214 ret = ndkCamera_->StartPhoto(photoId);
215 } else if (!strcmp(modeFlag, "video")) {
216 ret = ndkCamera_->StartVideo(videoId, photoId);
217 OH_LOG_ERROR(LOG_APP, "StartPhotoOrVideo %{public}s, %{public}s", videoId, photoId);
218 }
219 napi_create_int32(env, ret, &result);
220 return result;
221 }
222
VideoOutputStart(napi_env env,napi_callback_info info)223 static napi_value VideoOutputStart(napi_env env, napi_callback_info info)
224 {
225 OH_LOG_INFO(LOG_APP, "VideoOutputStart Start");
226 napi_value result;
227 Camera_ErrorCode ret = ndkCamera_->VideoOutputStart();
228 napi_create_int32(env, ret, &result);
229 return result;
230 }
231
IsExposureModeSupported(napi_env env,napi_callback_info info)232 static napi_value IsExposureModeSupported(napi_env env, napi_callback_info info)
233 {
234 OH_LOG_INFO(LOG_APP, "IsExposureModeSupported exposureMode start.");
235 size_t requireArgc = 2;
236 size_t argc = 2;
237 napi_value args[2] = {nullptr};
238 napi_value result;
239
240 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
241
242 napi_valuetype valuetype0;
243 napi_typeof(env, args[0], &valuetype0);
244
245 int32_t exposureMode;
246 napi_get_value_int32(env, args[0], &exposureMode);
247
248 OH_LOG_ERROR(LOG_APP, "IsExposureModeSupported exposureMode : %{public}d", exposureMode);
249
250 ndkCamera_->IsExposureModeSupportedFn(exposureMode);
251 OH_LOG_INFO(LOG_APP, "IsExposureModeSupported exposureMode end.");
252 napi_create_int32(env, argc, &result);
253 return result;
254 }
255
IsMeteringPoint(napi_env env,napi_callback_info info)256 static napi_value IsMeteringPoint(napi_env env, napi_callback_info info)
257 {
258 size_t requireArgc = 2;
259 size_t argc = 2;
260 napi_value args[2] = {nullptr};
261 napi_value result;
262
263 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
264
265 napi_valuetype valuetype0;
266 napi_typeof(env, args[0], &valuetype0);
267 int x;
268 napi_get_value_int32(env, args[0], &x);
269
270 napi_valuetype valuetype1;
271 napi_typeof(env, args[0], &valuetype0);
272 int y;
273 napi_get_value_int32(env, args[1], &y);
274 ndkCamera_->IsMeteringPoint(x, y);
275 napi_create_int32(env, argc, &result);
276 return result;
277 }
278
IsExposureBiasRange(napi_env env,napi_callback_info info)279 static napi_value IsExposureBiasRange(napi_env env, napi_callback_info info)
280 {
281 OH_LOG_INFO(LOG_APP, "IsExposureBiasRange start.");
282 size_t requireArgc = 2;
283 size_t argc = 2;
284 napi_value args[2] = {nullptr};
285 napi_value result;
286
287 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
288
289 napi_valuetype valuetype0;
290 napi_typeof(env, args[0], &valuetype0);
291
292 int exposureBiasValue;
293 napi_get_value_int32(env, args[0], &exposureBiasValue);
294 ndkCamera_->IsExposureBiasRange(exposureBiasValue);
295 OH_LOG_INFO(LOG_APP, "IsExposureBiasRange end.");
296 napi_create_int32(env, argc, &result);
297 return result;
298 }
299
IsFocusModeSupported(napi_env env,napi_callback_info info)300 static napi_value IsFocusModeSupported(napi_env env, napi_callback_info info)
301 {
302 OH_LOG_INFO(LOG_APP, "IsFocusModeSupported start.");
303 size_t requireArgc = 2;
304 size_t argc = 2;
305 napi_value args[2] = {nullptr};
306 napi_value result;
307
308 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
309
310 napi_valuetype valuetype0;
311 napi_typeof(env, args[0], &valuetype0);
312
313 int32_t focusMode;
314 napi_get_value_int32(env, args[0], &focusMode);
315
316 OH_LOG_ERROR(LOG_APP, "IsFocusModeSupportedFn videoMode : %{public}d", focusMode);
317
318 ndkCamera_->IsFocusModeSupported(focusMode);
319 OH_LOG_INFO(LOG_APP, "IsFocusModeSupported end.");
320 napi_create_int32(env, argc, &result);
321 return result;
322 }
323
IsFocusPoint(napi_env env,napi_callback_info info)324 static napi_value IsFocusPoint(napi_env env, napi_callback_info info)
325 {
326 size_t requireArgc = 2;
327 size_t argc = 2;
328 napi_value args[2] = {nullptr};
329 napi_value result;
330
331 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
332
333 napi_valuetype valuetype0;
334 napi_typeof(env, args[0], &valuetype0);
335 double x;
336 napi_get_value_double(env, args[0], &x);
337
338 napi_valuetype valuetype1;
339 napi_typeof(env, args[1], &valuetype1);
340 double y;
341 napi_get_value_double(env, args[1], &y);
342
343 float focusPointX = static_cast<float>(x);
344 float focusPointY = static_cast<float>(y);
345 ndkCamera_->IsFocusPoint(focusPointX, focusPointY);
346 napi_create_int32(env, argc, &result);
347 return result;
348 }
349
GetVideoFrameWidth(napi_env env,napi_callback_info info)350 static napi_value GetVideoFrameWidth(napi_env env, napi_callback_info info)
351 {
352 OH_LOG_ERROR(LOG_APP, "GetVideoFrameWidth Start");
353 size_t argc = 1;
354 napi_value args[1] = {nullptr};
355 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
356
357 napi_value result = nullptr;
358 napi_create_int32(env, ndkCamera_->GetVideoFrameWidth(), &result);
359
360 OH_LOG_ERROR(LOG_APP, "GetVideoFrameWidth End");
361 return result;
362 }
363
GetVideoFrameHeight(napi_env env,napi_callback_info info)364 static napi_value GetVideoFrameHeight(napi_env env, napi_callback_info info)
365 {
366 OH_LOG_ERROR(LOG_APP, "GetVideoFrameHeight Start");
367 size_t argc = 1;
368 napi_value args[1] = {nullptr};
369 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
370
371 napi_value result = nullptr;
372 napi_create_int32(env, ndkCamera_->GetVideoFrameHeight(), &result);
373
374 OH_LOG_ERROR(LOG_APP, "GetVideoFrameHeight End");
375 return result;
376 }
377
GetVideoFrameRate(napi_env env,napi_callback_info info)378 static napi_value GetVideoFrameRate(napi_env env, napi_callback_info info)
379 {
380 OH_LOG_ERROR(LOG_APP, "GetVideoFrameRate Start");
381 size_t argc = 1;
382 napi_value args[1] = {nullptr};
383 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
384
385 napi_value result = nullptr;
386 napi_create_int32(env, ndkCamera_->GetVideoFrameRate(), &result);
387
388 OH_LOG_ERROR(LOG_APP, "GetVideoFrameRate End");
389 return result;
390 }
391
VideoOutputStopAndRelease(napi_env env,napi_callback_info info)392 static napi_value VideoOutputStopAndRelease(napi_env env, napi_callback_info info)
393 {
394 OH_LOG_ERROR(LOG_APP, "VideoOutputStopAndRelease Start");
395 size_t argc = 1;
396 napi_value args[1] = {nullptr};
397 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
398
399 napi_value result = nullptr;
400 ndkCamera_->VideoOutputStop();
401 ndkCamera_->VideoOutputRelease();
402
403 OH_LOG_ERROR(LOG_APP, "VideoOutputStopAndRelease End");
404 napi_create_int32(env, argc, &result);
405 return result;
406 }
407
TakePicture(napi_env env,napi_callback_info info)408 static napi_value TakePicture(napi_env env, napi_callback_info info)
409 {
410 OH_LOG_INFO(LOG_APP, "TakePicture Start");
411 napi_value result;
412 Camera_ErrorCode ret = ndkCamera_->TakePicture();
413 OH_LOG_ERROR(LOG_APP, "TakePicture result is %{public}d", ret);
414 napi_create_int32(env, ret, &result);
415 return result;
416 }
417
GetCaptureParam(napi_env env,napi_value captureConfigValue,Capture_Setting * config)418 static napi_value GetCaptureParam(napi_env env, napi_value captureConfigValue, Capture_Setting *config)
419 {
420 napi_value value = nullptr;
421 napi_get_named_property(env, captureConfigValue, "quality", &value);
422 napi_get_value_int32(env, value, &config->quality);
423
424 napi_get_named_property(env, captureConfigValue, "rotation", &value);
425 napi_get_value_int32(env, value, &config->rotation);
426
427 napi_get_named_property(env, captureConfigValue, "mirror", &value);
428 napi_get_value_bool(env, value, &config->mirror);
429
430 napi_get_named_property(env, captureConfigValue, "latitude", &value);
431 napi_get_value_int32(env, value, &config->latitude);
432
433 napi_get_named_property(env, captureConfigValue, "longitude", &value);
434 napi_get_value_int32(env, value, &config->longitude);
435
436 napi_get_named_property(env, captureConfigValue, "altitude", &value);
437 napi_get_value_int32(env, value, &config->altitude);
438
439 OH_LOG_INFO(LOG_APP, "get quality %{public}d, rotation %{public}d, mirror %{public}d, latitude "
440 "%{public}d, longitude %{public}d, altitude %{public}d", config->quality, config->rotation,
441 config->mirror, config->latitude, config->longitude, config->altitude);
442 return 0;
443 }
SetConfig(Capture_Setting settings,Camera_PhotoCaptureSetting * photoSetting,Camera_Location * location)444 static void SetConfig(Capture_Setting settings, Camera_PhotoCaptureSetting* photoSetting, Camera_Location* location)
445 {
446 if (photoSetting == nullptr || location == nullptr) {
447 OH_LOG_INFO(LOG_APP, "photoSetting is null");
448 }
449 photoSetting->quality = static_cast<Camera_QualityLevel>(settings.quality);
450 photoSetting->rotation = static_cast<Camera_ImageRotation>(settings.rotation);
451 photoSetting->mirror = settings.mirror;
452 location->altitude = settings.altitude;
453 location->latitude = settings.latitude;
454 location->longitude = settings.longitude;
455 photoSetting->location = location;
456 }
457
TakePictureWithSettings(napi_env env,napi_callback_info info)458 static napi_value TakePictureWithSettings(napi_env env, napi_callback_info info)
459 {
460 OH_LOG_INFO(LOG_APP, "TakePictureWithSettings Start");
461 size_t requireArgc = 1;
462 size_t argc = 1;
463 napi_value args[1] = {nullptr};
464 Camera_PhotoCaptureSetting photoSetting;
465 Capture_Setting setting_inner;
466 Camera_Location* location = new Camera_Location;
467
468 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
469 GetCaptureParam(env, args[0], &setting_inner);
470 SetConfig(setting_inner, &photoSetting, location);
471
472 napi_value result;
473 Camera_ErrorCode ret = ndkCamera_->TakePictureWithPhotoSettings(photoSetting);
474 OH_LOG_ERROR(LOG_APP, "TakePictureWithSettings result is %{public}d", ret);
475 napi_create_int32(env, ret, &result);
476 return result;
477 }
478
479 EXTERN_C_START
Init(napi_env env,napi_value exports)480 static napi_value Init(napi_env env, napi_value exports)
481 {
482 napi_property_descriptor desc[] = {
483 { "initCamera", nullptr, InitCamera, nullptr, nullptr, nullptr, napi_default, nullptr },
484 { "startPhotoOrVideo", nullptr, StartPhotoOrVideo, nullptr, nullptr, nullptr, napi_default, nullptr },
485 { "videoOutputStart", nullptr, VideoOutputStart, nullptr, nullptr, nullptr, napi_default, nullptr },
486 { "setZoomRatio", nullptr, SetZoomRatio, nullptr, nullptr, nullptr, napi_default, nullptr },
487 { "hasFlash", nullptr, HasFlash, nullptr, nullptr, nullptr, napi_default, nullptr },
488 { "isVideoStabilizationModeSupported", nullptr, IsVideoStabilizationModeSupported,
489 nullptr, nullptr, nullptr, napi_default, nullptr },
490 { "isExposureModeSupported", nullptr, IsExposureModeSupported,
491 nullptr, nullptr, nullptr, napi_default, nullptr },
492 { "isMeteringPoint", nullptr, IsMeteringPoint, nullptr, nullptr, nullptr, napi_default, nullptr },
493 { "isExposureBiasRange", nullptr, IsExposureBiasRange, nullptr, nullptr, nullptr, napi_default, nullptr },
494 { "IsFocusModeSupported", nullptr, IsFocusModeSupported, nullptr, nullptr, nullptr, napi_default, nullptr },
495 { "isFocusPoint", nullptr, IsFocusPoint, nullptr, nullptr, nullptr, napi_default, nullptr },
496 { "getVideoFrameWidth", nullptr, GetVideoFrameWidth, nullptr, nullptr, nullptr, napi_default, nullptr },
497 { "getVideoFrameHeight", nullptr, GetVideoFrameHeight, nullptr, nullptr, nullptr, napi_default, nullptr },
498 { "getVideoFrameRate", nullptr, GetVideoFrameRate, nullptr, nullptr, nullptr, napi_default, nullptr },
499 { "videoOutputStopAndRelease", nullptr, VideoOutputStopAndRelease,
500 nullptr, nullptr, nullptr, napi_default, nullptr },
501 { "takePicture", nullptr, TakePicture, nullptr, nullptr, nullptr, napi_default, nullptr },
502 { "takePictureWithSettings", nullptr, TakePictureWithSettings, nullptr, nullptr, nullptr,
503 napi_default, nullptr },
504 { "releaseSession", nullptr, ReleaseSession, nullptr, nullptr, nullptr, napi_default, nullptr },
505 { "releaseCamera", nullptr, ReleaseCamera, nullptr, nullptr, nullptr, napi_default, nullptr }
506 };
507 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
508 return exports;
509 }
510 EXTERN_C_END
511
512 static napi_module demoModule = {
513 .nm_version =1,
514 .nm_flags = 0,
515 .nm_filename = nullptr,
516 .nm_register_func = Init,
517 .nm_modname = "entry",
518 .nm_priv = ((void*)0),
519 .reserved = { 0 },
520 };
521
RegisterEntryModule(void)522 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
523 {
524 napi_module_register(&demoModule);
525 }