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_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_ELEMENT_H
18 
19 #ifdef OHOS_STANDARD_SYSTEM
20 #include "core/components/camera/standard_system/camera.h"
21 #else
22 #include "core/components/camera/large_system/camera.h"
23 #endif
24 
25 #if defined(PREVIEW)
26 #include "core/components/align/align_component.h"
27 #include "core/components/text/text_component.h"
28 #include "core/components/theme/theme_manager.h"
29 #endif
30 #include "core/components/camera/camera_component.h"
31 #include "core/components/camera/camera_theme.h"
32 #include "core/focus/focus_node.h"
33 #include "core/pipeline/base/composed_element.h"
34 #include "core/pipeline/base/render_element.h"
35 
36 namespace OHOS::Ace {
37 
38 enum CameraState : int32_t {
39     STATE_UNDEFINED,
40     STATE_ENABLE,
41     STATE_DISABLE
42 };
43 
44 class CameraElement : public RenderElement, public FocusNode {
45     DECLARE_ACE_TYPE(CameraElement, RenderElement, FocusNode);
46 
47 public:
48     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
49     using EventCallback = std::function<void(const std::string&)>;
50 
51     CameraElement() = default;
52     ~CameraElement() override;
53 
54     void Prepare(const WeakPtr<Element>& parent) override;
55     void SetNewComponent(const RefPtr<Component>& newComponent) override;
56     void TakePhoto(const TakePhotoParams& params);
57 
58 #ifdef OHOS_STANDARD_SYSTEM
59     void StartRecord();
60     void CloseRecorder(const std::string& params);
61     void StartPreview();
62     void StopPreview();
63 #endif
64 
65 private:
66     void SetMethodCall(const RefPtr<CameraComponent>& cameraComponent);
67     void OnTextureSize(int64_t textureId, int32_t textureWidth, int32_t textureHeight);
68     void CreatePlatformResource();
69     void ReleasePlatformResource();
70     void InitListener();
71     void OnTakePhotoCallBack(const std::map<std::string, std::string>& result);
72     void ExecuteJsCallback(const std::string& callbackId, const std::string& result);
73     void InitEvent(const RefPtr<CameraComponent>& cameraComponent);
74     void OnError(const std::string& errorcode, const std::string& errormsg);
75     void HiddenChange(bool hidden);
76     void OnPrepared();
77 
78 #ifdef OHOS_STANDARD_SYSTEM
79     void CreateCamera();
80     void OnRecorderCallBack(const std::map<std::string, std::string>& result);
81     void OnTextureOffset(int64_t textureId, int32_t x, int32_t y);
82     void TakePhoto(const Size& size);
83     void CloseRecorder();
84 
85     std::string recorderBackId_;
86     bool isRecording_ = false;
87     bool isPreViewing_ = false;
88 #else
89     void CreateTexture();
90     void CreateCamera(int64_t id, ErrorCallback &&errorCallback);
91     void UpdateCamera(const RefPtr<CameraComponent>& cameraComponent);
92     void OnPreViewSizeChange(int32_t preViewWidth, int32_t preViewHeight);
93     void InitDeclarativePara(const RefPtr<CameraComponent>& cameraComponent);
94     void HandleDeclarativePara();
95     void ReleaseDeclarativePara();
96     void OnTextureRefresh();
97 
98     RefPtr<Texture> texture_;
99     FlashType flash_ = FlashType::AUTO;
100     DevicePosition devicePosition_ = DevicePosition::CAMERA_FACING_BACK;
101     int32_t preViewWidth_ = 0;
102     int32_t preViewHeight_ = 0;
103     int32_t resolutionWidth_ = 0;
104     int32_t resolutionHeight_ = 0;
105     bool isSetResolution_ = false;
106     std::string cameraId_;
107 #endif
108 #if defined(PREVIEW)
109     void UpdateChildInner(const RefPtr<Component>& childComponent);
110     const RefPtr<Component> CreateErrorText(const std::string& errorMsg);
111     TextDirection textDirection_ = TextDirection::LTR;
112 #endif
113 
114     EventCallback onTakePhotoEvent_;
115     EventCallback onError_;
116     TakePhotoParams callbackIds_;
117     RefPtr<CameraTheme> theme_;
118     RefPtr<Camera> camera_;
119 
120     CameraState cameraState_ = CameraState::STATE_UNDEFINED;
121 };
122 
123 } // namespace OHOS::Ace
124 
125 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_ELEMENT_H
126