• 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 "ohos_camera_demo_3a.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             "-c | --capture             capture one picture\n"
27             "-e | --auto exposure       Auto Exposure\n"
28             "-f | --auto focus          Auto Focus\n"
29             "-w | --auto white balance  Auto White Balance\n"
30             "-g | --exposure lock       Auto Exposure Lock\n"
31             "-i | --white balance lock  Auto White Balance Lock\n"
32             "-v | --white balance lock  Video test\n"
33             "-q | --quit                stop preview and quit this app\n");
34 }
35 
36 const static struct option LONG_OPTIONS[] = {
37     {"help", no_argument, nullptr, 'h'}, {"capture", no_argument, nullptr, 'c'},
38     {"ae", no_argument, nullptr, 'e'}, {"af", no_argument, nullptr, 'f'},
39     {"awb", no_argument, nullptr, 'w'}, {"ael", no_argument, nullptr, 'g'},
40     {"awbl", no_argument, nullptr, 'i'}, {"video", no_argument, nullptr, 'v'},
41     {nullptr, 0, nullptr, 0}
42 };
43 
PutMenuAndGetChr(void)44 static int PutMenuAndGetChr(void)
45 {
46     constexpr uint32_t inputCount = 50;
47     int c = 0;
48     char strs[inputCount];
49 
50     Usage(stdout);
51     CAMERA_LOGD("pls input command(input -q exit this app)\n");
52     fgets(strs, inputCount, stdin);
53 
54     for (int i = 0; i < inputCount; i++) {
55         if (strs[i] != '-') {
56             c = strs[i];
57             break;
58         }
59     }
60 
61     return c;
62 }
63 
PreviewOn(int mode,const std::shared_ptr<OhosCameraDemo> & mainDemo)64 static RetCode PreviewOn(int mode, const std::shared_ptr<OhosCameraDemo>& mainDemo)
65 {
66     RetCode rc = RC_OK;
67     CAMERA_LOGD("main test: PreviewOn enter");
68 
69     rc = mainDemo->StartPreviewStream();
70     if (rc != RC_OK) {
71         CAMERA_LOGE("main test: PreviewOn StartPreviewStream error");
72         return RC_ERROR;
73     }
74 
75     if (mode == 0) {
76         rc = mainDemo->StartCaptureStream();
77         if (rc != RC_OK) {
78             CAMERA_LOGE("main test: PreviewOn StartCaptureStream error");
79             return RC_ERROR;
80         }
81     } else {
82         rc = mainDemo->StartVideoStream();
83         if (rc != RC_OK) {
84             CAMERA_LOGE("main test: PreviewOn StartVideoStream error");
85             return RC_ERROR;
86         }
87     }
88 
89     rc = mainDemo->CaptureON(STREAM_ID_PREVIEW, CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
90     if (rc != RC_OK) {
91         CAMERA_LOGE("main test: PreviewOn mainDemo->CaptureON() preview error");
92         return RC_ERROR;
93     }
94 
95     CAMERA_LOGD("main test: PreviewOn exit");
96     return RC_OK;
97 }
98 
PreviewOff(const std::shared_ptr<OhosCameraDemo> & mainDemo)99 static void PreviewOff(const std::shared_ptr<OhosCameraDemo>& mainDemo)
100 {
101     CAMERA_LOGD("main test: PreviewOff enter");
102 
103     mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
104     mainDemo->ReleaseAllStream();
105 
106     CAMERA_LOGD("main test: PreviewOff exit");
107 }
108 
CaptureTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)109 static void CaptureTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
110 {
111     RetCode rc = RC_OK;
112     constexpr size_t delayTime = 5;
113 
114     rc = mainDemo->CaptureON(STREAM_ID_CAPTURE, CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
115     if (rc != RC_OK) {
116         CAMERA_LOGE("main test: mainDemo->CaptureON() capture error");
117         return;
118     }
119 
120     sleep(delayTime);
121     rc = mainDemo->CaptureOff(CAPTURE_ID_CAPTURE, CAPTURE_SNAPSHOT);
122     if (rc != RC_OK) {
123         CAMERA_LOGE("main test: mainDemo->CaptureOff() capture error");
124         return;
125     }
126 }
127 
VideoTest(const std::shared_ptr<OhosCameraDemo> & mainDemo)128 static void VideoTest(const std::shared_ptr<OhosCameraDemo>& mainDemo)
129 {
130     RetCode rc = RC_OK;
131     constexpr size_t delayTime = 5;
132 
133     PreviewOff(mainDemo);
134     mainDemo->StartDualStreams(STREAM_ID_VIDEO);
135     mainDemo->CaptureOnDualStreams(STREAM_ID_VIDEO);
136 
137     sleep(delayTime);
138     mainDemo->CaptureOff(CAPTURE_ID_PREVIEW, CAPTURE_PREVIEW);
139     mainDemo->CaptureOff(CAPTURE_ID_VIDEO, CAPTURE_VIDEO);
140     mainDemo->ReleaseAllStream();
141 
142     rc = PreviewOn(0, mainDemo);
143     if (rc != RC_OK) {
144         CAMERA_LOGE("main test: video PreviewOn() error please -q exit demo");
145     }
146 }
147 
ManuList(const std::shared_ptr<OhosCameraDemo> & mainDemo,const int argc,char ** argv)148 static void ManuList(const std::shared_ptr<OhosCameraDemo>& mainDemo,
149     const int argc, char** argv)
150 {
151     int idx;
152     int c;
153     const char *shortOptions = "h:cewgi:";
154     c = getopt_long(argc, argv, shortOptions, LONG_OPTIONS, &idx);
155     while (1) {
156         switch (c) {
157             case 'h':
158                 c = PutMenuAndGetChr();
159                 break;
160             case 'c':
161                 CaptureTest(mainDemo);
162                 c = PutMenuAndGetChr();
163                 break;
164             case 'e':
165                 mainDemo->SetAeAuto();
166                 c = PutMenuAndGetChr();
167                 break;
168             case 'f':
169                 mainDemo->SetAfAuto();
170                 c = PutMenuAndGetChr();
171                 break;
172             case 'w':
173                 mainDemo->SetAwbMode();
174                 c = PutMenuAndGetChr();
175                 break;
176             case 'g':
177                 mainDemo->SetAELock();
178                 c = PutMenuAndGetChr();
179                 break;
180             case 'i':
181                 mainDemo->SetAWBLock();
182                 c = PutMenuAndGetChr();
183                 break;
184             case 'v':
185                 VideoTest(mainDemo);
186                 c = PutMenuAndGetChr();
187                 break;
188             case 'q':
189                 PreviewOff(mainDemo);
190                 mainDemo->QuitDemo();
191                 return;
192             default:
193                 CAMERA_LOGE("main test: command error please retry input command");
194                 c = PutMenuAndGetChr();
195                 break;
196         }
197     }
198 }
199 
main(int argc,char ** argv)200 int main(int argc, char** argv)
201 {
202     RetCode rc = RC_OK;
203 
204     auto mainDemo = std::make_shared<OhosCameraDemo>();
205     rc = mainDemo->InitSensors();
206     if (rc == RC_ERROR) {
207         CAMERA_LOGE("main test: mainDemo->InitSensors() error");
208         return -1;
209     }
210     rc = mainDemo->InitCameraDevice();
211     if (rc == RC_ERROR) {
212         CAMERA_LOGE("main test: mainDemo->InitCameraDevice() error");
213         return -1;
214     }
215     mainDemo->SetEnableResult();
216 
217     rc = PreviewOn(0, mainDemo);
218     if (rc != RC_OK) {
219         CAMERA_LOGE("main test: PreviewOn() error demo exit");
220         return -1;
221     }
222 
223     ManuList(mainDemo, argc, argv);
224 
225     return RC_OK;
226 }
227 } // namespace Camera
228 
main(int argc,char ** argv)229 int main(int argc, char** argv)
230 {
231     OHOS::Camera::main(argc, argv);
232 
233     return 0;
234 }
235