11c1b0f19Sopenharmony_ci/*
21c1b0f19Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
31c1b0f19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41c1b0f19Sopenharmony_ci * you may not use this file except in compliance with the License.
51c1b0f19Sopenharmony_ci * You may obtain a copy of the License at
61c1b0f19Sopenharmony_ci *
71c1b0f19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81c1b0f19Sopenharmony_ci *
91c1b0f19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101c1b0f19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111c1b0f19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121c1b0f19Sopenharmony_ci * See the License for the specific language governing permissions and
131c1b0f19Sopenharmony_ci * limitations under the License.
141c1b0f19Sopenharmony_ci */
151c1b0f19Sopenharmony_ci
161c1b0f19Sopenharmony_ci#include "dcamera_client_demo.h"
171c1b0f19Sopenharmony_ci
181c1b0f19Sopenharmony_ciusing namespace OHOS;
191c1b0f19Sopenharmony_ciusing namespace OHOS::Camera;
201c1b0f19Sopenharmony_ciusing namespace OHOS::CameraStandard;
211c1b0f19Sopenharmony_ciusing namespace OHOS::DistributedHardware;
221c1b0f19Sopenharmony_ci
231c1b0f19Sopenharmony_cinamespace OHOS {
241c1b0f19Sopenharmony_cinamespace DistributedHardware {
251c1b0f19Sopenharmony_ciconstexpr double LOCATION_LATITUDE = 22.306;
261c1b0f19Sopenharmony_ciconstexpr double LOCATION_LONGITUDE = 52.12;
271c1b0f19Sopenharmony_ciconstexpr double LOCATION_ALTITUDE = 2.365;
281c1b0f19Sopenharmony_ci#ifdef DCAMERA_COMMON
291c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_WIDTH = 1280;
301c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_HEIGTH = 960;
311c1b0f19Sopenharmony_ci#else
321c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_WIDTH = 1920;
331c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_HEIGTH = 1080;
341c1b0f19Sopenharmony_ci#endif
351c1b0f19Sopenharmony_ciconstexpr int32_t PREVIEW_WIDTH = 640;
361c1b0f19Sopenharmony_ciconstexpr int32_t PREVIEW_HEIGTH = 480;
371c1b0f19Sopenharmony_ciconstexpr int32_t VIDEO_WIDTH = 640;
381c1b0f19Sopenharmony_ciconstexpr int32_t VIDEO_HEIGTH = 480;
391c1b0f19Sopenharmony_ci
401c1b0f19Sopenharmony_cistatic sptr<CameraDevice> g_cameraInfo = nullptr;
411c1b0f19Sopenharmony_cistatic sptr<CameraManager> g_cameraManager = nullptr;
421c1b0f19Sopenharmony_cistatic sptr<CaptureInput> g_cameraInput = nullptr;
431c1b0f19Sopenharmony_cistatic sptr<CaptureOutput> g_photoOutput = nullptr;
441c1b0f19Sopenharmony_cistatic sptr<CaptureOutput> g_previewOutput = nullptr;
451c1b0f19Sopenharmony_cistatic sptr<CaptureOutput> g_videoOutput = nullptr;
461c1b0f19Sopenharmony_cistatic sptr<CaptureSession> g_captureSession = nullptr;
471c1b0f19Sopenharmony_cistatic std::shared_ptr<DCameraCaptureInfo> g_photoInfo = nullptr;
481c1b0f19Sopenharmony_cistatic std::shared_ptr<DCameraCaptureInfo> g_previewInfo = nullptr;
491c1b0f19Sopenharmony_cistatic std::shared_ptr<DCameraCaptureInfo> g_videoInfo = nullptr;
501c1b0f19Sopenharmony_ci
511c1b0f19Sopenharmony_ci#ifdef DCAMERA_COMMON
521c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
531c1b0f19Sopenharmony_ci    constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
541c1b0f19Sopenharmony_ci    constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
551c1b0f19Sopenharmony_ci#else
561c1b0f19Sopenharmony_ci    constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
571c1b0f19Sopenharmony_ci    constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
581c1b0f19Sopenharmony_ci    constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
591c1b0f19Sopenharmony_ci#endif
601c1b0f19Sopenharmony_ci
611c1b0f19Sopenharmony_ciint32_t InitCameraStandard(CameraPosition position)
621c1b0f19Sopenharmony_ci{
631c1b0f19Sopenharmony_ci    g_cameraManager = CameraManager::GetInstance();
641c1b0f19Sopenharmony_ci    g_cameraManager->SetCallback(std::make_shared<DemoDCameraManagerCallback>());
651c1b0f19Sopenharmony_ci
661c1b0f19Sopenharmony_ci    int rv = g_cameraManager->CreateCaptureSession(&g_captureSession);
671c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
681c1b0f19Sopenharmony_ci        DHLOGE("InitCameraStandard create captureSession failed, rv: %{public}d", rv);
691c1b0f19Sopenharmony_ci        return rv;
701c1b0f19Sopenharmony_ci    }
711c1b0f19Sopenharmony_ci    std::shared_ptr<DemoDCameraSessionCallback> sessionCallback = std::make_shared<DemoDCameraSessionCallback>();
721c1b0f19Sopenharmony_ci    g_captureSession->SetCallback(sessionCallback);
731c1b0f19Sopenharmony_ci    g_captureSession->SetFocusCallback(sessionCallback);
741c1b0f19Sopenharmony_ci
751c1b0f19Sopenharmony_ci    std::vector<sptr<CameraDevice>> cameraObjList = g_cameraManager->GetSupportedCameras();
761c1b0f19Sopenharmony_ci    for (auto info : cameraObjList) {
771c1b0f19Sopenharmony_ci        DHLOGI("Camera: %{public}s, position: %{public}d, camera type: %{public}d, connection type: %{public}d",
781c1b0f19Sopenharmony_ci            GetAnonyString(info->GetID()).c_str(),
791c1b0f19Sopenharmony_ci            info->GetPosition(), info->GetCameraType(), info->GetConnectionType());
801c1b0f19Sopenharmony_ci        // CAMERA_POSITION_BACK or CAMERA_POSITION_FRONT
811c1b0f19Sopenharmony_ci        if ((info->GetPosition() == position) &&
821c1b0f19Sopenharmony_ci            (info->GetConnectionType() == ConnectionType::CAMERA_CONNECTION_REMOTE)) {
831c1b0f19Sopenharmony_ci            g_cameraInfo = info;
841c1b0f19Sopenharmony_ci            break;
851c1b0f19Sopenharmony_ci        }
861c1b0f19Sopenharmony_ci    }
871c1b0f19Sopenharmony_ci
881c1b0f19Sopenharmony_ci    if (g_cameraInfo == nullptr) {
891c1b0f19Sopenharmony_ci        DHLOGE("Distributed Camera Demo: have no remote camera");
901c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
911c1b0f19Sopenharmony_ci    }
921c1b0f19Sopenharmony_ci
931c1b0f19Sopenharmony_ci    rv = g_cameraManager->CreateCameraInput(g_cameraInfo, &((sptr<CameraInput> &)g_cameraInput));
941c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
951c1b0f19Sopenharmony_ci        DHLOGE("InitCameraStandard create cameraInput failed, rv: %{public}d", rv);
961c1b0f19Sopenharmony_ci        return rv;
971c1b0f19Sopenharmony_ci    }
981c1b0f19Sopenharmony_ci    int32_t ret = ((sptr<CameraInput> &)g_cameraInput)->Open();
991c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
1001c1b0f19Sopenharmony_ci        DHLOGE("InitCameraStandard g_cameraInput Open failed, ret: %{public}d", ret);
1011c1b0f19Sopenharmony_ci        return ret;
1021c1b0f19Sopenharmony_ci    }
1031c1b0f19Sopenharmony_ci    std::shared_ptr<DemoDCameraInputCallback> inputCallback = std::make_shared<DemoDCameraInputCallback>();
1041c1b0f19Sopenharmony_ci    ((sptr<CameraInput> &)g_cameraInput)->SetErrorCallback(inputCallback);
1051c1b0f19Sopenharmony_ci    return DCAMERA_OK;
1061c1b0f19Sopenharmony_ci}
1071c1b0f19Sopenharmony_ci
1081c1b0f19Sopenharmony_civoid InitCaptureInfo(int32_t width, int32_t height)
1091c1b0f19Sopenharmony_ci{
1101c1b0f19Sopenharmony_ci    g_photoInfo = std::make_shared<DCameraCaptureInfo>();
1111c1b0f19Sopenharmony_ci    g_photoInfo->width_ = PHOTO_WIDTH;
1121c1b0f19Sopenharmony_ci    g_photoInfo->height_ = PHOTO_HEIGTH;
1131c1b0f19Sopenharmony_ci    g_photoInfo->format_ = PHOTO_FORMAT;
1141c1b0f19Sopenharmony_ci
1151c1b0f19Sopenharmony_ci    g_previewInfo = std::make_shared<DCameraCaptureInfo>();
1161c1b0f19Sopenharmony_ci    if (width == 0 && height == 0) {
1171c1b0f19Sopenharmony_ci        g_previewInfo->width_ = PREVIEW_WIDTH;
1181c1b0f19Sopenharmony_ci        g_previewInfo->height_ = PREVIEW_HEIGTH;
1191c1b0f19Sopenharmony_ci    } else {
1201c1b0f19Sopenharmony_ci        g_previewInfo->width_ = width;
1211c1b0f19Sopenharmony_ci        g_previewInfo->height_ = height;
1221c1b0f19Sopenharmony_ci    }
1231c1b0f19Sopenharmony_ci    g_previewInfo->format_ = PREVIEW_FORMAT;
1241c1b0f19Sopenharmony_ci
1251c1b0f19Sopenharmony_ci    g_videoInfo = std::make_shared<DCameraCaptureInfo>();
1261c1b0f19Sopenharmony_ci    g_videoInfo->width_ = VIDEO_WIDTH;
1271c1b0f19Sopenharmony_ci    g_videoInfo->height_ = VIDEO_HEIGTH;
1281c1b0f19Sopenharmony_ci    g_videoInfo->format_ = VIDEO_FORMAT;
1291c1b0f19Sopenharmony_ci}
1301c1b0f19Sopenharmony_ci
1311c1b0f19Sopenharmony_ciCameraFormat ConvertToCameraFormat(int32_t format)
1321c1b0f19Sopenharmony_ci{
1331c1b0f19Sopenharmony_ci    CameraFormat ret = CameraFormat::CAMERA_FORMAT_INVALID;
1341c1b0f19Sopenharmony_ci    DCameraFormat df = static_cast<DCameraFormat>(format);
1351c1b0f19Sopenharmony_ci    switch (df) {
1361c1b0f19Sopenharmony_ci        case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888:
1371c1b0f19Sopenharmony_ci            ret = CameraFormat::CAMERA_FORMAT_RGBA_8888;
1381c1b0f19Sopenharmony_ci            break;
1391c1b0f19Sopenharmony_ci        case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888:
1401c1b0f19Sopenharmony_ci            ret = CameraFormat::CAMERA_FORMAT_YCBCR_420_888;
1411c1b0f19Sopenharmony_ci            break;
1421c1b0f19Sopenharmony_ci        case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP:
1431c1b0f19Sopenharmony_ci            ret = CameraFormat::CAMERA_FORMAT_YUV_420_SP;
1441c1b0f19Sopenharmony_ci            break;
1451c1b0f19Sopenharmony_ci        case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG:
1461c1b0f19Sopenharmony_ci            ret = CameraFormat::CAMERA_FORMAT_JPEG;
1471c1b0f19Sopenharmony_ci            break;
1481c1b0f19Sopenharmony_ci        default:
1491c1b0f19Sopenharmony_ci            break;
1501c1b0f19Sopenharmony_ci    }
1511c1b0f19Sopenharmony_ci    return ret;
1521c1b0f19Sopenharmony_ci}
1531c1b0f19Sopenharmony_ci
1541c1b0f19Sopenharmony_civoid InitPhotoOutput(void)
1551c1b0f19Sopenharmony_ci{
1561c1b0f19Sopenharmony_ci    DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %{public}d, height = %{public}d, format = %{public}d",
1571c1b0f19Sopenharmony_ci        g_photoInfo->width_, g_photoInfo->height_, g_photoInfo->format_);
1581c1b0f19Sopenharmony_ci    sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
1591c1b0f19Sopenharmony_ci    sptr<IBufferConsumerListener> photoListener = new DemoDCameraPhotoSurfaceListener(photoSurface);
1601c1b0f19Sopenharmony_ci    photoSurface->RegisterConsumerListener(photoListener);
1611c1b0f19Sopenharmony_ci    CameraFormat photoFormat = ConvertToCameraFormat(g_photoInfo->format_);
1621c1b0f19Sopenharmony_ci    Size photoSize = {g_photoInfo->width_, g_photoInfo->height_};
1631c1b0f19Sopenharmony_ci    Profile photoProfile(photoFormat, photoSize);
1641c1b0f19Sopenharmony_ci    sptr<IBufferProducer> photoProducer = photoSurface->GetProducer();
1651c1b0f19Sopenharmony_ci    int rv = g_cameraManager->CreatePhotoOutput(photoProfile, photoProducer, &((sptr<PhotoOutput> &)g_photoOutput));
1661c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
1671c1b0f19Sopenharmony_ci        DHLOGE("InitPhotoOutput create photoOutput failed, rv: %{public}d", rv);
1681c1b0f19Sopenharmony_ci        return;
1691c1b0f19Sopenharmony_ci    }
1701c1b0f19Sopenharmony_ci    ((sptr<PhotoOutput> &)g_photoOutput)->SetCallback(std::make_shared<DemoDCameraPhotoCallback>());
1711c1b0f19Sopenharmony_ci}
1721c1b0f19Sopenharmony_ci
1731c1b0f19Sopenharmony_civoid InitPreviewOutput(void)
1741c1b0f19Sopenharmony_ci{
1751c1b0f19Sopenharmony_ci    DHLOGI("Distributed Camera Demo: Create PreviewOutput, width = %{public}d, height = %{public}d, format = "
1761c1b0f19Sopenharmony_ci        "%{public}d", g_previewInfo->width_, g_previewInfo->height_, g_previewInfo->format_);
1771c1b0f19Sopenharmony_ci    sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
1781c1b0f19Sopenharmony_ci    sptr<IBufferConsumerListener> previewListener = new DemoDCameraPreviewSurfaceListener(previewSurface);
1791c1b0f19Sopenharmony_ci    previewSurface->RegisterConsumerListener(previewListener);
1801c1b0f19Sopenharmony_ci    CameraFormat previewFormat = ConvertToCameraFormat(g_previewInfo->format_);
1811c1b0f19Sopenharmony_ci    Size previewSize = {g_previewInfo->width_, g_previewInfo->height_};
1821c1b0f19Sopenharmony_ci    Profile previewProfile(previewFormat, previewSize);
1831c1b0f19Sopenharmony_ci    sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
1841c1b0f19Sopenharmony_ci    sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
1851c1b0f19Sopenharmony_ci    int rv = g_cameraManager->CreatePreviewOutput(
1861c1b0f19Sopenharmony_ci        previewProfile, previewProducerSurface, &((sptr<PreviewOutput> &)g_previewOutput));
1871c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
1881c1b0f19Sopenharmony_ci        DHLOGE("InitPhotoOutput create previewOutput failed, rv: %{public}d", rv);
1891c1b0f19Sopenharmony_ci        return;
1901c1b0f19Sopenharmony_ci    }
1911c1b0f19Sopenharmony_ci    ((sptr<PreviewOutput> &)g_previewOutput)->SetCallback(std::make_shared<DemoDCameraPreviewCallback>());
1921c1b0f19Sopenharmony_ci}
1931c1b0f19Sopenharmony_ci
1941c1b0f19Sopenharmony_civoid InitVideoOutput(void)
1951c1b0f19Sopenharmony_ci{
1961c1b0f19Sopenharmony_ci    DHLOGI("Distributed Camera Demo: Create VideoOutput, width = %{public}d, height = %{public}d, format = %{public}d",
1971c1b0f19Sopenharmony_ci        g_videoInfo->width_, g_videoInfo->height_, g_videoInfo->format_);
1981c1b0f19Sopenharmony_ci    sptr<IConsumerSurface> videoSurface = IConsumerSurface::Create();
1991c1b0f19Sopenharmony_ci    sptr<IBufferConsumerListener> videoListener = new DemoDCameraVideoSurfaceListener(videoSurface);
2001c1b0f19Sopenharmony_ci    videoSurface->RegisterConsumerListener(videoListener);
2011c1b0f19Sopenharmony_ci    CameraFormat videoFormat = ConvertToCameraFormat(g_videoInfo->format_);
2021c1b0f19Sopenharmony_ci    Size videoSize = {g_videoInfo->width_, g_videoInfo->height_};
2031c1b0f19Sopenharmony_ci    std::vector<int32_t> framerates = {};
2041c1b0f19Sopenharmony_ci    VideoProfile videoSettings(videoFormat, videoSize, framerates);
2051c1b0f19Sopenharmony_ci    sptr<IBufferProducer> videoProducer = videoSurface->GetProducer();
2061c1b0f19Sopenharmony_ci    sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(videoProducer);
2071c1b0f19Sopenharmony_ci    int rv = g_cameraManager->CreateVideoOutput(videoSettings, pSurface, &((sptr<VideoOutput> &)g_videoOutput));
2081c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
2091c1b0f19Sopenharmony_ci        DHLOGE("InitPhotoOutput create videoOutput failed, rv: %{public}d", rv);
2101c1b0f19Sopenharmony_ci        return;
2111c1b0f19Sopenharmony_ci    }
2121c1b0f19Sopenharmony_ci    ((sptr<VideoOutput> &)g_videoOutput)->SetCallback(std::make_shared<DemoDCameraVideoCallback>());
2131c1b0f19Sopenharmony_ci}
2141c1b0f19Sopenharmony_ci
2151c1b0f19Sopenharmony_civoid ConfigCaptureSession(void)
2161c1b0f19Sopenharmony_ci{
2171c1b0f19Sopenharmony_ci    g_captureSession->BeginConfig();
2181c1b0f19Sopenharmony_ci    g_captureSession->AddInput(g_cameraInput);
2191c1b0f19Sopenharmony_ci    g_captureSession->AddOutput(g_previewOutput);
2201c1b0f19Sopenharmony_ci    g_captureSession->AddOutput(g_videoOutput);
2211c1b0f19Sopenharmony_ci    g_captureSession->AddOutput(g_photoOutput);
2221c1b0f19Sopenharmony_ci    g_captureSession->CommitConfig();
2231c1b0f19Sopenharmony_ci
2241c1b0f19Sopenharmony_ci    std::vector<VideoStabilizationMode> stabilizationModes;
2251c1b0f19Sopenharmony_ci    int32_t rv = g_captureSession->GetSupportedStabilizationMode(stabilizationModes);
2261c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
2271c1b0f19Sopenharmony_ci        DHLOGE("ConfigCaptureSession get supported stabilization mode failed, rv: %{public}d", rv);
2281c1b0f19Sopenharmony_ci        return;
2291c1b0f19Sopenharmony_ci    }
2301c1b0f19Sopenharmony_ci    if (!stabilizationModes.empty()) {
2311c1b0f19Sopenharmony_ci        for (auto mode : stabilizationModes) {
2321c1b0f19Sopenharmony_ci            DHLOGI("Distributed Camera Demo: video stabilization mode %{public}d", mode);
2331c1b0f19Sopenharmony_ci        }
2341c1b0f19Sopenharmony_ci        g_captureSession->SetVideoStabilizationMode(stabilizationModes.back());
2351c1b0f19Sopenharmony_ci    }
2361c1b0f19Sopenharmony_ci    g_captureSession->Start();
2371c1b0f19Sopenharmony_ci}
2381c1b0f19Sopenharmony_ci
2391c1b0f19Sopenharmony_civoid ConfigFocusFlashAndExposure(bool isVideo)
2401c1b0f19Sopenharmony_ci{
2411c1b0f19Sopenharmony_ci    g_captureSession->LockForControl();
2421c1b0f19Sopenharmony_ci    FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO;
2431c1b0f19Sopenharmony_ci    ExposureMode exposureMode = EXPOSURE_MODE_AUTO;
2441c1b0f19Sopenharmony_ci    float exposureValue = 0;
2451c1b0f19Sopenharmony_ci    std::vector<float> biasRange;
2461c1b0f19Sopenharmony_ci    int32_t rv = g_captureSession->GetExposureBiasRange(biasRange);
2471c1b0f19Sopenharmony_ci    if (rv != DCAMERA_OK) {
2481c1b0f19Sopenharmony_ci        DHLOGE("ConfigFocusAndExposure get exposure bias range failed, rv: %{public}d", rv);
2491c1b0f19Sopenharmony_ci        return;
2501c1b0f19Sopenharmony_ci    }
2511c1b0f19Sopenharmony_ci    if (!biasRange.empty()) {
2521c1b0f19Sopenharmony_ci        DHLOGI("Distributed Camera Demo: get %{public}d exposure compensation range", biasRange.size());
2531c1b0f19Sopenharmony_ci        exposureValue = biasRange[0];
2541c1b0f19Sopenharmony_ci    }
2551c1b0f19Sopenharmony_ci    FlashMode flash = FLASH_MODE_OPEN;
2561c1b0f19Sopenharmony_ci    if (isVideo) {
2571c1b0f19Sopenharmony_ci        flash = FLASH_MODE_ALWAYS_OPEN;
2581c1b0f19Sopenharmony_ci    }
2591c1b0f19Sopenharmony_ci    g_captureSession->SetFlashMode(flash);
2601c1b0f19Sopenharmony_ci    g_captureSession->SetFocusMode(focusMode);
2611c1b0f19Sopenharmony_ci    g_captureSession->SetExposureMode(exposureMode);
2621c1b0f19Sopenharmony_ci    g_captureSession->SetExposureBias(exposureValue);
2631c1b0f19Sopenharmony_ci    g_captureSession->UnlockForControl();
2641c1b0f19Sopenharmony_ci}
2651c1b0f19Sopenharmony_ci
2661c1b0f19Sopenharmony_cistd::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
2671c1b0f19Sopenharmony_ci{
2681c1b0f19Sopenharmony_ci    std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
2691c1b0f19Sopenharmony_ci    // Rotation
2701c1b0f19Sopenharmony_ci    PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0;
2711c1b0f19Sopenharmony_ci    photoCaptureSettings->SetRotation(rotation);
2721c1b0f19Sopenharmony_ci    // QualityLevel
2731c1b0f19Sopenharmony_ci    PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
2741c1b0f19Sopenharmony_ci    photoCaptureSettings->SetQuality(quality);
2751c1b0f19Sopenharmony_ci    // Location
2761c1b0f19Sopenharmony_ci    std::unique_ptr<Location> location = std::make_unique<Location>();
2771c1b0f19Sopenharmony_ci    location->latitude = LOCATION_LATITUDE;
2781c1b0f19Sopenharmony_ci    location->longitude = LOCATION_LONGITUDE;
2791c1b0f19Sopenharmony_ci    location->altitude = LOCATION_ALTITUDE;
2801c1b0f19Sopenharmony_ci    photoCaptureSettings->SetLocation(location);
2811c1b0f19Sopenharmony_ci    return photoCaptureSettings;
2821c1b0f19Sopenharmony_ci}
2831c1b0f19Sopenharmony_ci
2841c1b0f19Sopenharmony_civoid ReleaseResource(void)
2851c1b0f19Sopenharmony_ci{
2861c1b0f19Sopenharmony_ci    if (g_previewOutput != nullptr) {
2871c1b0f19Sopenharmony_ci        ((sptr<CameraStandard::PreviewOutput> &)g_previewOutput)->Stop();
2881c1b0f19Sopenharmony_ci        g_previewOutput->Release();
2891c1b0f19Sopenharmony_ci        g_previewOutput = nullptr;
2901c1b0f19Sopenharmony_ci    }
2911c1b0f19Sopenharmony_ci    if (g_photoOutput != nullptr) {
2921c1b0f19Sopenharmony_ci        g_photoOutput->Release();
2931c1b0f19Sopenharmony_ci        g_photoOutput = nullptr;
2941c1b0f19Sopenharmony_ci    }
2951c1b0f19Sopenharmony_ci    if (g_videoOutput != nullptr) {
2961c1b0f19Sopenharmony_ci        g_videoOutput->Release();
2971c1b0f19Sopenharmony_ci        g_videoOutput = nullptr;
2981c1b0f19Sopenharmony_ci    }
2991c1b0f19Sopenharmony_ci    if (g_cameraInput != nullptr) {
3001c1b0f19Sopenharmony_ci        g_cameraInput->Close();
3011c1b0f19Sopenharmony_ci        g_cameraInput->Release();
3021c1b0f19Sopenharmony_ci        g_cameraInput = nullptr;
3031c1b0f19Sopenharmony_ci    }
3041c1b0f19Sopenharmony_ci    if (g_captureSession != nullptr) {
3051c1b0f19Sopenharmony_ci        g_captureSession->Stop();
3061c1b0f19Sopenharmony_ci        g_captureSession->Release();
3071c1b0f19Sopenharmony_ci        g_captureSession = nullptr;
3081c1b0f19Sopenharmony_ci    }
3091c1b0f19Sopenharmony_ci    if (g_cameraManager != nullptr) {
3101c1b0f19Sopenharmony_ci        g_cameraManager->SetCallback(nullptr);
3111c1b0f19Sopenharmony_ci    }
3121c1b0f19Sopenharmony_ci    g_cameraInfo = nullptr;
3131c1b0f19Sopenharmony_ci    g_cameraManager = nullptr;
3141c1b0f19Sopenharmony_ci}
3151c1b0f19Sopenharmony_ci
3161c1b0f19Sopenharmony_ciint32_t Capture()
3171c1b0f19Sopenharmony_ci{
3181c1b0f19Sopenharmony_ci    int32_t ret = ((sptr<PhotoOutput> &)g_photoOutput)->Capture(ConfigPhotoCaptureSetting());
3191c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
3201c1b0f19Sopenharmony_ci        DHLOGE("main g_photoOutput Capture failed, ret: %{public}d", ret);
3211c1b0f19Sopenharmony_ci        return ret;
3221c1b0f19Sopenharmony_ci    }
3231c1b0f19Sopenharmony_ci    return DCAMERA_OK;
3241c1b0f19Sopenharmony_ci}
3251c1b0f19Sopenharmony_ci
3261c1b0f19Sopenharmony_ciint32_t Video()
3271c1b0f19Sopenharmony_ci{
3281c1b0f19Sopenharmony_ci    int32_t ret = ((sptr<VideoOutput> &)g_videoOutput)->Start();
3291c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
3301c1b0f19Sopenharmony_ci        DHLOGE("main VideoOutput Start failed, ret: %{public}d", ret);
3311c1b0f19Sopenharmony_ci        return ret;
3321c1b0f19Sopenharmony_ci    }
3331c1b0f19Sopenharmony_ci    return DCAMERA_OK;
3341c1b0f19Sopenharmony_ci}
3351c1b0f19Sopenharmony_ci
3361c1b0f19Sopenharmony_ciint32_t GetPreviewProfiles(std::vector<CameraStandard::Size> &previewResolution)
3371c1b0f19Sopenharmony_ci{
3381c1b0f19Sopenharmony_ci    sptr<CameraOutputCapability> capability = g_cameraManager->GetSupportedOutputCapability(g_cameraInfo);
3391c1b0f19Sopenharmony_ci    if (capability == nullptr) {
3401c1b0f19Sopenharmony_ci        DHLOGI("get supported capability is null");
3411c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
3421c1b0f19Sopenharmony_ci    }
3431c1b0f19Sopenharmony_ci    std::vector<CameraStandard::Profile> previewProfiles = capability->GetPreviewProfiles();
3441c1b0f19Sopenharmony_ci    DHLOGI("size: %{public}d", previewProfiles.size());
3451c1b0f19Sopenharmony_ci    for (auto& profile : previewProfiles) {
3461c1b0f19Sopenharmony_ci        CameraStandard::Size picSize = profile.GetSize();
3471c1b0f19Sopenharmony_ci        DHLOGI("width: %{public}d, height: %{public}d", picSize.width, picSize.height);
3481c1b0f19Sopenharmony_ci        if (IsValid(picSize)) {
3491c1b0f19Sopenharmony_ci            previewResolution.push_back(picSize);
3501c1b0f19Sopenharmony_ci        }
3511c1b0f19Sopenharmony_ci    }
3521c1b0f19Sopenharmony_ci    return DCAMERA_OK;
3531c1b0f19Sopenharmony_ci}
3541c1b0f19Sopenharmony_ci
3551c1b0f19Sopenharmony_cibool IsValid(const CameraStandard::Size& size)
3561c1b0f19Sopenharmony_ci{
3571c1b0f19Sopenharmony_ci    return (size.width >= RESOLUTION_MIN_WIDTH) && (size.height >= RESOLUTION_MIN_HEIGHT) &&
3581c1b0f19Sopenharmony_ci        (size.width <= RESOLUTION_MAX_WIDTH_CONTINUOUS) && (size.height <= RESOLUTION_MAX_HEIGHT_CONTINUOUS);
3591c1b0f19Sopenharmony_ci}
3601c1b0f19Sopenharmony_ci} // namespace DistributedHardware
3611c1b0f19Sopenharmony_ci} // namespace OHOS
362