拍照开发指导

使用场景

使用Camera产生图片帧(拍照)。

接口说明

表 1 API列表

类名

接口名

描述

CameraKit

int32_t GetCameraIds(std::list<string> cameraList)

获取cameraId列表

CameraKit

CameraAbility& GetCameraAbility(string cameraId)

获取指定camera的能力

CameraKit

void RegisterCameraDeviceCallback(CameraDeviceCallback callback, EventHandler handler)

注册camera设备状态回调

CameraKit

void UnregisterCameraDeviceCallback(CameraDeviceCallback callback)

去注册camera设备状态回调

CameraKit

void CreateCamera(string cameraId, CameraStateCallback callback, EventHandler handler)

创建camera实例

Camera

string GetCameraId()

获取cameraID

Camera

CameraConfig& GetCameraConfig()

获取camera配置信息

Camera

FrameConfig& GetFrameConfig(int32_t type)

获取捕获帧类型

Camera

void Configure(CameraConfig& config)

配置camera

Camera

void Release()

释放camera

Camera

int TriggerLoopingCapture(FrameConfig& frameConfig)

开始循环帧捕获

Camera

void StopLoopingCapture()

停止循环帧捕获

Camera

int32_t TriggerSingleCapture(FrameConfig& frameConfig)

抓图

CameraConfig

void SetFrameStateCallback(FrameStateCallback callback, EventHandler handler);

设置帧状态回调

CameraConfig

static CameraConfig CreateCameraConfig()

创建camera配置信息实例

CameraAbility

std::list<Size> GetSupportedSizes(int format)

根据类型获取支持输出图像尺寸大小

CameraAbility

std::list<T> GetParameterRange(uint32_t key)

获取支持的参数范围

CameraDevice

CameraDeviceCallback()

camera设备回调类构造函数

CameraDevice

void OnCameraStatus​(std::string cameraId, int32_t status)

camera设备状态变化时的回调

CameraStateCallback

CameraStateCallback​()

camera状态回调类构造函数

CameraStateCallback

void OnConfigured​(Camera& camera)

camera配置成功回调

CameraStateCallback

void OnConfigureFailed​(Camera& camera,int32_t errorCode)

camera配置失败回调

CameraStateCallback

void OnCreated​(Camera& camera)

camera创建成功回调

CameraStateCallback

void OnCreateFailed​(std::string cameraId,int32_t errorCode)

camera创建失败回调

CameraStateCallback

void OnReleased​(Camera& camera)

camera释放回调

FrameStateCallback

FrameStateCallback​()

帧状态回调类构造函数

FrameStateCallback

void OnFrameFinished(Camera& camera, FrameConfig& frameConfig, FrameResult& frameResult)

拍照帧完成回调

FrameStateCallback

void OnFrameError​(Camera& camera, FrameConfig& frameConfig, int32_t errorCode, FrameResult& frameResult)

拍照帧异常回调

FrameConfig

int32_t GetFrameConfigType()

获取帧配置类型

FrameConfig

std::list<OHOS::Surface> GetSurfaces()

获取帧配置的surface

FrameConfig

void AddSurface(OHOS::AGP::UISurface& surface);

添加surface

FrameConfig

void RemoveSurface(OHOS::AGP::UISurface& surface);

删除surface

约束与限制

无。

