1# Camera<a name="EN-US_TOPIC_0000001101564782"></a> 2 3- [Camera<a name="EN-US_TOPIC_0000001101564782"></a>](#camera) 4 - [Introduction<a name="section11660541593"></a>](#introduction) 5 - [Basic Concepts<a name="sectionbasicconcepts"></a>](#basic-concepts) 6 - [Directory Structure<a name="section176641621345"></a>](#directory-structure) 7 - [Usage Guidelines<a name="usage-guidelines"></a>](#usage-guidelines) 8 - [To Capture a Photo](#to-capture-a-photo) 9 - [To Start and Stop Preview](#to-start-and-stop-preview) 10 - [To Record Video](#to-record-video) 11 - [To Switch Between Camera Devices](#to-switch-between-camera-devices) 12 - [To configure flash mode](#to-configure-flash-mode) 13 - [Repositories Involved<a name="section16511040154318"></a>](#repositories-involved) 14 15## Introduction<a name="section11660541593"></a> 16 17The **camera\_standard** repository enables your application to provide camera functions. You can access and operate the camera device and develop new functions through open APIs. Common operations include preview, photographing and video recording. 18 19### Basic Concepts<a name="sectionbasicconcepts"></a> 20 21- Capture Photo 22 23 This function is used to capture a photo. 24 25- Preview 26 27 This function repeatedly captures multiple frames in the buffer once the camera is started. It is used to preview the subject before capturing a photo or start recording a video. 28 29- Record Video 30 31 This function repeatedly captures multiple frames in the buffer once the recording is started and continues till the recording is stopped. It is used for video recording. 32 33**Figure 1** Camera architecture<a name="fig310889397"></a> 34 35 36 37## Directory Structure<a name="section176641621345"></a> 38 39The structure of the repository directory is as follows: 40 41``` 42/foundation/multimedia/camera_framework # Camera code 43├── frameworks # Framework code 44│ ├── native # Internal Native API Implementation 45│ │ ├── camera # Camera Framework Implementation 46│ │ └── metadata # Metadata Implementation 47│ └── js # External JS API Implementation 48│ └── camera_napi # Camera NAPI Implementation 49├── interfaces # Interfaces 50│ ├── inner_api # Internal Native APIs 51│ └── kits # External JS APIs 52├── LICENSE # License file 53├── ohos.build # Build file 54├── sa_profile # Service configuration profile 55└── services # Service code 56 ├── camera_service # Camera Service Implementation 57 └── etc # Camera Service Init Configuration 58``` 59 60 61## Usage Guidelines<a name="usage-guidelines"></a> 62 63### To Capture a Photo 64 65Steps to capture a photo: 66 671. Create the buffer consumer listener(**CaptureSurfaceListener**) to save the image. 68 ``` 69 class CaptureSurfaceListener : public IBufferConsumerListener { 70 public: 71 int32_t mode_; 72 sptr<Surface> surface_; 73 74 void OnBufferAvailable() override 75 { 76 int32_t flushFence = 0; 77 int64_t timestamp = 0; 78 OHOS::Rect damage; // initialize the damage 79 80 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr; 81 surface_->AcquireBuffer(buffer, flushFence, timestamp, damage); 82 if (buffer != nullptr) { 83 void* addr = buffer->GetVirAddr(); 84 int32_t size = buffer->GetSize(); 85 86 // Save the buffer(addr) to a file. 87 88 surface_->ReleaseBuffer(buffer, -1); 89 } 90 } 91 }; 92 ``` 93 942. Get the Camera Manager Instance and retrieve the camera object list. 95 96 ``` 97 sptr<CameraManager> camManagerObj = CameraManager::GetInstance(); 98 std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras(); 99 ``` 100 1013. Create the Camera Input using the Camera Object to open the camera. 102 103 ``` 104 sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]); 105 ``` 106 1074. Create the Capture Session 108 109 ``` 110 sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession(); 111 ``` 112 1135. Begin configuring the Capture Session. 114 115 ``` 116 int32_t result = captureSession->BeginConfig(); 117 ``` 118 1196. Add the Camera Input to the Capture Session. 120 121 ``` 122 result = captureSession->AddInput(cameraInput); 123 ``` 124 1257. Create the Consumer Surface and register listerner for buffer updates. The photo width and height can be configured to the supported resolution which is 1280x960. 126 127 ``` 128 sptr<Surface> photoSurface = Surface::CreateSurfaceAsConsumer(); 129 int32_t photoWidth = 1280; 130 int32_t photoHeight = 960; 131 photoSurface->SetDefaultWidthAndHeight(photoWidth, photoHeight); 132 photoSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_JPEG)); 133 sptr<CaptureSurfaceListener> capturelistener = new(std::nothrow) CaptureSurfaceListener(); 134 capturelistener->mode_ = MODE_PHOTO; 135 capturelistener->surface_ = photoSurface; 136 photoSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)capturelistener); 137 ``` 138 1398. Create a Photo Output with Surface created above. 140 141 ``` 142 sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoSurface); 143 ``` 144 1459. Add the Photo Output to the Capture Session. 146 147 ``` 148 result = captureSession->AddOutput(photoOutput); 149 ``` 150 15110. Commit the configuration to Capture Session. 152 153 ``` 154 result = captureSession->CommitConfig(); 155 ``` 156 15711. Capture the Photo. 158 159 ``` 160 result = ((sptr<PhotoOutput> &)photoOutput)->Capture(); 161 ``` 162 16312. Release the Capture Session resources. 164 165 ``` 166 captureSession->Release(); 167 ``` 168 16913. Release the Camera Input to close the camera. 170 171 ``` 172 cameraInput->Release(); 173 ``` 174 175### To Start and Stop Preview 176 177Steps to start and stop preview: 178 1791. Get the Camera Manager Instance and retrieve the camera object list. 180 181 ``` 182 sptr<CameraManager> camManagerObj = CameraManager::GetInstance(); 183 std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras(); 184 ``` 185 1862. Create the Camera Input using the Camera Object to open the camera. 187 188 ``` 189 sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]); 190 ``` 191 1923. Create the Capture Session 193 194 ``` 195 sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession(); 196 ``` 197 1984. Begin configuring the Capture Session. 199 200 ``` 201 int32_t result = captureSession->BeginConfig(); 202 ``` 203 2045. Add the Camera Input to the Capture Session. 205 206 ``` 207 result = captureSession->AddInput(cameraInput); 208 ``` 209 2106. Create a Preview Output with Surface obtained from Window Manager to render on display. The preview width and Height can be configured to one of the camera supported resolutions. And to save to a file, can follow the step to create Surface and register listerner for buffer updates mentioned in capture photo. 211 212 ``` 213 int32_t previewWidth = 640; 214 int32_t previewHeight = 480; 215 previewSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP)); 216 sptr<CaptureOutput> previewOutput = camManagerObj->CreateCustomPreviewOutput(previewSurface, previewWidth, previewHeight); 217 ``` 218 2197. Add the Preview Output to the Capture Session. 220 221 ``` 222 result = captureSession->AddOutput(previewOutput); 223 ``` 224 2258. Commit the configuration to Capture Session. 226 227 ``` 228 result = captureSession->CommitConfig(); 229 ``` 230 2319. Start the Preview 232 233 ``` 234 result = captureSession->Start(); 235 ``` 236 23710. Stop the Preview when needed. 238 239 ``` 240 result = captureSession->Stop(); 241 ``` 242 24311. Release the Capture Session resources. 244 245 ``` 246 captureSession->Release(); 247 ``` 248 24912. Release the Camera Input to close the camera. 250 251 ``` 252 cameraInput->Release(); 253 ``` 254 255### To Record Video 256 257Steps to record Video: 258 2591. Get the Camera Manager Instance and retrieve the camera object list. 260 261 ``` 262 sptr<CameraManager> camManagerObj = CameraManager::GetInstance(); 263 std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras(); 264 ``` 265 2662. Create the Camera Input using the Camera Object to open the camera. 267 268 ``` 269 sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]); 270 ``` 271 2723. Create the Capture Session 273 274 ``` 275 sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession(); 276 ``` 277 2784. Begin configuring the Capture Session. 279 280 ``` 281 s int32_t result = captureSession->BeginConfig(); 282 ``` 283 2845. Add the Camera Input to the Capture Session. 285 286 ``` 287 result = captureSession->AddInput(cameraInput); 288 ``` 289 2906. Create a Video Output with Surface obtained from Recoder to MUX with audio and save the file. And to save just Video buffer to a file, can follow the step to create Surface and register listerner for buffer updates mentioned in capture photo. The video resolution can be configured to one of the camera and recorder supported resolutions while setting the recorder configurations. 291 292 ``` 293 videoSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP)); 294 sptr<CaptureOutput> videoOutput = camManagerObj->CreateVideoOutput(videoSurface); 295 ``` 296 2977. Add the Video Output to the Capture Session. 298 299 ``` 300 result = captureSession->AddOutput(videoOutput); 301 ``` 302 3038. Commit the configuration to Capture Session. 304 305 ``` 306 result = captureSession->CommitConfig(); 307 ``` 308 3099. Start the Video Recording. 310 311 ``` 312 result = ((sptr<VideoOutput> &)videoOutput)->Start(); 313 ``` 314 31510. Stop the recording when needed. 316 317 ``` 318 result = ((sptr<VideoOutput> &)videoOutput)->Stop(); 319 ``` 320 32111. Release the Capture Session resources. 322 323 ``` 324 captureSession->Release(); 325 ``` 326 32712. Release the Camera Input to close the camera. 328 329 ``` 330 cameraInput->Release(); 331 ``` 332 333### To Switch Between Camera Devices 334 335Below steps demonstrate how to switch between the camera devices. Initially a video output is added to the capture session. If user wish to switch between cameras, existing input and output have to be removed first and then add a new input and output(i.e., photo output in this case). 336 3371. Get the Camera Manager Instance and retrieve the camera object list. 338 339 ``` 340 sptr<CameraManager> camManagerObj = CameraManager::GetInstance(); 341 std::vector<sptr<CameraInfo>> cameraObjList = camManagerObj->GetCameras(); 342 ``` 343 3442. Create the Camera Input using the Camera Object to open the camera. 345 346 ``` 347 sptr<CaptureInput> cameraInput = camManagerObj->CreateCameraInput(cameraObjList[0]); 348 ``` 349 3503. Create the Capture Session 351 352 ``` 353 sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession(); 354 ``` 355 3564. Begin configuring the Capture Session. 357 358 ``` 359 int32_t result = captureSession->BeginConfig(); 360 ``` 361 3625. Add the Camera Input to the Capture Session. 363 364 ``` 365 result = captureSession->AddInput(cameraInput); 366 ``` 367 3686. Create a Video Output with Surface 369 370 ``` 371 sptr<CaptureOutput> videoOutput = camManagerObj->CreateVideoOutput(videoSurface); 372 ``` 373 3747. Add the Video Output to the Capture Session. 375 376 ``` 377 result = captureSession->AddOutput(videoOutput); 378 ``` 379 3808. Commit the configuration to Capture Session. 381 382 ``` 383 result = captureSession->CommitConfig(); 384 ``` 385 3869. Start the Video Recording. 387 388 ``` 389 result = ((sptr<VideoOutput> &)videoOutput)->Start(); 390 ``` 391 39210. Stop the recording when needed. 393 394 ``` 395 result = ((sptr<VideoOutput> &)videoOutput)->Stop(); 396 ``` 397 39811. In order to remove camera input and output, configure the Capture Session again. 399 400 ``` 401 int32_t result = captureSession->BeginConfig(); 402 ``` 403 40412. Remove the Camera Input in the new capture session configuration. 405 406 ``` 407 int32_t result = captureSession->RemoveInput(cameraInput); 408 ``` 409 41013. Remove the Camera Output as well. 411 412 ``` 413 int32_t result = captureSession->RemoveOutut(videoOutput); 414 ``` 415 41614. Create new camera input, add it to capture session 417 418 ``` 419 sptr<CaptureInput> cameraInput2 = camManagerObj->CreateCameraInput(cameraObjList[1]); 420 result = captureSession->AddInput(cameraInput2); 421 ``` 422 42315. Create the photo output and add the photo output to the capture session once it is successfully created. Create the Consumer Surface and register listerner for buffer updates prior to creation of photo output. The surface is used for creation of photo output. 424 425 ``` 426 // Get the surface 427 sptr<Surface> photoSurface = Surface::CreateSurfaceAsConsumer(); 428 int32_t photoWidth = 1280; 429 int32_t photoHeight = 960; 430 photoSurface->SetDefaultWidthAndHeight(photoWidth, photoHeight); 431 photoSurface->SetUserData(CameraManager::surfaceFormat, std::to_string(OHOS_CAMERA_FORMAT_JPEG)); 432 sptr<CaptureSurfaceListener> capturelistener = new(std::nothrow) CaptureSurfaceListener(); 433 capturelistener->mode_ = MODE_PHOTO; 434 capturelistener->surface_ = photoSurface; 435 photoSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)capturelistener); 436 437 // Create the Photo Output 438 sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoSurface); 439 440 // Add the output to the capture session 441 result = captureSession->AddOutput(photoOutput); 442 ``` 443 44416. Commit the configuration to Capture Session. 445 446 ``` 447 result = captureSession->CommitConfig(); 448 ``` 449 45017. Release the Camera Input that is removed from session. 451 452 ``` 453 cameraInput->Release(); 454 ``` 455 45618. Capture the Photo. 457 458 ``` 459 result = ((sptr<PhotoOutput> &)photoOutput)->Capture(); 460 ``` 461 462 46319. Release the Capture Session resources. 464 465 ``` 466 captureSession->Release(); 467 ``` 468 46920. Release the Camera Input to close the camera. 470 471 ``` 472 cameraInput2->Release(); 473 ``` 474 475### To configure flash mode 476 477Flash mode can be configured on the Camera Input before capturing a photo or recording a video. 478 4791. Set the flash mode for photo capture: 480 481 ``` 482 cameraInput->LockForControl(); 483 cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_OPEN); 484 cameraInput->UnlockForControl(); 485 ``` 4862. Set the flash mode for video recording: 487 488 ``` 489 cameraInput->LockForControl(); 490 cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN); 491 cameraInput->UnlockForControl(); 492 ``` 493 4943. Turnoff the flash: 495 496 ``` 497 cameraInput->LockForControl(); 498 cameraInput->SetFlashMode(OHOS_CAMERA_FLASH_MODE_CLOSE); 499 cameraInput->UnlockForControl(); 500 ``` 501 502## Repositories Involved<a name="section16511040154318"></a> 503 504[multimedia\_camera\_framework](https://gitee.com/openharmony/multimedia_camera_framework) 505 506