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 "hos_camera_demo.h"
17 #include <cstdio>
18 #include <getopt.h>
19
20 namespace OHOS::Camera {
Usage(FILE * fp)21 static void Usage(FILE* fp)
22 {
23 fprintf(fp,
24 "Options:\n"
25 "-h | --help Print this message\n"
26 "-o | --offline stream offline test\n"
27 "-c | --capture capture one picture\n"
28 "-w | --set WB Set white balance Cloudy\n"
29 "-v | --video capture Viedeo of 10s\n"
30 "-a | --Set AE Set Auto exposure\n"
31 "-f | --Set Flashlight Set flashlight ON 5s OFF\n"
32 "-q | --quit stop preview and quit this app\n");
33 }
34
35 const static struct option longOptions[] = {
36 {"help", no_argument, nullptr, 'h'}, {"capture", no_argument, nullptr, 'c'},
37 {"WB", no_argument, nullptr, 'w'}, {"video", no_argument, nullptr, 'v'},
38 {"quit", no_argument, nullptr, 'q'}, {"AE", no_argument, nullptr, 'a'},
39 {"OL", no_argument, nullptr, 'o'}, {"flashlight", no_argument, nullptr, 'f'},
40 {0, 0, 0, 0}
41 };
42
PutMenuAndGetChr(void)43 static int PutMenuAndGetChr(void)
44 {
45 constexpr uint32_t inputCount = 50;
46 int c = 0;
47 char strs[inputCount];
48
49 Usage(stdout);
50 CAMERA_LOGD("pls input command(input -q exit this app)\n");
51 fgets(strs, inputCount, stdin);
52
53 for (int i = 0; i < inputCount; i++) {
54 if (strs[i] != '-') {
55 c = strs[i];
56 break;
57 }
58 }
59
60 return c;
61 }
62
PreviewOn(int mode,const std::shared_ptr<HosCameraDemo> & mainDemo)63 static RetCode PreviewOn(int mode, const std::shared_ptr<HosCameraDemo>& mainDemo)
64 {
65 RetCode rc = RC_OK;
66 CAMERA_LOGD("main test: PreviewOn enter");
67
68 rc = mainDemo->StartPreviewStream();
69 if (rc != RC_OK) {
70 CAMERA_LOGE("main test: PreviewOn StartPreviewStream error");
71 return RC_ERROR;
72 }
73
74 if (mode == 0) {
75 rc = mainDemo->StartCaptureStream();
76 if (rc != RC_OK) {
77 CAMERA_LOGE("main test: PreviewOn StartCaptureStream error");
78 return RC_ERROR;
79 }
80 } else {
81 rc = mainDemo->StartVideoStream();
82 if (rc != RC_OK) {
83 CAMERA_LOGE("main test: PreviewOn StartVideoStream error");
84 return RC_ERROR;
85 }
86 }
87
88 rc = mainDemo->CaptureON(STREAM_ID_PREVIEW, CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
89 if (rc != RC_OK) {
90 CAMERA_LOGE("main test: PreviewOn mainDemo->CaptureON() preview error");
91 return RC_ERROR;
92 }
93
94 CAMERA_LOGD("main test: PreviewOn exit");
95 return RC_OK;
96 }
97
PreviewOff(const std::shared_ptr<HosCameraDemo> & mainDemo)98 static void PreviewOff(const std::shared_ptr<HosCameraDemo>& mainDemo)
99 {
100 CAMERA_LOGD("main test: PreviewOff enter");
101
102 mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
103 mainDemo->ReleaseAllStream();
104
105 CAMERA_LOGD("main test: PreviewOff exit");
106 }
107
FlashLightTest(const std::shared_ptr<HosCameraDemo> & mainDemo)108 static void FlashLightTest(const std::shared_ptr<HosCameraDemo>& mainDemo)
109 {
110 constexpr size_t delayTime = 5;
111
112 PreviewOff(mainDemo);
113 mainDemo->ReleaseCameraDevice();
114 sleep(1);
115 mainDemo->FlashlightOnOff(true);
116 sleep(delayTime);
117 mainDemo->FlashlightOnOff(false);
118 mainDemo->InitCameraDevice();
119 PreviewOn(0, mainDemo);
120 }
121
OfflineTest(const std::shared_ptr<HosCameraDemo> & mainDemo)122 static void OfflineTest(const std::shared_ptr<HosCameraDemo>& mainDemo)
123 {
124 RetCode rc = RC_OK;
125
126 PreviewOff(mainDemo);
127
128 mainDemo->StartDualStreams(STREAM_ID_CAPTURE);
129 mainDemo->CaptureOnDualStreams(STREAM_ID_CAPTURE);
130 sleep(1);
131
132 rc = mainDemo->StreamOffline(STREAM_ID_CAPTURE);
133 if (rc != RC_OK) {
134 CAMERA_LOGE("main test: mainDemo->StreamOffline error");
135 } else {
136 sleep(1);
137 mainDemo->InitCameraDevice();
138 rc = PreviewOn(0, mainDemo);
139 if (rc != RC_OK) {
140 CAMERA_LOGE("main test: PreviewOn() error");
141 }
142 }
143 }
144
CaptureTest(const std::shared_ptr<HosCameraDemo> & mainDemo)145 static void CaptureTest(const std::shared_ptr<HosCameraDemo>& mainDemo)
146 {
147 RetCode rc = RC_OK;
148 constexpr size_t delayTime = 5;
149
150 rc = mainDemo->CaptureON(STREAM_ID_CAPTURE, CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
151 if (rc != RC_OK) {
152 CAMERA_LOGE("main test: mainDemo->CaptureON() capture error");
153 return;
154 }
155
156 sleep(delayTime);
157 rc = mainDemo->CaptureOff(CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
158 if (rc != RC_OK) {
159 CAMERA_LOGE("main test: mainDemo->CaptureOff() capture error");
160 return;
161 }
162 }
163
VideoTest(const std::shared_ptr<HosCameraDemo> & mainDemo)164 static void VideoTest(const std::shared_ptr<HosCameraDemo>& mainDemo)
165 {
166 RetCode rc = RC_OK;
167 constexpr size_t delayTime = 5;
168
169 PreviewOff(mainDemo);
170 mainDemo->StartDualStreams(STREAM_ID_VIDEO);
171 mainDemo->CaptureOnDualStreams(STREAM_ID_VIDEO);
172
173 sleep(delayTime);
174 mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
175 mainDemo->CaptureOff(CAPTURE_ID_VIDEO, CAPTURE_VIDEO);
176 mainDemo->ReleaseAllStream();
177
178 rc = PreviewOn(0, mainDemo);
179 if (rc != RC_OK) {
180 CAMERA_LOGE("main test: video PreviewOn() error plese -q exit demo");
181 }
182 }
183
ManuList(const std::shared_ptr<HosCameraDemo> & mainDemo,const int argc,char ** argv)184 static void ManuList(const std::shared_ptr<HosCameraDemo>& mainDemo,
185 const int argc, char** argv)
186 {
187 int idx, c;
188 int awb = 1;
189 const char *shortOptions = "h:cwvaqof:";
190
191 c = getopt_long(argc, argv, shortOptions, longOptions, &idx);
192 while(1) {
193 switch (c) {
194 case 'h':
195 c = PutMenuAndGetChr();
196 break;
197
198 case 'f':
199 FlashLightTest(mainDemo);
200 c = PutMenuAndGetChr();
201 break;
202
203 case 'o':
204 OfflineTest(mainDemo);
205 c = PutMenuAndGetChr();
206 break;
207
208 case 'c':
209 CaptureTest(mainDemo);
210 c = PutMenuAndGetChr();
211 break;
212
213 case 'w':
214 if (awb) {
215 mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_INCANDESCENT);
216 } else {
217 mainDemo->SetAwbMode(OHOS_CAMERA_AWB_MODE_OFF);
218 }
219 awb = !awb;
220 c = PutMenuAndGetChr();
221 break;
222
223 case 'a':
224 mainDemo->SetAeExpo();
225 c = PutMenuAndGetChr();
226 break;
227
228 case 'v':
229 VideoTest(mainDemo);
230 c = PutMenuAndGetChr();
231 break;
232
233 case 'q':
234 PreviewOff(mainDemo);
235 mainDemo->QuitDemo();
236 return;
237
238 default:
239 CAMERA_LOGE("main test: command error please retry input command");
240 c = PutMenuAndGetChr();
241 break;
242 }
243 }
244 }
245
main(int argc,char ** argv)246 int main(int argc, char** argv)
247 {
248 RetCode rc = RC_OK;
249
250 auto mainDemo = std::make_shared<HosCameraDemo>();
251 rc = mainDemo->InitSensors();
252 if (rc == RC_ERROR) {
253 CAMERA_LOGE("main test: mainDemo->InitSensors() error\n");
254 return -1;
255 }
256 rc = mainDemo->InitCameraDevice();
257 if (rc == RC_ERROR) {
258 CAMERA_LOGE("main test: mainDemo->InitCameraDevice() error\n");
259 return -1;
260 }
261
262 rc = PreviewOn(0, mainDemo);
263 if (rc != RC_OK) {
264 CAMERA_LOGE("main test: PreviewOn() error demo exit");
265 return -1;
266 }
267
268 ManuList(mainDemo, argc, argv);
269
270 return RC_OK;
271 }
272 } // namespace Camera
273
main(int argc,char ** argv)274 int main(int argc, char** argv)
275 {
276 OHOS::Camera::main(argc, argv);
277
278 return 0;
279 }
280