1# Photo Capture (ArkTS) 2 3Photo capture 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-camera-kit/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 '@kit.ImageKit'; 13 import { camera } from '@kit.CameraKit'; 14 import { fileIo as fs } from '@kit.CoreFileKit'; 15 import { photoAccessHelper } from '@kit.MediaLibraryKit'; 16 import { BusinessError } from '@kit.BasicServicesKit'; 17 ``` 18 192. Create a photo output stream. 20 21 Obtain the photo output streams supported by the current device from **photoProfiles** in the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class, and then call [createPhotoOutput](../../reference/apis-camera-kit/js-apis-camera.md#createphotooutput11) to pass in a supported output stream and the surface ID obtained in step 1 to create a photo output stream. 22 23 ```ts 24 function getPhotoOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined { 25 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 26 if (!photoProfilesArray) { 27 console.error("createOutput photoProfilesArray == null || undefined"); 28 } 29 let photoOutput: camera.PhotoOutput | undefined = undefined; 30 try { 31 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 32 } catch (error) { 33 let err = error as BusinessError; 34 console.error(`Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); 35 } 36 return photoOutput; 37 } 38 ``` 39 403. Set the callback for the **'photoAvailable'** event and save the photo buffer as an image. 41 42 For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 43 44 To view the saved images and videos in Gallery, you must save them to the media library. For details, see [Creating a Media Asset Using SaveButton](../medialibrary/photoAccessHelper-savebutton.md). 45 46 Specifically, when [photoOutput.on('photoAvailable')](../../reference/apis-camera-kit/js-apis-camera.md#onphotoavailable11) is called and a buffer is obtained, the buffer must be stored in the security component to the media library. 47 ```ts 48 let context = getContext(this); 49 50 function setPhotoOutputCb(photoOutput: camera.PhotoOutput) { 51 // After the callback is set, call capture() of photoOutput to transfer the photo buffer back to the callback. 52 photoOutput.on('photoAvailable', (errCode: BusinessError, photo: camera.Photo): void => { 53 console.info('getPhoto start'); 54 console.info(`err: ${JSON.stringify(errCode)}`); 55 if (errCode || photo === undefined) { 56 console.error('getPhoto failed'); 57 return; 58 } 59 let imageObj: image.Image = photo.main; 60 imageObj.getComponent(image.ComponentType.JPEG, (errCode: BusinessError, component: image.Component): void => { 61 console.info('getComponent start'); 62 if (errCode || component === undefined) { 63 console.error('getComponent failed'); 64 return; 65 } 66 let buffer: ArrayBuffer; 67 if (component.byteBuffer) { 68 buffer = component.byteBuffer; 69 } else { 70 console.error('byteBuffer is null'); 71 return; 72 } 73 // To view the saved image and video resources in Gallery, use a security component to create media assets. 74 75 // After the buffer processing is complete, the buffer must be released. Otherwise, no buffer is available for subsequent photo capture. 76 imageObj.release(); 77 }); 78 }); 79 } 80 ``` 81 824. Set camera parameters. 83 84 You can set camera parameters to adjust photo capture functions, including the flash, zoom ratio, and focal length. 85 86 ```ts 87 function configuringSession(photoSession: camera.PhotoSession): void { 88 // Check whether the camera has flash. 89 let flashStatus: boolean = false; 90 try { 91 flashStatus = photoSession.hasFlash(); 92 } catch (error) { 93 let err = error as BusinessError; 94 console.error(`Failed to hasFlash. error: ${JSON.stringify(err)}`); 95 } 96 console.info(`Returned with the flash light support status: ${flashStatus}`); 97 if (flashStatus) { 98 // Check whether the auto flash mode is supported. 99 let flashModeStatus: boolean = false; 100 try { 101 let status: boolean = photoSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 102 flashModeStatus = status; 103 } catch (error) { 104 let err = error as BusinessError; 105 console.error(`Failed to check whether the flash mode is supported. error: ${JSON.stringify(err)}`); 106 } 107 if (flashModeStatus) { 108 // Set the flash mode to auto. 109 try { 110 photoSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 111 } catch (error) { 112 let err = error as BusinessError; 113 console.error(`Failed to set the flash mode. error: ${JSON.stringify(err)}`); 114 } 115 } 116 } 117 // Check whether the continuous auto focus is supported. 118 let focusModeStatus: boolean = false; 119 try { 120 let status: boolean = photoSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 121 focusModeStatus = status; 122 } catch (error) { 123 let err = error as BusinessError; 124 console.error(`Failed to check whether the focus mode is supported. error: ${JSON.stringify(err)}`); 125 } 126 if (focusModeStatus) { 127 // Set the focus mode to continuous auto focus. 128 try { 129 photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 130 } catch (error) { 131 let err = error as BusinessError; 132 console.error(`Failed to set the focus mode. error: ${JSON.stringify(err)}`); 133 } 134 } 135 // Obtain the zoom ratio range supported by the camera. 136 let zoomRatioRange: Array<number> = []; 137 try { 138 zoomRatioRange = photoSession.getZoomRatioRange(); 139 } catch (error) { 140 let err = error as BusinessError; 141 console.error(`Failed to get the zoom ratio range. error: ${JSON.stringify(err)}`); 142 } 143 if (zoomRatioRange.length <= 0 ) { 144 return; 145 } 146 // Set a zoom ratio. 147 try { 148 photoSession.setZoomRatio(zoomRatioRange[0]); 149 } catch (error) { 150 let err = error as BusinessError; 151 console.error(`Failed to set the zoom ratio value. error: ${JSON.stringify(err)}`); 152 } 153 } 154 ``` 155 1565. Trigger photo capture. 157 158 Call [capture](../../reference/apis-camera-kit/js-apis-camera.md#capture-2) 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 photo capture, and the second parameter is a callback function. 159 160 To obtain the photo rotation angle (specified by **rotation**), call [getPhotoRotation](../../reference/apis-camera-kit/js-apis-camera.md#getphotorotation12) in the [PhotoOutput](../../reference/apis-camera-kit/js-apis-camera.md#photooutput) class. 161 162 ```ts 163 function capture(captureLocation: camera.Location, photoOutput: camera.PhotoOutput): void { 164 let settings: camera.PhotoCaptureSetting = { 165 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // Set the photo quality to high. 166 rotation: camera.ImageRotation.ROTATION_0, // The photo rotation angle, camera.ImageRotation.ROTATION_0, is obtained through getPhotoRotation. 167 location: captureLocation, // Set the geolocation information of the photo. 168 mirror: false // Disable mirroring (disabled by default). 169 }; 170 photoOutput.capture(settings, (err: BusinessError) => { 171 if (err) { 172 console.error(`Failed to capture the photo. error: ${JSON.stringify(err)}`); 173 return; 174 } 175 console.info('Callback invoked to indicate the photo capture request success.'); 176 }); 177 } 178 ``` 179 180## Status Listening 181 182During 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. 183 184- Register the **'captureStart'** event to listen for photo capture start events. This event can be registered when a **PhotoOutput** instance is created and is triggered when the camera device starts photo capture. The capture ID is returned. 185 186 ```ts 187 function onPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 188 photoOutput.on('captureStartWithInfo', (err: BusinessError, captureStartInfo: camera.CaptureStartInfo) => { 189 if (err !== undefined && err.code !== 0) { 190 return; 191 } 192 console.info(`photo capture started, captureId : ${captureStartInfo.captureId}`); 193 }); 194 } 195 ``` 196 197- Register the **'captureEnd'** event to listen for photo capture end events. This event can be registered when a **PhotoOutput** instance is created and is triggered when the photo capture is complete. [CaptureEndInfo](../../reference/apis-camera-kit/js-apis-camera.md#captureendinfo) is returned. 198 199 ```ts 200 function onPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 201 photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { 202 if (err !== undefined && err.code !== 0) { 203 return; 204 } 205 console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`); 206 console.info(`frameCount : ${captureEndInfo.frameCount}`); 207 }); 208 } 209 ``` 210 211- Register the **'captureReady'** event to obtain the result of the next photo capture. This event can be registered when a **PhotoOutput** instance is created and is triggered when the camera device is ready for taking a photo. The information about the next photo capture is returned. 212 213 ```ts 214 function onPhotoOutputCaptureReady(photoOutput: camera.PhotoOutput): void { 215 photoOutput.on('captureReady', (err: BusinessError) => { 216 if (err !== undefined && err.code !== 0) { 217 return; 218 } 219 console.info(`photo capture ready`); 220 }); 221 } 222 ``` 223 224- 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 [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 225 226 ```ts 227 function onPhotoOutputError(photoOutput: camera.PhotoOutput): void { 228 photoOutput.on('error', (error: BusinessError) => { 229 console.error(`Photo output error code: ${error.code}`); 230 }); 231 } 232 ``` 233