1 /*
2 * Copyright (c) 2023-2023 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 <unistd.h>
17 #include <iostream>
18 #include <string>
19 #include "input/camera_input.h"
20 #include "input/camera_manager.h"
21 #include "output/camera_output_capability.h"
22 #include "portrait_session.h"
23 #include "camera_log.h"
24 #include "surface.h"
25 #include "test_common.h"
26
27 #include "ipc_skeleton.h"
28 #include "access_token.h"
29 #include "hap_token_info.h"
30 #include "accesstoken_kit.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::CameraStandard;
36
ConfigPhotoCaptureSetting()37 static std::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
38 {
39 std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
40 // QualityLevel
41 PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
42 photoCaptureSettings->SetQuality(quality);
43 return photoCaptureSettings;
44 }
45
46 std::map<PortraitEffect, camera_portrait_effect_type_t> g_fwToMetaPortraitEffect_ = {
47 {OFF_EFFECT, OHOS_CAMERA_PORTRAIT_EFFECT_OFF},
48 {CIRCLES, OHOS_CAMERA_PORTRAIT_CIRCLES}
49 };
50
51 std::map<FilterType, camera_filter_type_t> g_fwToMetaFilterType_ = {
52 {NONE, OHOS_CAMERA_FILTER_TYPE_OFF},
53 {CLASSIC, OHOS_CAMERA_FILTER_TYPE_CLASSIC},
54 {DAWN, OHOS_CAMERA_FILTER_TYPE_DAWN},
55 {PURE, OHOS_CAMERA_FILTER_TYPE_PURE},
56 {GREY, OHOS_CAMERA_FILTER_TYPE_GREY},
57 {NATURAL, OHOS_CAMERA_FILTER_TYPE_NATURAL},
58 {MORI, OHOS_CAMERA_FILTER_TYPE_MORI},
59 {FAIR, OHOS_CAMERA_FILTER_TYPE_FAIR},
60 {PINK, OHOS_CAMERA_FILTER_TYPE_PINK}
61 };
62
63 std::map<camera_beauty_type_t, BeautyType> g_metaToFwBeautyType_ = {
64 {OHOS_CAMERA_BEAUTY_TYPE_AUTO, AUTO_TYPE},
65 {OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH, SKIN_SMOOTH},
66 {OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER, FACE_SLENDER},
67 {OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE, SKIN_TONE}
68 };
69
70 std::map<BeautyType, camera_beauty_type_t> g_fwToMetaBeautyType_ = {
71 {AUTO_TYPE, OHOS_CAMERA_BEAUTY_TYPE_AUTO},
72 {SKIN_SMOOTH, OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH},
73 {FACE_SLENDER, OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER},
74 {SKIN_TONE, OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE}
75 };
76
77 std::map<BeautyType, camera_device_metadata_tag_t> g_beautyTypeAbility_ = {
78 {SKIN_SMOOTH, OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES},
79 {FACE_SLENDER, OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES},
80 {SKIN_TONE, OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES}
81 };
82
83 std::map<BeautyType, std::string> g_beautyTypeStr_ = {
84 {AUTO_TYPE, "OHOS_CAMERA_BEAUTY_TYPE_AUTO"},
85 {SKIN_SMOOTH, "OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH"},
86 {FACE_SLENDER, "OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER"},
87 {SKIN_TONE, "OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE"}
88 };
89
90 std::map<BeautyType, std::string> g_beautyAbilityStr_ = {
91 {SKIN_SMOOTH, "OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES"},
92 {FACE_SLENDER, "OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES"},
93 {SKIN_TONE, "OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES"}
94 };
95
96 std::map<FilterType, std::string> g_filterTypeStr_ = {
97 {NONE, "OHOS_CAMERA_FILTER_TYPE_OFF"},
98 {CLASSIC, "OHOS_CAMERA_FILTER_TYPE_CLASSIC"},
99 {DAWN, "OHOS_CAMERA_FILTER_TYPE_DAWN"},
100 {PURE, "OHOS_CAMERA_FILTER_TYPE_PURE"},
101 {GREY, "OHOS_CAMERA_FILTER_TYPE_GREY"},
102 {NATURAL, "OHOS_CAMERA_FILTER_TYPE_NATURAL"},
103 {MORI, "OHOS_CAMERA_FILTER_TYPE_MORI"},
104 {FAIR, "OHOS_CAMERA_FILTER_TYPE_FAIR"},
105 {PINK, "OHOS_CAMERA_FILTER_TYPE_PINK"}
106 };
107
108 std::map<PortraitEffect, std::string> g_portraitEffectStr_ = {
109 {OFF_EFFECT, "OHOS_CAMERA_PORTRAIT_EFFECT_OFF"},
110 {CIRCLES, "OHOS_CAMERA_PORTRAIT_CIRCLES"}
111 };
112
113 std::map<uint32_t, std::string> g_abilityIdStr_ = {
114 {536870912, "OHOS_ABILITY_SCENE_FILTER_TYPES"},
115 {536870914, "OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES"},
116 {536870916, "OHOS_ABILITY_SCENE_BEAUTY_TYPES"}
117 };
118
PrintSupportBeautyTypes(vector<BeautyType> supportedBeautyTypes)119 void PrintSupportBeautyTypes(vector<BeautyType> supportedBeautyTypes)
120 {
121 MEDIA_INFO_LOG("Supported BeautyType size = %{public}zu", supportedBeautyTypes.size());
122 cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_BEAUTY_TYPES] << " : size = " << supportedBeautyTypes.size() <<endl;
123 uint32_t index = 0;
124 for (BeautyType type : supportedBeautyTypes) {
125 for (auto itrType = g_fwToMetaBeautyType_.cbegin(); itrType != g_fwToMetaBeautyType_.cend(); itrType++) {
126 if (type == itrType->first) {
127 break;
128 }
129 }
130 cout << "[" << index++ << "]" << "type = " << g_beautyTypeStr_[type] << ", ";
131 MEDIA_INFO_LOG("Supported BeautyType type = %{public}s", g_beautyTypeStr_[type].c_str());
132 }
133 cout << endl;
134 cout << "------------------------------------------------------------------------------------------------"<<endl;
135 return;
136 }
137
PrintSupportFilterTypes(vector<FilterType> supportedFilterTypes)138 void PrintSupportFilterTypes(vector<FilterType> supportedFilterTypes)
139 {
140 MEDIA_INFO_LOG("Supported FilterTypes size = %{public}zu", supportedFilterTypes.size());
141 cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_FILTER_TYPES] <<" : size = " << supportedFilterTypes.size() << endl;
142 uint32_t index = 0;
143 for (FilterType type : supportedFilterTypes) {
144 for (auto itrType = g_fwToMetaFilterType_.cbegin(); itrType != g_fwToMetaFilterType_.cend(); itrType++) {
145 if (type == itrType->first) {
146 break;
147 }
148 }
149 cout << " [" << index++ << "]" << "type = " << g_filterTypeStr_[type] << ", ";
150 MEDIA_INFO_LOG("Supported FilterType type = %{public}s", g_filterTypeStr_[type].c_str());
151 }
152 cout << endl;
153 cout << "------------------------------------------------------------------------------------------------"<<endl;
154 return;
155 }
156
PrintSupportPortraitEffects(vector<PortraitEffect> supportedPortraitEffects)157 void PrintSupportPortraitEffects(vector<PortraitEffect> supportedPortraitEffects)
158 {
159 MEDIA_INFO_LOG("Supported PortraitEffects size = %{public}zu", supportedPortraitEffects.size());
160 cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES] <<" : size = "
161 << supportedPortraitEffects.size() <<endl;
162 uint32_t index = 0;
163 for (PortraitEffect type : supportedPortraitEffects) {
164 for (auto itrType = g_fwToMetaPortraitEffect_.cbegin(); itrType != g_fwToMetaPortraitEffect_.cend();
165 itrType++) {
166 if (type == itrType->first) {
167 break;
168 }
169 }
170 cout << "[" << index++ << "]" << "effect = " << g_portraitEffectStr_[type] << ", ";
171 MEDIA_INFO_LOG("Supported PortraitEffect effect = %{public}s", g_portraitEffectStr_[type].c_str());
172 }
173 cout << endl;
174 cout << "------------------------------------------------------------------------------------------------"<<endl;
175 return;
176 }
177
PrintSupportBeautyRange(BeautyType beauty,vector<int32_t> range)178 void PrintSupportBeautyRange(BeautyType beauty, vector<int32_t> range)
179 {
180 MEDIA_INFO_LOG("%{public}s size = %{public}zu", g_beautyAbilityStr_[beauty].c_str(), range.size());
181 cout << " " << g_beautyAbilityStr_[beauty] <<" size = " << range.size();
182 for (auto val : range) {
183 cout << " val = " << val << ", ";
184 MEDIA_INFO_LOG("val = %{public}d", val);
185 }
186 cout << endl;
187 return;
188 }
189
GetSupportedPortraitEffectsStub()190 std::vector<PortraitEffect> GetSupportedPortraitEffectsStub()
191 {
192 cout<<"=================GetSupportedPortraitEffectsStub call================="<<endl;
193 std::vector<PortraitEffect> supportedPortraitEffects = {OFF_EFFECT, CIRCLES};
194 return supportedPortraitEffects;
195 }
196
GetSupportedBeautyTypesStub()197 std::vector<BeautyType> GetSupportedBeautyTypesStub()
198 {
199 cout<<"=================GetSupportedBeautyTypesStub call================="<<endl;
200 std::vector<BeautyType> supportedBeautyTypes = {AUTO_TYPE, SKIN_SMOOTH, FACE_SLENDER, SKIN_TONE};
201 return supportedBeautyTypes;
202 }
203
GetSupportedBeautyRangeStub(void * obj,BeautyType beautyType)204 std::vector<int32_t> GetSupportedBeautyRangeStub(void* obj, BeautyType beautyType)
205 {
206 cout<<"=================GetSupportedBeautyRangeStub call================="<<endl;
207 vector<int32_t> beautyRange;
208 switch (beautyType) {
209 case BeautyType::AUTO_TYPE : {
210 std::vector<int32_t> values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
211 beautyRange = values;
212 break;
213 }
214 case BeautyType::SKIN_TONE : {
215 std::vector<int32_t> values = {0xFFFFFFFF, 0xBF986C, 0xE9BB97, 0xEDCDA3, 0xF7D7B3, 0xFEE6CF};
216 beautyRange = values;
217 break;
218 }
219 case BeautyType::SKIN_SMOOTH : {
220 std::vector<int32_t> values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
221 beautyRange = values;
222 break;
223 }
224 case BeautyType::FACE_SLENDER : {
225 std::vector<int32_t> values = {0, 1, 2, 3, 4, 5};
226 beautyRange = values;
227 break;
228 }
229 default : {
230 std::vector<int32_t> values = {};
231 beautyRange = values;
232 }
233 }
234 cout<<"BeautyType = " << beautyType << " BeautyType size()" << beautyRange.size() <<endl;
235 return beautyRange;
236 }
237
GetExposureRangeStub(std::vector<uint32_t> & exposureRange)238 int32_t GetExposureRangeStub(std::vector<uint32_t> &exposureRange)
239 {
240 cout<<"=================GetSupportedBeautyRangeStub call================="<<endl;
241 exposureRange.clear();
242 std::vector<uint32_t> exposureRangeStub = {250, 500, 1000, 2000, 4000, 8000,
243 12000, 16000, 20000, 24000, 28000, 32000};
244 for (size_t i = 0; i < exposureRangeStub.size(); i++) {
245 exposureRange.push_back(exposureRangeStub.at(i));
246 }
247 cout << " exposureRangeStub size()" << exposureRangeStub.size() <<endl;
248 return 0;
249 }
250
GetSupportedFiltersStub()251 std::vector<FilterType> GetSupportedFiltersStub()
252 {
253 cout<<"=================GetSupportedFiltersStub call================="<<endl;
254 std::vector<FilterType> supportedFilterTypes = { NONE, CLASSIC, DAWN, PURE, GREY, NATURAL, MORI, FAIR, PINK };
255 return supportedFilterTypes;
256 }
257
main(int argc,char ** argv)258 int main(int argc, char **argv)
259 {
260 cout<<"-----------------version:20230822-----------------"<<endl;
261 const int32_t previewWidthIndex = 1;
262 const int32_t previewHeightIndex = 2;
263 const int32_t photoWidthIndex = 3;
264 const int32_t photoHeightIndex = 4;
265 const int32_t devicePosionIndex = 5;
266 const int32_t validArgCount = 6;
267 const int32_t gapAfterCapture = 3; // 1 second
268 const int32_t previewCaptureGap = 5; // 5 seconds
269 const char* testName = "camera_capture_mode";
270 int32_t ret = -1;
271 int32_t previewFd = -1;
272 int32_t photoFd = -1;
273 int32_t previewFormat = CAMERA_FORMAT_YUV_420_SP;
274 int32_t previewWidth = 1280;
275 int32_t previewHeight = 960;
276 int32_t photoFormat = CAMERA_FORMAT_JPEG;
277 int32_t photoWidth = 1280;
278 int32_t photoHeight = 960;
279 int32_t photoCaptureCount = 1;
280 int32_t devicePosion = 0;
281 bool isResolutionConfigured = false;
282 Size previewsize;
283 Size photosize;
284
285 // Update sizes if enough number of valid arguments are passed
286 if (argc == validArgCount) {
287 // Validate arguments
288 for (int counter = 1; counter < argc; counter++) {
289 if (!TestUtils::IsNumber(argv[counter])) {
290 cout << "Invalid argument: " << argv[counter] << endl;
291 cout << "Retry by giving proper sizes" << endl;
292 return 0;
293 }
294 }
295 previewWidth = atoi(argv[previewWidthIndex]);
296 previewHeight = atoi(argv[previewHeightIndex]);
297 photoWidth = atoi(argv[photoWidthIndex]);
298 photoHeight = atoi(argv[photoHeightIndex]);
299 devicePosion = atoi(argv[devicePosionIndex]);
300 isResolutionConfigured = true;
301 } else if (argc != 1) {
302 cout << "Pass " << (validArgCount - 1) << "arguments" << endl;
303 cout << "PreviewHeight, PreviewWidth, PhotoWidth, PhotoHeight"
304 << endl;
305 return 0;
306 }
307
308 uint64_t tokenId;
309 const char *perms[0];
310 perms[0] = "ohos.permission.CAMERA";
311 NativeTokenInfoParams infoInstance = {
312 .dcapsNum = 0,
313 .permsNum = 1,
314 .aclsNum = 0,
315 .dcaps = NULL,
316 .perms = perms,
317 .acls = NULL,
318 .processName = "camera_capture_mode",
319 .aplStr = "system_basic",
320 };
321 tokenId = GetAccessTokenId(&infoInstance);
322 SetSelfTokenID(tokenId);
323 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
324
325 sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
326 MEDIA_INFO_LOG("Setting callback to listen camera status and flash status");
327 camManagerObj->RegisterCameraStatusCallback(std::make_shared<TestCameraMngerCallback>(testName));
328 std::vector<sptr<CameraDevice>> cameraObjList = camManagerObj->GetSupportedCameras();
329 CHECK_ERROR_RETURN_RET(cameraObjList.size() == 0, 0);
330 sptr<CameraDevice> device = cameraObjList[0];
331 MEDIA_INFO_LOG("Camera ID count: %{public}zu", cameraObjList.size());
332 for (auto& it : cameraObjList) {
333 MEDIA_INFO_LOG("Camera ID: %{public}s", it->GetID().c_str());
334 MEDIA_INFO_LOG("Camera Posion: %{public}d", it->GetPosition());
335 if (devicePosion == 0 && it->GetPosition() == CameraPosition::CAMERA_POSITION_BACK) {
336 device = it;
337 break;
338 }
339 if (devicePosion == 1 && it->GetPosition() == CameraPosition::CAMERA_POSITION_FRONT) {
340 device = it;
341 break;
342 }
343 }
344 cout<<"Camera ID ="<<device->GetID()<<",camera Position = "<<device->GetPosition()<<endl;
345 sptr<CameraManager> modeManagerObj = CameraManager::GetInstance();
346 std::vector<SceneMode> supportedModes = modeManagerObj->GetSupportedModes(device);
347 std::string modes = "";
348 for (auto mode : supportedModes) {
349 modes += std::to_string(static_cast<uint32_t>(mode)) + " , ";
350 }
351 MEDIA_INFO_LOG("supportedModes : %{public}s", modes.c_str());
352 sptr<CaptureSession> captureSession = modeManagerObj->CreateCaptureSession(SceneMode::PORTRAIT);
353 sptr<PortraitSession> portraitSession = nullptr;
354 portraitSession = static_cast<PortraitSession *> (captureSession.GetRefPtr());
355 CHECK_ERROR_RETURN_RET(portraitSession == nullptr, 0);
356 portraitSession->BeginConfig();
357 sptr<CaptureInput> captureInput = camManagerObj->CreateCameraInput(device);
358 CHECK_ERROR_RETURN_RET(captureInput == nullptr, 0);
359
360 sptr<CameraInput> cameraInput = (sptr<CameraInput> &)captureInput;
361 cameraInput->Open();
362 std::vector<Profile> photoProfiles = {};
363 std::vector<Profile> previewProfiles = {};
364 std::string abilityIds = "";
365 if (isResolutionConfigured) {
366 std::vector<CameraFormat> previewFormats;
367 std::vector<CameraFormat> photoFormats;
368 std::vector<Size> previewSizes;
369 std::vector<Size> photoSizes;
370 sptr<CameraOutputCapability> outputcapability =
371 modeManagerObj->GetSupportedOutputCapability(device, SceneMode::PORTRAIT);
372 previewProfiles = outputcapability->GetPreviewProfiles();
373 uint32_t profileIndex = 0;
374 for (auto i : previewProfiles) {
375 previewFormats.push_back(i.GetCameraFormat());
376 previewSizes.push_back(i.GetSize());
377
378 abilityIds = "";
379 for (auto id : i.GetAbilityId()) {
380 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
381 }
382 MEDIA_INFO_LOG("index(%{public}d), preview profile f(%{public}d), w(%{public}d), h(%{public}d) "
383 "support ability: %{public}s", profileIndex++,
384 i.GetCameraFormat(), i.GetSize().width, i.GetSize().height, abilityIds.c_str());
385 }
386
387 photoProfiles = outputcapability->GetPhotoProfiles();
388 profileIndex = 0;
389 for (auto i : photoProfiles) {
390 photoFormats.push_back(i.GetCameraFormat());
391 photoSizes.push_back(i.GetSize());
392 abilityIds = "";
393 for (auto id : i.GetAbilityId()) {
394 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
395 }
396 MEDIA_INFO_LOG("index %{public}d, photo support format : %{public}d, width: %{public}d, height: %{public}d"
397 "support ability: %{public}s", profileIndex++,
398 i.GetCameraFormat(), i.GetSize().width, i.GetSize().height, abilityIds.c_str());
399 }
400 }
401
402 MEDIA_INFO_LOG("photoCaptureCount: %{public}d", photoCaptureCount);
403
404 cameraInput->SetErrorCallback(std::make_shared<TestDeviceCallback>(testName));
405 ret = portraitSession->AddInput(captureInput);
406 CHECK_ERROR_RETURN_RET(ret != 0, 0);
407
408 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
409 CHECK_ERROR_RETURN_RET(photoSurface == nullptr, 0);
410 photosize.width = photoWidth;
411 photosize.height = photoHeight;
412 Profile photoprofile;
413 for (auto it : photoProfiles) {
414 if (it.GetSize().width == photosize.width && it.GetSize().height == photosize.height
415 && it.GetCameraFormat() == static_cast<CameraFormat>(photoFormat)) {
416 photoprofile = it;
417 }
418 }
419 abilityIds = "";
420 for (auto id : photoprofile.GetAbilityId()) {
421 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
422 }
423 MEDIA_INFO_LOG("photo support format : %{public}d, width: %{public}d, height: %{public}d"
424 "support ability: %{public}s",
425 photoFormat, photoWidth, photoHeight, abilityIds.c_str());
426 cout<< "photoFormat: " << photoFormat << " photoWidth: "<< photoWidth
427 << " photoHeight: " << photoHeight << " support ability: " << abilityIds.c_str() << endl;
428
429 sptr<SurfaceListener> captureListener = new(std::nothrow) SurfaceListener("Photo", SurfaceType::PHOTO,
430 photoFd, photoSurface);
431 CHECK_ERROR_RETURN_RET(captureListener == nullptr, 0);
432 photoSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)captureListener);
433 sptr<IBufferProducer> bp = photoSurface->GetProducer();
434 sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoprofile, bp);
435 CHECK_ERROR_RETURN_RET(photoOutput == nullptr, 0);
436
437 MEDIA_INFO_LOG("Setting photo callback");
438 ((sptr<PhotoOutput> &)photoOutput)->SetCallback(std::make_shared<TestPhotoOutputCallback>(testName));
439 ret = portraitSession->AddOutput(photoOutput);
440 CHECK_ERROR_RETURN_RET(ret != 0, 0);
441
442 sptr<CaptureOutput> metaOutput = camManagerObj->CreateMetadataOutput();
443 MEDIA_INFO_LOG("Setting Meta callback");
444 ((sptr<MetadataOutput> &)metaOutput)->SetCallback(std::make_shared<TestMetadataOutputObjectCallback>(testName));
445
446 ret = portraitSession->AddOutput(metaOutput);
447 CHECK_ERROR_RETURN_RET(ret != 0, 0);
448
449 sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
450 CHECK_ERROR_RETURN_RET(previewSurface == nullptr, 0);
451 previewsize.width = previewWidth;
452 previewsize.height = previewHeight;
453 Profile previewprofile;
454 for (auto it : previewProfiles) {
455 if (it.GetSize().width == previewsize.width && it.GetSize().height == previewsize.height
456 && it.GetCameraFormat() == static_cast<CameraFormat>(previewFormat)) {
457 previewprofile = it;
458 }
459 }
460 abilityIds = "";
461 for (auto id : previewprofile.GetAbilityId()) {
462 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
463 }
464 MEDIA_INFO_LOG("previewFormat: %{public}d, previewWidth: %{public}d, previewHeight: %{public}d"
465 "support ability: %{public}s",
466 previewFormat, previewWidth, previewHeight, abilityIds.c_str());
467
468 cout<< "previewFormat: " << previewFormat << " previewWidth: "<< previewWidth
469 << " previewHeight: " << previewHeight << " support ability: " << abilityIds.c_str() <<endl;
470
471 cout << "------------------------------------------------------------------------------------------------"<<endl;
472 sptr<SurfaceListener> listener = new(std::nothrow) SurfaceListener("Preview", SurfaceType::PREVIEW,
473 previewFd, previewSurface);
474 CHECK_ERROR_RETURN_RET(listener == nullptr, 0);
475 previewSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)listener);
476 sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
477 sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
478 sptr<CaptureOutput> previewOutput = camManagerObj->CreatePreviewOutput(previewprofile, previewProducerSurface);
479 CHECK_ERROR_RETURN_RET(previewOutput == nullptr, 0);
480
481 MEDIA_INFO_LOG("Setting preview callback");
482 ((sptr<PreviewOutput> &)previewOutput)->SetCallback(std::make_shared<TestPreviewOutputCallback>(testName));
483 ret = portraitSession->AddOutput(previewOutput);
484 CHECK_ERROR_RETURN_RET(ret != 0, 0);
485
486 ret = portraitSession->CommitConfig();
487 CHECK_ERROR_RETURN_RET(ret != 0, 0);
488
489 MEDIA_INFO_LOG("Preview started");
490 ret = portraitSession->Start();
491 CHECK_ERROR_RETURN_RET(ret != 0, 0);
492
493 std::vector<float> vecZoomRatioList;
494 portraitSession->GetZoomRatioRange(vecZoomRatioList);
495 cout<< "getMode:" << portraitSession->GetFeaturesMode().Dump() << ",minZoom:" << vecZoomRatioList[0]
496 << ", maxZoom:" << vecZoomRatioList[1]<<endl;
497 std::vector<BeautyType> supportedBeautyType = portraitSession->GetSupportedBeautyTypes();
498 PrintSupportBeautyTypes(supportedBeautyType);
499
500 std::vector<int32_t> faceSlenderRange = portraitSession->GetSupportedBeautyRange(BeautyType::FACE_SLENDER);
501 PrintSupportBeautyRange(BeautyType::FACE_SLENDER, faceSlenderRange);
502
503 std::vector<int32_t> skinSmoothRange = portraitSession->GetSupportedBeautyRange(BeautyType::SKIN_SMOOTH);
504 PrintSupportBeautyRange(BeautyType::SKIN_SMOOTH, skinSmoothRange);
505
506 std::vector<int32_t> skinToneRange = portraitSession->GetSupportedBeautyRange(BeautyType::SKIN_TONE);
507 PrintSupportBeautyRange(BeautyType::SKIN_TONE, skinToneRange);
508
509 vector<FilterType> supportedFilterTypes = portraitSession->GetSupportedFilters();
510 PrintSupportFilterTypes(supportedFilterTypes);
511
512 vector<PortraitEffect> supportedPortraitEffects = portraitSession->GetSupportedPortraitEffects();
513 PrintSupportPortraitEffects(supportedPortraitEffects);
514
515 sleep(previewCaptureGap);
516
517 uint32_t photoCnt = 1;
518
519 MEDIA_INFO_LOG("Normal Capture started");
520 for (int i = 1; i <= photoCaptureCount; i++) {
521 MEDIA_INFO_LOG("Photo capture %{public}d started", i);
522 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
523 if (ret != 0) {
524 MEDIA_INFO_LOG("Photo capture err ret=%{public}d", ret);
525 return 0;
526 }
527 sleep(gapAfterCapture);
528 }
529
530 cout<< "Filter Capture started" <<endl;
531 for (auto filter : supportedFilterTypes) {
532 MEDIA_INFO_LOG("Set Filter: %{public}s", g_filterTypeStr_[filter].c_str());
533 portraitSession->LockForControl();
534 portraitSession->SetFilter(filter);
535 portraitSession->UnlockForControl();
536 sleep(gapAfterCapture);
537 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
538 CHECK_ERROR_RETURN_RET(ret != 0, 0);
539 MEDIA_INFO_LOG("Photo capture cnt [%{public}d] filter : %{public}s",
540 photoCnt++, g_filterTypeStr_[filter].c_str());
541 sleep(gapAfterCapture);
542 }
543 cout<< "Filter Capture started" <<endl;
544
545 cout<< "BeautyType::SKIN_TONE Capture started" <<endl;
546 for (auto beautyVal : skinToneRange) {
547 MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
548 g_beautyTypeStr_[BeautyType::SKIN_TONE].c_str(), beautyVal);
549 portraitSession->LockForControl();
550 portraitSession->SetBeauty(BeautyType::SKIN_TONE, beautyVal);
551 portraitSession->UnlockForControl();
552 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
553 CHECK_ERROR_RETURN_RET(ret != 0, 0);
554 sleep(gapAfterCapture);
555 }
556 cout<< "BeautyType::SKIN_TONE Capture end" <<endl;
557
558 cout<< "BeautyType::FACE_SLENDER Capture started" <<endl;
559 for (auto beautyVal : skinToneRange) {
560 MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
561 g_beautyTypeStr_[BeautyType::FACE_SLENDER].c_str(), beautyVal);
562 portraitSession->LockForControl();
563 portraitSession->SetBeauty(BeautyType::FACE_SLENDER, beautyVal);
564 portraitSession->UnlockForControl();
565 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
566 CHECK_ERROR_RETURN_RET(ret != 0, 0);
567 sleep(gapAfterCapture);
568 }
569 cout<< "BeautyType::FACE_SLENDER Capture end" <<endl;
570
571 cout<< "BeautyType::SKIN_SMOOTH Capture started" <<endl;
572 for (auto beautyVal : skinToneRange) {
573 MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
574 g_beautyTypeStr_[BeautyType::SKIN_SMOOTH].c_str(), beautyVal);
575 portraitSession->LockForControl();
576 portraitSession->SetBeauty(BeautyType::SKIN_SMOOTH, beautyVal);
577 portraitSession->UnlockForControl();
578 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
579 CHECK_ERROR_RETURN_RET(ret != 0, 0);
580 sleep(gapAfterCapture);
581 }
582 cout<< "BeautyType::SKIN_SMOOTH Capture end" <<endl;
583
584 cout<< "PortraitEffect Capture started" <<endl;
585 for (auto effect : supportedPortraitEffects) {
586 MEDIA_INFO_LOG("Set PortraitEffect: %{public}s", g_portraitEffectStr_[effect].c_str());
587 portraitSession->LockForControl();
588 portraitSession->SetPortraitEffect(effect);
589 portraitSession->UnlockForControl();
590 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
591 CHECK_ERROR_RETURN_RET(ret != 0, 0);
592 MEDIA_INFO_LOG("Photo capture [%{public}d] filter : %{public}s",
593 photoCnt++, g_portraitEffectStr_[effect].c_str());
594 sleep(gapAfterCapture);
595 }
596 cout<< "PortraitEffect Capture end" <<endl;
597 MEDIA_INFO_LOG("Closing the session");
598 ((sptr<PreviewOutput> &)previewOutput)->Stop();
599 portraitSession->Stop();
600 portraitSession->Release();
601 cameraInput->Release();
602 camManagerObj->RegisterCameraStatusCallback(nullptr);
603
604 MEDIA_INFO_LOG("Camera new sample end.");
605 return 0;
606 }
607