开发步骤

  1. 实现设备状态回调的派生类,用户在设备状态发生变更(如新插入相机设备/相机掉线)时,自定义操作。

    1. class SampleCameraDeviceCallback : public CameraDeviceCallback {
    2. void OnCameraStatus(std::string cameraId, int32_t status) override
    3. {
    4. //do something when camera is available/unavailable
    5. }
    6. };
  2. 实现帧事件回调的派生类,这里在拿到帧数据以后将其转存为文件。

    1. static void SampleSaveCapture(const char *p, uint32_t size)
    2. {
    3. cout << "Start saving picture" << endl;
    4. struct timeval tv;
    5. gettimeofday(&tv, NULL);
    6. struct tm *ltm = localtime(&tv.tv_sec);
    7. if (ltm != nullptr) {
    8. ostringstream ss("Capture_");
    9. ss << "Capture" << ltm->tm_hour << "-" << ltm->tm_min << "-" << ltm->tm_sec << ".jpg";
    10. ofstream pic("/sdcard/" + ss.str(), ofstream::out | ofstream::trunc);
    11. cout << "write " << size << " bytes" << endl;
    12. pic.write(p, size);
    13. cout << "Saving picture end" << endl;
    14. }
    15. }
    16. class TestFrameStateCallback : public FrameStateCallback {
    17. void OnFrameFinished(Camera &camera, FrameConfig &fc, FrameResult &result) override
    18. {
    19. cout << "Receive frame complete inform." << endl;
    20. if (fc.GetFrameConfigType() == FRAME_CONFIG_CAPTURE) {
    21. cout << "Capture frame received." << endl;
    22. list<Surface *> surfaceList = fc.GetSurfaces();
    23. for (Surface *surface : surfaceList) {
    24. SurfaceBuffer *buffer = surface->AcquireBuffer();
    25. if (buffer != nullptr) {
    26. char *virtAddr = static_cast<char *>(buffer->GetVirAddr());
    27. if (virtAddr != nullptr) {
    28. SampleSaveCapture(virtAddr, buffer->GetSize());
    29. }
    30. surface->ReleaseBuffer(buffer);
    31. }
    32. delete surface;
    33. }
    34. delete &fc;
    35. }
    36. }
    37. };
  3. 实现相机状态回调的派生类,当相机状态发生变化(配置成功/失败,创建成功/失败)时,自定义操作。

    1. class SampleCameraStateMng : public CameraStateCallback {
    2. public:
    3. SampleCameraStateMng() = delete;
    4. SampleCameraStateMng(EventHandler &eventHdlr) : eventHdlr_(eventHdlr) {}
    5. ~SampleCameraStateMng()
    6. {
    7. if (recordFd_ != -1) {
    8. close(recordFd_);
    9. }
    10. }
    11. void OnCreated(Camera &c) override
    12. {
    13. cout << "Sample recv OnCreate camera." << endl;
    14. auto config = CameraConfig::CreateCameraConfig();
    15. config->SetFrameStateCallback(&fsCb_, &eventHdlr_);
    16. c.Configure(*config);
    17. cam_ = &c;
    18. }
    19. void OnCreateFailed(const std::string cameraId, int32_t errorCode) override {}
    20. void OnReleased(Camera &c) override {}
    21. };
  4. 创建CameraKit,用于创建和获取camera信息。

    1. CameraKit *camKit = CameraKit::GetInstance();
    2. list<string> camList = camKit->GetCameraIds();
    3. string camId;
    4. for (auto &cam : camList) {
    5. cout << "camera name:" << cam << endl;
    6. const CameraAbility *ability = camKit->GetCameraAbility(cam);
    7. /* find camera which fits user's ability */
    8. list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
    9. if (find(sizeList.begin(), sizeList.end(), CAM_PIC_1080P) != sizeList.end()) {
    10. camId = cam;
    11. break;
    12. }
    13. }
  5. 创建Camera实例。

    1. EventHandler eventHdlr; // Create a thread to handle callback events
    2. SampleCameraStateMng CamStateMng(eventHdlr);
    3. camKit->CreateCamera(camId, CamStateMng, eventHdlr);
  6. 根据步骤1步骤2步骤3中的回调设计,camera实例创建成功后会进行配置操作,主流程中app需要设计同步机制。

    1. void OnCreated(Camera &c) override
    2. {
    3. cout << "Sample recv OnCreate camera." << endl;
    4. auto config = CameraConfig::CreateCameraConfig();
    5. config->SetFrameStateCallback(&fsCb_, &eventHdlr_);
    6. c.Configure(*config);
    7. cam_ = &c;
    8. }
    9. void Capture()
    10. {
    11. if (cam_ == nullptr) {
    12. cout << "Camera is not ready." << endl;
    13. return;
    14. }
    15. FrameConfig *fc = new FrameConfig(FRAME_CONFIG_CAPTURE);
    16. Surface *surface = Surface::CreateSurface();
    17. if (surface == nullptr) {
    18. delete fc;
    19. }
    20. surface->SetWidthAndHeight(1920, 1080); /* 1920:width,1080:height */
    21. fc->AddSurface(*surface);
    22. cam_->TriggerSingleCapture(*fc);
    23. }