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 import camera from '@ohos.multimedia.camera'; 14 import { BusinessError } from '@ohos.base'; 15 ``` 16 172. Obtain the surface ID. 18 19 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. 20 21 ```ts 22 async function getImageReceiverSurfaceId(): Promise<string | undefined> { 23 let photoSurfaceId: string | undefined = undefined; 24 let receiver: image.ImageReceiver = image.createImageReceiver(640, 480, 4, 8); 25 console.info('before ImageReceiver check'); 26 if (receiver !== undefined) { 27 console.info('ImageReceiver is ok'); 28 photoSurfaceId = await receiver.getReceivingSurfaceId(); 29 console.info(`ImageReceived id: ${JSON.stringify(photoSurfaceId)}`); 30 } else { 31 console.info('ImageReceiver is not ok'); 32 } 33 return photoSurfaceId; 34 } 35 ``` 36 373. Create a photo output stream. 38 39 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. 40 41 ```ts 42 function getPhotoOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability, photoSurfaceId: string): camera.PhotoOutput | undefined { 43 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 44 if (!photoProfilesArray) { 45 console.error("createOutput photoProfilesArray == null || undefined"); 46 } 47 let photoOutput: camera.PhotoOutput; 48 try { 49 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId); 50 } catch (error) { 51 let err = error as BusinessError; 52 console.error(`Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); 53 } 54 return photoOutput; 55 } 56 ``` 57 584. Set camera parameters. 59 60 You can set camera parameters to adjust photographing functions, including the flash, zoom ratio, and focal length. 61 62 ```ts 63 function configuringSession(captureSession: camera.CaptureSession): void { 64 // Check whether the camera has flash. 65 let flashStatus: boolean = false; 66 try { 67 flashStatus = captureSession.hasFlash(); 68 } catch (error) { 69 let err = error as BusinessError; 70 console.error(`Failed to hasFlash. error: ${JSON.stringify(err)}`); 71 } 72 console.info(`Promise returned with the flash light support status: ${flashStatus}`); 73 if (flashStatus) { 74 // Check whether the auto flash mode is supported. 75 let flashModeStatus: boolean = false; 76 try { 77 let status: boolean = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 78 flashModeStatus = status; 79 } catch (error) { 80 let err = error as BusinessError; 81 console.error(`Failed to check whether the flash mode is supported. error: ${JSON.stringify(err)}`); 82 } 83 if (flashModeStatus) { 84 // Set the flash mode to auto. 85 try { 86 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 87 } catch (error) { 88 let err = error as BusinessError; 89 console.error(`Failed to set the flash mode. error: ${JSON.stringify(err)}`); 90 } 91 } 92 } 93 // Check whether the continuous auto focus is supported. 94 let focusModeStatus: boolean = false; 95 try { 96 let status: boolean = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 97 focusModeStatus = status; 98 } catch (error) { 99 let err = error as BusinessError; 100 console.error(`Failed to check whether the focus mode is supported. error: ${JSON.stringify(err)}`); 101 } 102 if (focusModeStatus) { 103 // Set the focus mode to continuous auto focus. 104 try { 105 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 106 } catch (error) { 107 let err = error as BusinessError; 108 console.error(`Failed to set the focus mode. error: ${JSON.stringify(err)}`); 109 } 110 } 111 // Obtain the zoom ratio range supported by the camera. 112 let zoomRatioRange: Array<number> = []; 113 try { 114 zoomRatioRange = captureSession.getZoomRatioRange(); 115 } catch (error) { 116 let err = error as BusinessError; 117 console.error(`Failed to get the zoom ratio range. error: ${JSON.stringify(err)}`); 118 } 119 if (zoomRatioRange.length <= 0 ) { 120 return; 121 } 122 // Set a zoom ratio. 123 try { 124 captureSession.setZoomRatio(zoomRatioRange[0]); 125 } catch (error) { 126 let err = error as BusinessError; 127 console.error(`Failed to set the zoom ratio value. error: ${JSON.stringify(err)}`); 128 } 129 } 130 ``` 131 1325. Trigger photographing. 133 134 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. 135 136 ```ts 137 function capture(captureLocation: camera.Location, photoOutput: camera.PhotoOutput): void { 138 let settings: camera.PhotoCaptureSetting = { 139 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // Set the photo quality to high. 140 rotation: camera.ImageRotation.ROTATION_0, // Set the rotation angle of the photo to 0. 141 location: captureLocation, // Set the geolocation information of the photo. 142 mirror: false // Disable mirroring (disabled by default). 143 }; 144 photoOutput.capture(settings, (err: BusinessError) => { 145 if (err) { 146 console.error(`Failed to capture the photo. error: ${JSON.stringify(err)}`); 147 return; 148 } 149 console.info('Callback invoked to indicate the photo capture request success.'); 150 }); 151 } 152 ``` 153 154## Status Listening 155 156During 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. 157 158- 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. 159 160 ```ts 161 function onPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 162 photoOutput.on('captureStart', (err: BusinessError, captureId: number) => { 163 console.info(`photo capture stated, captureId : ${captureId}`); 164 }); 165 } 166 ``` 167 168- 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. 169 170 ```ts 171 function onPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 172 photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { 173 console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`); 174 console.info(`frameCount : ${captureEndInfo.frameCount}`); 175 }); 176 } 177 ``` 178 179- 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). 180 181 ```ts 182 function onPhotoOutputError(photoOutput: camera.PhotoOutput): void { 183 photoOutput.on('error', (error: BusinessError) => { 184 console.info(`Photo output error code: ${error.code}`); 185 }); 186 } 187 ``` 188