• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 拍照开发指导
2
3
4## 使用场景
5
6使用Camera产生图片帧(拍照)。
7
8
9### 接口说明
10
11  **表1** API列表
12
13| 类名 | 接口名 | 描述 |
14| -------- | -------- | -------- |
15| CameraKit | int32_t GetCameraIds(std::list<string> cameraList) | 获取cameraId列表 |
16| CameraKit | CameraAbility& GetCameraAbility(string cameraId) | 获取指定camera的能力 |
17| CameraKit | void RegisterCameraDeviceCallback(CameraDeviceCallback\* callback, EventHandler\* handler) | 注册camera设备状态回调 |
18| CameraKit | void UnregisterCameraDeviceCallback(CameraDeviceCallback\* callback) | 去注册camera设备状态回调 |
19| CameraKit | void CreateCamera(string cameraId, CameraStateCallback\* callback, EventHandler\* handler) | 创建camera实例 |
20| Camera | string GetCameraId() | 获取cameraID |
21| Camera | CameraConfig& GetCameraConfig() | 获取camera配置信息 |
22| Camera | FrameConfig& GetFrameConfig(int32_t type) | 获取捕获帧类型 |
23| Camera | void Configure(CameraConfig& config) | 配置camera |
24| Camera | void Release() | 释放camera |
25| Camera | int TriggerLoopingCapture(FrameConfig& frameConfig) | 开始循环帧捕获 |
26| Camera | void StopLoopingCapture() | 停止循环帧捕获 |
27| Camera | int32_t TriggerSingleCapture(FrameConfig& frameConfig) | 截图 |
28| CameraConfig | void SetFrameStateCallback(FrameStateCallback\* callback, EventHandler\* handler); | 设置帧状态回调 |
29| CameraConfig | static CameraConfig\* CreateCameraConfig() | 创建camera配置信息实例 |
30| CameraAbility | std::list<Size> GetSupportedSizes(int format) | 根据类型获取支持输出图像尺寸大小 |
31| CameraAbility | std::list<T> GetParameterRange(uint32_t key) | 获取支持的参数范围 |
32| CameraDevice | CameraDeviceCallback() | camera设备回调类构造函数 |
33| CameraDevice | void OnCameraStatus​(std::string cameraId, int32_t status) | camera设备状态变化时的回调 |
34| CameraStateCallback | CameraStateCallback​() | camera状态回调类构造函数 |
35| CameraStateCallback | void OnConfigured​(Camera& camera) | camera配置成功回调 |
36| CameraStateCallback | void OnConfigureFailed​(Camera& camera,int32_t errorCode) | camera配置失败回调 |
37| CameraStateCallback | void OnCreated​(Camera& camera) | camera创建成功回调 |
38| CameraStateCallback | void OnCreateFailed​(std::string cameraId,int32_t errorCode) | camera创建失败回调 |
39| CameraStateCallback | void OnReleased​(Camera& camera) | camera释放回调 |
40| FrameStateCallback | FrameStateCallback​() | 帧状态回调类构造函数 |
41| FrameStateCallback | void OnFrameFinished(Camera& camera, FrameConfig& frameConfig, FrameResult& frameResult) | 拍照帧完成回调 |
42| FrameStateCallback | void OnFrameError​(Camera& camera, FrameConfig& frameConfig, int32_t errorCode, FrameResult& frameResult) | 拍照帧异常回调 |
43| FrameConfig | int32_t GetFrameConfigType() | 获取帧配置类型 |
44| FrameConfig | std::list<OHOS::Surface> GetSurfaces() | 获取帧配置的surface |
45| FrameConfig | void AddSurface(OHOS::AGP::UISurface& surface); | 添加surface |
46| FrameConfig | void RemoveSurface(OHOS::AGP::UISurface& surface); | 删除surface |
47
48
49## 约束与限制
50
51无。
52
53
54## 开发步骤
55
561. 实现设备状态回调的派生类,用户在设备状态发生变更(如新插入相机设备/相机掉线)时,自定义操作。
57
58   ```
59   class SampleCameraDeviceCallback : public CameraDeviceCallback {
60       void OnCameraStatus(std::string cameraId, int32_t status) override
61       {
62           //do something when camera is available/unavailable
63       }
64   };
65   ```
66
672. 实现帧事件回调的派生类,这里在拿到帧数据以后将其转存为文件。
68
69   ```
70   static void SampleSaveCapture(const char *p, uint32_t size)
71   {
72       cout << "Start saving picture" << endl;
73       struct timeval tv;
74       gettimeofday(&tv, NULL);
75       struct tm *ltm = localtime(&tv.tv_sec);
76       if (ltm != nullptr) {
77           ostringstream ss("Capture_");
78           ss << "Capture" << ltm->tm_hour << "-" << ltm->tm_min << "-" << ltm->tm_sec << ".jpg";
79
80           ofstream pic("/sdcard/" + ss.str(), ofstream::out | ofstream::trunc);
81           cout << "write " << size << " bytes" << endl;
82           pic.write(p, size);
83           cout << "Saving picture end" << endl;
84       }
85   }
86
87   class TestFrameStateCallback : public FrameStateCallback {
88       void OnFrameFinished(Camera &camera, FrameConfig &fc, FrameResult &result) override
89       {
90           cout << "Receive frame complete inform." << endl;
91           if (fc.GetFrameConfigType() == FRAME_CONFIG_CAPTURE) {
92               cout << "Capture frame received." << endl;
93               list<Surface *> surfaceList = fc.GetSurfaces();
94               for (Surface *surface : surfaceList) {
95                   SurfaceBuffer *buffer = surface->AcquireBuffer();
96                   if (buffer != nullptr) {
97                       char *virtAddr = static_cast<char *>(buffer->GetVirAddr());
98                       if (virtAddr != nullptr) {
99                           SampleSaveCapture(virtAddr, buffer->GetSize());
100                       }
101                       surface->ReleaseBuffer(buffer);
102                   }
103                   delete surface;
104               }
105               delete &fc;
106           }
107       }
108   };
109   ```
110
1113. 实现相机状态回调的派生类,自定义相机状态发生变化(配置成功/失败,创建成功/失败)时的操作。
112
113   ```
114   class SampleCameraStateMng : public CameraStateCallback {
115   public:
116       SampleCameraStateMng() = delete;
117       SampleCameraStateMng(EventHandler &eventHdlr) : eventHdlr_(eventHdlr) {}
118       ~SampleCameraStateMng()
119       {
120           if (recordFd_ != -1) {
121               close(recordFd_);
122           }
123       }
124       void OnCreated(Camera &c) override
125       {
126           cout << "Sample recv OnCreate camera." << endl;
127           auto config = CameraConfig::CreateCameraConfig();
128           config->SetFrameStateCallback(&fsCb_, &eventHdlr_);
129           c.Configure(*config);
130           cam_ = &c;
131       }
132       void OnCreateFailed(const std::string cameraId, int32_t errorCode) override {}
133       void OnReleased(Camera &c) override {}
134   };
135   ```
136
1374. 创建CameraKit,用于创建和获取camera信息。
138
139   ```
140   CameraKit *camKit = CameraKit::GetInstance();
141   list<string> camList = camKit->GetCameraIds();
142   string camId;
143   for (auto &cam : camList) {
144       cout << "camera name:" << cam << endl;
145       const CameraAbility *ability = camKit->GetCameraAbility(cam);
146       /* find camera which fits user's ability */
147       list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
148       if (find(sizeList.begin(), sizeList.end(), CAM_PIC_1080P) != sizeList.end()) {
149           camId = cam;
150           break;
151       }
152   }
153   ```
154
1555. 创建Camera实例。
156
157   ```
158   EventHandler eventHdlr; // Create a thread to handle callback events
159   SampleCameraStateMng CamStateMng(eventHdlr);
160
161   camKit->CreateCamera(camId, CamStateMng, eventHdlr);
162   ```
163
1646. 根据步骤1、步骤2、步骤3中的回调设计,同步等待 OnCreated 回调拿到 cam_ 之后,进行相关操作。
165
166   ```
167   void OnCreated(Camera &c) override
168   {
169       cout << "Sample recv OnCreate camera." << endl;
170       auto config = CameraConfig::CreateCameraConfig();
171       config->SetFrameStateCallback(&fsCb_, &eventHdlr_);
172       c.Configure(*config);
173       cam_ = &c;
174   }
175
176   void Capture()
177   {
178       if (cam_ == nullptr) {
179           cout << "Camera is not ready." << endl;
180           return;
181       }
182       FrameConfig *fc = new FrameConfig(FRAME_CONFIG_CAPTURE);
183       Surface *surface = Surface::CreateSurface();
184       if (surface == nullptr) {
185           delete fc;
186           return;
187       }
188       surface->SetWidthAndHeight(1920, 1080); /* 1920:width,1080:height */
189       fc->AddSurface(*surface);
190       cam_->TriggerSingleCapture(*fc);
191   }
192   ```
193