1# Camera Photographing 2 3Photographing is an important function of the camera application. Based on the complex logic of the camera hardware, the camera module provides APIs for you to set information such as resolution, flash, focal length, photo quality, and rotation angle. 4 5## How to Develop 6 7Read [Camera](../reference/apis/js-apis-camera.md) for the API reference. 8 91. Import the image module. The APIs provided by this module are used to obtain the surface ID and create a photo output stream. 10 11 ```ts 12 import image from '@ohos.multimedia.image'; 13 ``` 14 152. Obtain the surface ID. 16 17 Call **createImageReceiver()** of the image module to create an **ImageReceiver** instance, and use **getReceivingSurfaceId()** of the instance to obtain the surface ID, which is associated with the photo output stream to process the data output by the stream. 18 19 ```ts 20 function getImageReceiverSurfaceId() { 21 let receiver = image.createImageReceiver(640, 480, 4, 8); 22 console.info('before ImageReceiver check'); 23 if (receiver !== undefined) { 24 console.info('ImageReceiver is ok'); 25 let photoSurfaceId = receiver.getReceivingSurfaceId(); 26 console.info('ImageReceived id: ' + JSON.stringify(photoSurfaceId)); 27 } else { 28 console.info('ImageReceiver is not ok'); 29 } 30 } 31 ``` 32 333. Create a photo output stream. 34 35 Obtain the photo output streams supported by the current device from **photoProfiles** in **CameraOutputCapability**, and then call **createPhotoOutput()** to pass in a supported output stream and the surface ID obtained in step 1 to create a photo output stream. 36 37 ```ts 38 let photoProfilesArray = cameraOutputCapability.photoProfiles; 39 if (!photoProfilesArray) { 40 console.error("createOutput photoProfilesArray == null || undefined"); 41 } 42 let photoOutput; 43 try { 44 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId); 45 } catch (error) { 46 console.error('Failed to createPhotoOutput errorCode = ' + error.code); 47 } 48 ``` 49 504. Set camera parameters. 51 52 You can set camera parameters to adjust photographing functions, including the flash, zoom ratio, and focal length. 53 54 ```ts 55 // Check whether the camera has flash. 56 let flashStatus; 57 try { 58 flashStatus = captureSession.hasFlash(); 59 } catch (error) { 60 console.error('Failed to hasFlash. errorCode = ' + error.code); 61 } 62 console.info('Promise returned with the flash light support status:' + flashStatus); 63 if (flashStatus) { 64 // Check whether the auto flash mode is supported. 65 let flashModeStatus; 66 try { 67 let status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 68 flashModeStatus = status; 69 } catch (error) { 70 console.error('Failed to check whether the flash mode is supported. errorCode = ' + error.code); 71 } 72 if(flashModeStatus) { 73 // Set the flash mode to auto. 74 try { 75 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 76 } catch (error) { 77 console.error('Failed to set the flash mode. errorCode = ' + error.code); 78 } 79 } 80 } 81 // Check whether the continuous auto focus is supported. 82 let focusModeStatus; 83 try { 84 let status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 85 focusModeStatus = status; 86 } catch (error) { 87 console.error('Failed to check whether the focus mode is supported. errorCode = ' + error.code); 88 } 89 if (focusModeStatus) { 90 // Set the focus mode to continuous auto focus. 91 try { 92 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 93 } catch (error) { 94 console.error('Failed to set the focus mode. errorCode = ' + error.code); 95 } 96 } 97 // Obtain the zoom ratio range supported by the camera. 98 let zoomRatioRange; 99 try { 100 zoomRatioRange = captureSession.getZoomRatioRange(); 101 } catch (error) { 102 console.error('Failed to get the zoom ratio range. errorCode = ' + error.code); 103 } 104 // Set a zoom ratio. 105 try { 106 captureSession.setZoomRatio(zoomRatioRange[0]); 107 } catch (error) { 108 console.error('Failed to set the zoom ratio value. errorCode = ' + error.code); 109 } 110 ``` 111 1125. Trigger photographing. 113 114 Call **capture()** in the **PhotoOutput** class to capture a photo. In this API, the first parameter specifies the settings (for example, photo quality and rotation angle) for photographing, and the second parameter is a callback function. 115 116 ```ts 117 let settings = { 118 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // Set the photo quality to high. 119 rotation: camera.ImageRotation.ROTATION_0, // Set the rotation angle of the photo to 0. 120 location: captureLocation, // Set the geolocation information of the photo. 121 mirror: false // Disable mirroring (disabled by default). 122 }; 123 photoOutput.capture(settings, async (err) => { 124 if (err) { 125 console.error('Failed to capture the photo ${err.message}'); 126 return; 127 } 128 console.info('Callback invoked to indicate the photo capture request success.'); 129 }); 130 ``` 131 132## Status Listening 133 134During camera application development, you can listen for the status of the photo output stream, including the start of the photo stream, the start and end of the photo frame, and the errors of the photo output stream. 135 136- Register the 'captureStart' event to listen for photographing start events. This event can be registered when a **PhotoOutput** object is created and is triggered when the bottom layer starts exposure for photographing for the first time. The capture ID is returned. 137 138 ```ts 139 photoOutput.on('captureStart', (captureId) => { 140 console.info(`photo capture stated, captureId : ${captureId}`); 141 }) 142 ``` 143 144- Register the 'captureEnd' event to listen for photographing end events. This event can be registered when a **PhotoOutput** object is created and is triggered when the photographing is complete. [CaptureEndInfo](../reference/apis/js-apis-camera.md#captureendinfo) is returned. 145 146 ```ts 147 photoOutput.on('captureEnd', (captureEndInfo) => { 148 console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`); 149 console.info(`frameCount : ${captureEndInfo.frameCount}`); 150 }) 151 ``` 152 153- Register the 'error' event to listen for photo output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode). 154 155 ```ts 156 photoOutput.on('error', (error) => { 157 console.info(`Photo output error code: ${error.code}`); 158 }) 159 ``` 160