鸿蒙(API 12 Beta3版)【录像流二次处理的实现方案(C/C++)】媒体相机开发指导

开发流程

在获取到相机支持的输出流能力后,开始创建录像流,开发流程如下。

1

完整示例

  1. 在CMake脚本中链接相关动态库。

        target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
    
  2. cpp侧导入NDK接口,并根据传入的SurfaceId进行录像。

#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 "ohcamera/camera_manager.h"

void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
{
    OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode);
}

CameraInput_Callbacks* GetCameraInputListener(void)
{
    static CameraInput_Callbacks cameraInputCallbacks = {
        .onError = OnCameraInputError
    };
    return &cameraInputCallbacks;
}

void CaptureSessionOnFocusStateChange(Camera_CaptureSession* session, Camera_FocusState focusState)
{
    OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange");
}

void CaptureSessionOnError(Camera_CaptureSession* session, Camera_ErrorCode errorCode)
{
    OH_LOG_INFO(LOG_APP, "CaptureSessionOnError = %{public}d", errorCode);
}

CaptureSession_Callbacks* GetCaptureSessionRegister(void)
{
    static CaptureSession_Callbacks captureSessionCallbacks = {
        .onFocusStateChange = CaptureSessionOnFocusStateChange,
        .onError = CaptureSessionOnError
    };
    return &captureSessionCallbacks;
}

void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
{
    OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
}

void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
{
    OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
}

void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
{
    OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
}

VideoOutput_Callbacks* GetVideoOutputListener(void)
{
    static VideoOutput_Callbacks videoOutputListener = {
        .onFrameStart = VideoOutputOnFrameStart,
        .onFrameEnd = VideoOutputOnFrameEnd,
        .onError = VideoOutputOnError
    };
    return &videoOutputListener;
}

void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
{
    OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
}

CameraManager_Callbacks* GetCameraManagerListener()
{
    static CameraManager_Callbacks cameraManagerListener = {
        .onCameraStatus = CameraManagerStatusCallback
    };
    return &cameraManagerListener;
}

NDKCamera::NDKCamera(char *previewId, char *videoId)
{
    Camera_Manager* cameraManager = nullptr;
    Camera_Device* cameras = nullptr;
    Camera_CaptureSession* captureSession = nullptr;
    Camera_OutputCapability* cameraOutputCapability = nullptr;
    Camera_VideoOutput* videoOutput = nullptr;
    const Camera_Profile* previewProfile = nullptr;
    const Camera_Profile* photoProfile = nullptr;
    const Camera_VideoProfile* videoProfile = nullptr;
    Camera_PreviewOutput* previewOutput = nullptr;
    Camera_PhotoOutput* photoOutput = nullptr;
    Camera_Input* cameraInput = nullptr;
    uint32_t size = 0;
    uint32_t cameraDeviceIndex = 0;
    char* videoSurfaceId = videoId;
    char* previewSurfaceId = previewId;
    // 创建CameraManager对象
    Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
    if (cameraManager == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed.");
    }
    // 监听相机状态变化
    ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
    }

    // 获取相机列表
    ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
    if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
    }

    for (int index = 0; index < size; index++) {
    OH_LOG_ERROR(LOG_APP, "cameraId  =  %{public}s ", cameras[index].cameraId);              // 获取相机ID
    OH_LOG_ERROR(LOG_APP, "cameraPosition  =  %{public}d ", cameras[index].cameraPosition);  // 获取相机位置
    OH_LOG_ERROR(LOG_APP, "cameraType  =  %{public}d ", cameras[index].cameraType);          // 获取相机类型
    OH_LOG_ERROR(LOG_APP, "connectionType  =  %{public}d ", cameras[index].connectionType);  // 获取相机连接类型
    }

    // 获取相机设备支持的输出流能力
    ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
                                                            &cameraOutputCapability);
    if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
    }

    if (cameraOutputCapability->previewProfilesSize < 0) {
    OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
    }
    previewProfile = cameraOutputCapability->previewProfiles[0];

    if (cameraOutputCapability->photoProfilesSize < 0) {
    OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null");
    }
    photoProfile = cameraOutputCapability->photoProfiles[0];

    if (cameraOutputCapability->videoProfilesSize < 0) {
    OH_LOG_ERROR(LOG_APP, "videorofilesSize == null");
    }
    videoProfile = cameraOutputCapability->videoProfiles[0];

    // 调用ts侧 getVideoSurfaceID()

    // 创建VideoOutput对象
    ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput);
    if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
    }

    // 监听视频输出错误信息
    ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
    }

    //创建会话
    ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
    if (captureSession == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
    }
    // 监听session错误信息
    ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
    }

    // 开始配置会话
    ret = OH_CaptureSession_BeginConfig(captureSession);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
    }

    // 创建相机输入流
    ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
    if (cameraInput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
    }

    // 监听cameraInput错误信息
    ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
    }

    // 打开相机
    ret = OH_CameraInput_Open(cameraInput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed.");
    }

    // 向会话中添加相机输入流
    ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
    }

    // 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface
    ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
    if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
    }

    // 向会话中添加预览输出流
    ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
    }

    // 向会话中添加录像输出流
    ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed.");
    }

    // 提交会话配置
    ret = OH_CaptureSession_CommitConfig(captureSession);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
    }

    // 启动会话
    ret = OH_CaptureSession_Start(captureSession);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
    }

    // 启动录像输出流
    ret = OH_VideoOutput_Start(videoOutput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
    }

    // 停止录像输出流
    ret = OH_VideoOutput_Stop(videoOutput);
    if (ret != CAMERA_OK) {
    OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
    }

    // 停止当前会话
    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);
    }

    // 释放相机输入流
    ret = OH_CameraInput_Close(cameraInput);
    if (ret == CAMERA_OK) {
    OH_LOG_INFO(LOG_APP, "OH_CameraInput_Close success ");
    } else {
    OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Close failed. %d ", ret);
    }

    // 释放预览输出流
    ret = OH_PreviewOutput_Release(previewOutput);
    if (ret == CAMERA_OK) {
    OH_LOG_INFO(LOG_APP, "OH_PreviewOutput_Release success ");
    } else {
    OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Release failed. %d ", ret);
    }

    // 释放录像输出流
    ret = OH_VideoOutput_Release(videoOutput);
    if (ret == CAMERA_OK) {
    OH_LOG_INFO(LOG_APP, "OH_VideoOutput_Release success ");
    } else {
    OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Release failed. %d ", ret);
    }

    // 释放会话
    ret = OH_CaptureSession_Release(captureSession);
    if (ret == CAMERA_OK) {
    OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Release success ");
    } else {
    OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Release failed. %d ", ret);
    }
}

最后呢

很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。

而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。

在这里插入图片描述

针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。

  • 《鸿蒙 (OpenHarmony)开发学习视频》
  • 《鸿蒙生态应用开发V2.0白皮书》
  • 《鸿蒙 (OpenHarmony)开发基础到实战手册》
  • OpenHarmony北向、南向开发环境搭建
  • 《鸿蒙开发基础》
  • 《鸿蒙开发进阶》
  • 《鸿蒙开发实战》

在这里插入图片描述

总结

鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。

并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿
1

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值