• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "frameworks/bridge/common/dom/dom_camera.h"
17 
18 #include "frameworks/bridge/common/dom/dom_type.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 
21 namespace OHOS::Ace::Framework {
22 namespace {
23 
24 const char CALLBACK_SUCCESS[] = "success";
25 const char CALLBACK_FAIL[] = "fail";
26 const char CALLBACK_COMPLETE[] = "complete";
27 const char QUALITY[] = "quality";
28 const char DEVICE_POSITION_FRONT[] = "front";
29 const char FLUSH_ON[] = "on";
30 const char FLUSH_OFF[] = "off";
31 const char FLUSH_TORCH[] = "torch";
32 const char FLUSH_AUTO[] = "auto";
33 const char START_STR[] = "[\"";
34 
35 } // namespace
36 
DOMCamera(NodeId nodeId,const std::string & nodeName)37 DOMCamera::DOMCamera(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
38 {
39     cameraComponent_ = AceType::MakeRefPtr<CameraComponent>();
40 #if defined(PREVIEW)
41     if (IsRightToLeft()) {
42         cameraComponent_->SetTextDirection(TextDirection::RTL);
43     }
44 #endif
45 }
46 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)47 bool DOMCamera::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
48 {
49     // Operator map for attr
50     static const std::unordered_map<std::string, void (*)(const RefPtr<CameraComponent>&, const std::string&)>
51         attrOperators = {
52             { DOM_CAMERA_FLASH,
53                 [](const RefPtr<CameraComponent>& cameraComponent, const std::string& val) {
54                     cameraComponent->SetFlash(GetFlashType(val));
55                 } },
56             { DOM_CAMERA_DEVICE_POSITION,
57                 [](const RefPtr<CameraComponent>& cameraComponent, const std::string& val) {
58                     cameraComponent->SetDevicePosition(GetDevicePosition(val));
59                 } },
60         };
61     auto operatorIter = attrOperators.find(attr.first);
62     if (operatorIter != attrOperators.end()) {
63         operatorIter->second(cameraComponent_, attr.second);
64         return true;
65     }
66     return false;
67 }
68 
AddSpecializedEvent(int32_t pageId,const std::string & event)69 bool DOMCamera::AddSpecializedEvent(int32_t pageId, const std::string& event)
70 {
71     // Operator map for event
72     static const std::unordered_map<std::string, void (*)(const RefPtr<CameraComponent>&, const EventMarker&)>
73         eventOperators = {
74             { DOM_ERROR,
75                 [](const RefPtr<CameraComponent>& cameraComponent, const EventMarker& event) {
76                     cameraComponent->SetErrorEventId(event);
77                 } },
78         };
79     auto operatorIter = eventOperators.find(event);
80     if (operatorIter != eventOperators.end()) {
81         operatorIter->second(cameraComponent_, EventMarker(GetNodeIdForEvent(), event, pageId));
82         return true;
83     }
84     return false;
85 }
86 
CallSpecializedMethod(const std::string & method,const std::string & args)87 void DOMCamera::CallSpecializedMethod(const std::string& method, const std::string& args)
88 {
89     LOGI("DOMCamera: method: %{public}s args: %{public}s", method.c_str(), args.c_str());
90     if (method == DOM_TAKE_PHOTO) {
91         auto controller = cameraComponent_->GetCameraController();
92         if (controller) {
93             controller->TakePhoto(GetParamFromJson(args));
94         }
95     }
96 
97     if (method == DOM_CAMERA_START_RECORD) {
98         auto controller = cameraComponent_->GetCameraController();
99         if (controller) {
100             controller->StartRecord();
101         }
102     }
103 
104     if (method == DOM_CAMERA_CLOSE_RECORDER) {
105         auto controller = cameraComponent_->GetCameraController();
106         if (controller) {
107             controller->CloseRecorder(GetRecorderParam(args));
108         }
109     }
110 }
111 
GetParamFromJson(const std::string & args) const112 TakePhotoParams DOMCamera::GetParamFromJson(const std::string& args) const
113 {
114     std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
115 
116     TakePhotoParams takePhotoParams;
117     if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() == 0) {
118         LOGE("parse args error");
119         return takePhotoParams;
120     }
121     std::unique_ptr<JsonValue> firstItem = argsValue->GetArrayItem(0);
122     std::string quality = firstItem->GetString(QUALITY);
123     if (!quality.empty()) {
124         takePhotoParams.quality = quality;
125     }
126 
127     std::string successId = firstItem->GetString(CALLBACK_SUCCESS);
128     if (!successId.empty()) {
129         takePhotoParams.success = successId;
130     }
131 
132     std::string failId = firstItem->GetString(CALLBACK_FAIL);
133     if (!failId.empty()) {
134         takePhotoParams.fail = failId;
135     }
136 
137     std::string completeId = firstItem->GetString(CALLBACK_COMPLETE);
138     if (!completeId.empty()) {
139         takePhotoParams.complete = completeId;
140     }
141 
142     return takePhotoParams;
143 }
144 
GetRecorderParam(const std::string & args) const145 std::string DOMCamera::GetRecorderParam(const std::string& args) const
146 {
147     size_t len = args.length();
148     size_t pos = args.find(START_STR);
149     size_t strLen = sizeof(START_STR) - 1;
150     int32_t result = 0;
151 
152     if (pos == std::string::npos || (pos + strLen) >= len) {
153         return std::string();
154     }
155     std::stringstream ss;
156     ss << args.substr(pos + strLen);
157     ss >> result;
158 
159     return std::to_string(result);
160 }
161 
GetFlashType(const std::string & val)162 FlashType DOMCamera::GetFlashType(const std::string& val)
163 {
164     static const std::unordered_map<std::string, FlashType> flashTypes = {
165         { FLUSH_ON, FlashType::ON },
166         { FLUSH_OFF, FlashType::OFF },
167         { FLUSH_TORCH, FlashType::TORCH },
168         { FLUSH_AUTO, FlashType::AUTO },
169     };
170     auto operatorIter = flashTypes.find(val);
171     if (operatorIter != flashTypes.end()) {
172         return operatorIter->second;
173     }
174     return FlashType::AUTO;
175 }
176 
GetDevicePosition(const std::string & val)177 DevicePosition DOMCamera::GetDevicePosition(const std::string& val)
178 {
179     if (val == DEVICE_POSITION_FRONT) {
180         return DevicePosition::CAMERA_FACING_FRONT;
181     } else {
182         return DevicePosition::CAMERA_FACING_BACK;
183     }
184 }
185 
186 } // namespace OHOS::Ace::Framework