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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_COMPONENT_H
18 
19 #include <string>
20 
21 #include "base/utils/utils.h"
22 #include "core/components/video/texture_component.h"
23 #include "core/event/ace_event_helper.h"
24 #include "core/pipeline/base/element.h"
25 
26 namespace OHOS::Ace {
27 
28 enum class DevicePosition : int32_t {
29     CAMERA_FACING_FRONT = 0,
30     CAMERA_FACING_BACK,
31     CAMERA_FACING_OTHERS,
32 };
33 
34 enum class FlashType : int32_t {
35     OFF = 0,
36     ON,
37     AUTO,
38     TORCH,
39 };
40 
41 struct TakePhotoParams {
42     std::string quality;
43     std::string success;
44     std::string fail;
45     std::string complete;
46 
clearTakePhotoParams47     void clear()
48     {
49         quality.clear();
50         success.clear();
51         fail.clear();
52         complete.clear();
53     }
54 };
55 
56 class CameraController : public virtual AceType {
57     DECLARE_ACE_TYPE(CameraController, AceType);
58 
59 public:
60     using TakePhotoImpl = std::function<void(const TakePhotoParams&)>;
61     using StartRecorderImpl = std::function<void()>;
62     using CloseRecordImpl = std::function<void(const std::string&)>;
63 
TakePhoto(const TakePhotoParams & params)64     void TakePhoto(const TakePhotoParams& params)
65     {
66         if (takePhotoImpl_) {
67             takePhotoImpl_(params);
68         }
69     }
70 
SetTakePhotoImpl(TakePhotoImpl && takePhotoImpl)71     void SetTakePhotoImpl(TakePhotoImpl&& takePhotoImpl)
72     {
73         takePhotoImpl_ = std::move(takePhotoImpl);
74     }
75 
StartRecord()76     void StartRecord()
77     {
78         if (startRecorderImpl_) {
79             startRecorderImpl_();
80         }
81     }
82 
SetStartRecordImpl(StartRecorderImpl && startRecorderImpl)83     void SetStartRecordImpl(StartRecorderImpl&& startRecorderImpl)
84     {
85         startRecorderImpl_ = std::move(startRecorderImpl);
86     }
87 
CloseRecorder(const std::string & recorderId)88     void CloseRecorder(const std::string& recorderId)
89     {
90         if (closeRecordImpl_) {
91             closeRecordImpl_(recorderId);
92         }
93     }
94 
SetCloseRecorderImpl(CloseRecordImpl && closeRecordImpl)95     void SetCloseRecorderImpl(CloseRecordImpl&& closeRecordImpl)
96     {
97         closeRecordImpl_ = std::move(closeRecordImpl);
98     }
99 
100 private:
101     TakePhotoImpl takePhotoImpl_;
102     StartRecorderImpl startRecorderImpl_;
103     CloseRecordImpl closeRecordImpl_;
104 };
105 
106 class ACE_EXPORT CameraComponent : public TextureComponent {
107     DECLARE_ACE_TYPE(CameraComponent, TextureComponent);
108 
109 public:
CameraComponent()110     CameraComponent()
111     {
112         cameraController_ = AceType::MakeRefPtr<CameraController>();
113     };
114     ~CameraComponent() override = default;
115 
GetCameraController()116     RefPtr<CameraController> GetCameraController() const
117     {
118         return cameraController_;
119     }
120 
SetFlash(FlashType flash)121     void SetFlash(FlashType flash)
122     {
123         flash_ = flash;
124     }
125 
GetFlash()126     FlashType GetFlash() const
127     {
128         return flash_;
129     }
130 
SetDevicePosition(DevicePosition devicePosition)131     void SetDevicePosition(DevicePosition devicePosition)
132     {
133         devicePosition_ = devicePosition;
134     }
135 
GetDevicePosition()136     DevicePosition GetDevicePosition() const
137     {
138         return devicePosition_;
139     }
140 
SetErrorEventId(const EventMarker & eventId)141     void SetErrorEventId(const EventMarker& eventId)
142     {
143         errorEventId_ = eventId;
144     }
145 
GetErrorEventId()146     const EventMarker& GetErrorEventId() const
147     {
148         return errorEventId_;
149     }
150 
SetCameraId(const std::string & cameraId)151     void SetCameraId(const std::string& cameraId)
152     {
153         cameraId_ = cameraId;
154     }
155 
GetCameraId()156     const std::string& GetCameraId() const
157     {
158         return cameraId_;
159     }
160 
SetResolutionWidth(int32_t resolutionWidth)161     void SetResolutionWidth(int32_t resolutionWidth)
162     {
163         resolutionWidth_ = resolutionWidth;
164     }
165 
GetResolutionWidth()166     int32_t GetResolutionWidth() const
167     {
168         return resolutionWidth_;
169     }
170 
SetResolutionHeight(int32_t resolutionHeight)171     void SetResolutionHeight(int32_t resolutionHeight)
172     {
173         resolutionHeight_ = resolutionHeight;
174     }
175 
GetResolutionHeight()176     int32_t GetResolutionHeight() const
177     {
178         return resolutionHeight_;
179     }
180 
SignSetResolution(bool isSetResolution)181     void SignSetResolution(bool isSetResolution)
182     {
183         isSetResolution_ = isSetResolution;
184     }
185 
IsSetResolution()186     bool IsSetResolution() const
187     {
188         return isSetResolution_;
189     }
190 
191     RefPtr<RenderNode> CreateRenderNode() override;
192     RefPtr<Element> CreateElement() override;
193 
194 private:
195     RefPtr<CameraController> cameraController_;
196     FlashType flash_ = FlashType::AUTO;
197     DevicePosition devicePosition_ = DevicePosition::CAMERA_FACING_BACK;
198     EventMarker errorEventId_;
199     std::string cameraId_;
200     int32_t resolutionWidth_ = 0;
201     int32_t resolutionHeight_ = 0;
202     bool isSetResolution_ = false;
203 };
204 
205 } // namespace OHOS::Ace
206 
207 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_COMPONENT_H
208