• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 "camera_napi_object_types.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <utility>
21 
22 #include "camera_device.h"
23 #include "camera_manager.h"
24 #include "camera_napi_metadata_utils.h"
25 #include "camera_napi_object.h"
26 #include "js_native_api_types.h"
27 #include "metadata_output.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 
GenerateNapiValue(napi_env env)32 napi_value CameraNapiObjectTypes::GenerateNapiValue(napi_env env)
33 {
34     napi_value value = GetCameraNapiObject().CreateNapiObjFromMap(env);
35     ptrHolder_.clear();
36     return value;
37 };
38 
GetCameraNapiObject()39 CameraNapiObject& CameraNapiObjSize::GetCameraNapiObject()
40 {
41     return *Hold<CameraNapiObject>(
42         CameraNapiObject::CameraNapiObjFieldMap { { "width", &size_.width }, { "height", &size_.height } });
43 }
44 
GetCameraNapiObject()45 CameraNapiObject& CameraNapiObjFrameRateRange::GetCameraNapiObject()
46 {
47     return *Hold<CameraNapiObject>(
48         CameraNapiObject::CameraNapiObjFieldMap { { "min", &frameRateRange_[0] }, { "max", &frameRateRange_[1] } });
49 }
50 
GetCameraNapiObject()51 CameraNapiObject& CameraNapiObjProfile::GetCameraNapiObject()
52 {
53     auto sizeObj = Hold<CameraNapiObjSize>(profile_.size_);
54     auto format = Hold<int32_t>(profile_.format_);
55     return *Hold<CameraNapiObject>(
56         CameraNapiObject::CameraNapiObjFieldMap { { "format", format }, { "size", &sizeObj->GetCameraNapiObject() } });
57 }
58 
GetCameraNapiObject()59 CameraNapiObject& CameraNapiObjVideoProfile::GetCameraNapiObject()
60 {
61     auto format = Hold<int32_t>(videoProfile_.format_);
62     auto sizeObj = Hold<CameraNapiObjSize>(videoProfile_.size_);
63     auto frameRateRange = Hold<CameraNapiObjFrameRateRange>(videoProfile_.framerates_);
64     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
65         { "format", format },
66         { "size", &sizeObj->GetCameraNapiObject() },
67         { "frameRateRange", &frameRateRange->GetCameraNapiObject() } });
68 }
69 
GetCameraNapiObject()70 CameraNapiObject& CameraNapiObjDepthProfile::GetCameraNapiObject()
71 {
72     auto format = Hold<int32_t>(depthProfile_.format_);
73     auto sizeObj = Hold<CameraNapiObjSize>(depthProfile_.size_);
74     auto dataAccuracy = Hold<int32_t>(depthProfile_.dataAccuracy_);
75     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
76         { "format", format },
77         { "size", &sizeObj->GetCameraNapiObject() },
78         { "dataAccuracy", dataAccuracy } });
79 }
80 
GetCameraNapiObject()81 CameraNapiObject& CameraNapiObjCameraDevice::GetCameraNapiObject()
82 {
83     auto cameraId = Hold<std::string>(cameraDevice_.GetID());
84     auto cameraPosition = Hold<int32_t>(cameraDevice_.GetPosition());
85     auto cameraType = Hold<int32_t>(cameraDevice_.GetCameraType());
86     auto connectionType = Hold<int32_t>(cameraDevice_.GetConnectionType());
87     auto hostDeviceName = Hold<std::string>(cameraDevice_.GetHostName());
88     auto hostDeviceType = Hold<uint32_t>(cameraDevice_.GetDeviceType());
89     auto cameraOrientation = Hold<int32_t>(cameraDevice_.GetCameraOrientation());
90     auto isRetractable = Hold<bool>(cameraDevice_.GetisRetractable());
91     auto lensEquivalentFocalLength = Hold<std::vector<int32_t>>(cameraDevice_.GetLensEquivalentFocalLength());
92     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
93         { "cameraId", cameraId },
94         { "cameraPosition", cameraPosition },
95         { "cameraType", cameraType },
96         { "connectionType", connectionType },
97         { "hostDeviceName", hostDeviceName },
98         { "hostDeviceType", hostDeviceType },
99         { "cameraOrientation", cameraOrientation },
100         { "isRetractable", isRetractable },
101         { "lensEquivalentFocalLength", lensEquivalentFocalLength } });
102 }
103 
GetCameraNapiObject()104 CameraNapiObject& CameraNapiBoundingBox::GetCameraNapiObject()
105 {
106     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
107         { "topLeftX", &rect_.topLeftX },
108         { "topLeftY", &rect_.topLeftY },
109         { "width", &rect_.width },
110         { "height", &rect_.height } });
111 }
112 
GetCameraNapiObject()113 CameraNapiObject& CameraNapiObjMetadataObject::GetCameraNapiObject()
114 {
115     auto type = Hold<int32_t>(CameraNapiMetadataUtils::MapMetadataObjSupportedTypesEnum(metadataObject_.GetType()));
116     auto timestamp = Hold<int32_t>(metadataObject_.GetTimestamp());
117     auto boundingBox = Hold<CameraNapiBoundingBox>(*Hold<Rect>(metadataObject_.GetBoundingBox()));
118     auto objectId = Hold<int32_t>(metadataObject_.GetObjectId());
119     auto confidence = Hold<int32_t>(metadataObject_.GetConfidence());
120     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
121         { "type", type },
122         { "timestamp", timestamp },
123         { "boundingBox", &boundingBox->GetCameraNapiObject() },
124         { "objectId", objectId },
125         { "confidence", confidence }});
126 }
127 
GetCameraNapiObject()128 CameraNapiObject& CameraNapiObjCameraOutputCapability::GetCameraNapiObject()
129 {
130     auto previewProfiles = Hold<std::list<CameraNapiObject>>();
131     auto nativePreviewProfiles = Hold<std::vector<Profile>>(cameraOutputCapability_.GetPreviewProfiles());
132     for (auto& profile : *nativePreviewProfiles) {
133         previewProfiles->emplace_back(std::move(Hold<CameraNapiObjProfile>(profile)->GetCameraNapiObject()));
134     }
135 
136     auto photoProfiles = Hold<std::list<CameraNapiObject>>();
137     auto nativePhotoProfiles = Hold<std::vector<Profile>>(cameraOutputCapability_.GetPhotoProfiles());
138     for (auto& profile : *nativePhotoProfiles) {
139         photoProfiles->emplace_back(std::move(Hold<CameraNapiObjProfile>(profile)->GetCameraNapiObject()));
140     }
141 
142     auto videoProfiles = Hold<std::list<CameraNapiObject>>();
143     auto nativeVideoProfiles = Hold<std::vector<VideoProfile>>(cameraOutputCapability_.GetVideoProfiles());
144     for (auto& profile : *nativeVideoProfiles) {
145         videoProfiles->emplace_back(std::move(Hold<CameraNapiObjVideoProfile>(profile)->GetCameraNapiObject()));
146     }
147 
148     auto depthProfiles = Hold<std::list<CameraNapiObject>>();
149     auto nativeDepthProfiles = Hold<std::vector<DepthProfile>>(cameraOutputCapability_.GetDepthProfiles());
150     for (auto& profile : *nativeDepthProfiles) {
151         depthProfiles->emplace_back(std::move(Hold<CameraNapiObjDepthProfile>(profile)->GetCameraNapiObject()));
152     }
153 
154     auto supportedMetadataObjectTypes = Hold<std::vector<int32_t>>();
155     auto nativeSupportedMetadataObjectTypes = cameraOutputCapability_.GetSupportedMetadataObjectType();
156     for (auto& type : nativeSupportedMetadataObjectTypes) {
157         supportedMetadataObjectTypes->emplace_back(static_cast<int32_t>(type));
158     }
159 
160     return *Hold<CameraNapiObject>(CameraNapiObject::CameraNapiObjFieldMap {
161         { "previewProfiles", previewProfiles },
162         { "photoProfiles", photoProfiles },
163         { "videoProfiles", videoProfiles },
164         { "depthProfiles", depthProfiles },
165         { "supportedMetadataObjectTypes", supportedMetadataObjectTypes } });
166 }
167 } // namespace CameraStandard
168 } // namespace OHOS
169