目录

Preview (C/C++)

Preview is the image you see after you start the camera application but before you take photos or record videos.

How to Develop

For detailed API descriptions, see OH_Camera.

  1. Import the NDK, which provides camera-related properties and methods.

    #include <cstdint>
    #include <native_buffer/buffer_common.h>
    #include <unistd.h>
    #include <string>
    #include <thread>
    #include <cstdio>
    #include <fcntl.h>
    #include <map>
    #include <vector>
    #include <native_buffer/native_buffer.h>
    #include "iostream"
    #include "mutex"
    
    #include "hilog/log.h"
    #include "ohcamera/camera.h"
    #include "ohcamera/camera_input.h"
    #include "ohcamera/capture_session.h"
    #include "ohcamera/photo_output.h"
    #include "ohcamera/preview_output.h"
    #include "ohcamera/video_output.h"
    #include "napi/native_api.h"
    #include "ohcamera/camera_manager.h"
    #include <window_manager/oh_display_info.h>
    #include <window_manager/oh_display_manager.h>
    
    namespace OHOS_CAMERA_SAMPLE {
    class NDKCamera {
      public:
        struct CameraBuildingConfig {
            char *str;
            uint32_t focusMode;
            uint32_t cameraDeviceIndex;
            bool isVideo;
            bool isHdr;
            char *videoId;
        };
        ~NDKCamera();
        explicit NDKCamera(CameraBuildingConfig config);
        // ...
    };
    } // namespace OHOS_CAMERA_SAMPLE
  2. Link the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC
        libace_napi.z.so
        libohcamera.so
        libhilog_ndk.z.so
    )
  3. Obtain the surface ID.

    The XComponent provides the surface ID for preview streams, and its capabilities are offered by the UI. For details, see XComponent Reference.

  4. Call OH_CameraManager_GetSupportedCameraOutputCapability() to obtain the preview capability supported by the current device based on the surface ID. Then call OH_CameraManager_CreatePreviewOutput() to create a PreviewOutput instance, with the parameters set to the cameraManager pointer, the first item in the previewProfiles array, the surface ID obtained in step 3, and the returned previewOutput pointer, respectively.

    Camera_ErrorCode NDKCamera::CreatePreviewOutput(void)
    {
        if (previewProfile_ == nullptr) {
            OH_LOG_ERROR(LOG_APP, "Get previewProfiles failed.");
            return CAMERA_INVALID_ARGUMENT;
        }
        ret_ = OH_CameraManager_CreatePreviewOutput(cameraManager_, previewProfile_, previewSurfaceId_, &previewOutput_);
        OH_LOG_INFO(LOG_APP, "create preview width: %{public}d, height: %{public}d, format: %{public}d",
            previewProfile_->size.width, previewProfile_->size.height, previewProfile_->format);
        if (previewSurfaceId_ == nullptr || previewOutput_ == nullptr || ret_ != CAMERA_OK) {
            OH_LOG_ERROR(LOG_APP, "CreatePreviewOutput failed.");
            return CAMERA_INVALID_ARGUMENT;
        }
        PreviewOutputRegisterCallback();
        return ret_;
    }
  5. Configure the session. Call commitConfig() to commit the session configuration, and then call OH_CaptureSession_Start() to start outputting the preview stream. If the call fails, an error code is returned. For details, see Camera_ErrorCode.

    Camera_ErrorCode NDKCamera::SessionStart(void)
    {
        Camera_ErrorCode ret = OH_CaptureSession_Start(captureSession_);
        if (ret == CAMERA_OK) {
            OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Start success.");
        } else {
            OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed. %d ", ret);
        }
        return ret;
    }
  6. Call OH_CaptureSession_Stop() to stop outputting the preview stream. If the call fails, an error code is returned. For details, see Camera_ErrorCode.

    Camera_ErrorCode NDKCamera::SessionStop(void)
    {
        Camera_ErrorCode ret = OH_CaptureSession_Stop(captureSession_);
        if (ret == CAMERA_OK) {
            OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success.");
        } else {
            OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
        }
        return ret;
    }

Status Listening

During camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors.