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 20namespace OHOS::Camera { 21static 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 36const 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 44static 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 64static 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 99static 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 109static 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 128static 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 148static void ManuList(const std::shared_ptr<OhosCameraDemo>& mainDemo, 149 const int argc, char** argv) 150{ 151 int idx, c; 152 const char *shortOptions = "h:cewgi:"; 153 c = getopt_long(argc, argv, shortOptions, LONG_OPTIONS, &idx); 154 while (1) { 155 switch (c) { 156 case 'h': 157 c = PutMenuAndGetChr(); 158 break; 159 case 'c': 160 CaptureTest(mainDemo); 161 c = PutMenuAndGetChr(); 162 break; 163 case 'e': 164 mainDemo->SetAeAuto(); 165 c = PutMenuAndGetChr(); 166 break; 167 case 'f': 168 mainDemo->SetAfAuto(); 169 c = PutMenuAndGetChr(); 170 break; 171 case 'w': 172 mainDemo->SetAwbMode(); 173 c = PutMenuAndGetChr(); 174 break; 175 case 'g': 176 mainDemo->SetAELock(); 177 c = PutMenuAndGetChr(); 178 break; 179 case 'i': 180 mainDemo->SetAWBLock(); 181 c = PutMenuAndGetChr(); 182 break; 183 case 'v': 184 VideoTest(mainDemo); 185 c = PutMenuAndGetChr(); 186 break; 187 case 'q': 188 PreviewOff(mainDemo); 189 mainDemo->QuitDemo(); 190 return; 191 default: 192 CAMERA_LOGE("main test: command error please retry input command"); 193 c = PutMenuAndGetChr(); 194 break; 195 } 196 } 197} 198 199int main(int argc, char** argv) 200{ 201 RetCode rc = RC_OK; 202 203 auto mainDemo = std::make_shared<OhosCameraDemo>(); 204 rc = mainDemo->InitSensors(); 205 if (rc == RC_ERROR) { 206 CAMERA_LOGE("main test: mainDemo->InitSensors() error"); 207 return -1; 208 } 209 rc = mainDemo->InitCameraDevice(); 210 if (rc == RC_ERROR) { 211 CAMERA_LOGE("main test: mainDemo->InitCameraDevice() error"); 212 return -1; 213 } 214 mainDemo->SetEnableResult(); 215 216 rc = PreviewOn(0, mainDemo); 217 if (rc != RC_OK) { 218 CAMERA_LOGE("main test: PreviewOn() error demo exit"); 219 return -1; 220 } 221 222 ManuList(mainDemo, argc, argv); 223 224 return RC_OK; 225} 226} // namespace Camera 227 228int main(int argc, char** argv) 229{ 230 OHOS::Camera::main(argc, argv); 231 232 return 0; 233} 234