1# High-Performance Photo Capture Sample (for System Applications Only) (ArkTS) 2 3Before developing a camera application, request permissions by following the instructions provided in [Camera Development Preparations](camera-preparation.md). 4 5This topic provides sample code that covers the complete high-performance photo capture process to help you understand the complete API calling sequence. 6 7Before referring to the sample code, you are advised to read [High-Performance Photo Capture (for System Applications Only) (ArkTS)](camera-deferred-photo.md), [Device Input Management](camera-device-input.md), [Camera Session Management](camera-session-management.md), and [Photo Capture](camera-shooting.md). 8 9## Development Process 10 11After obtaining the output stream capabilities supported by the camera, create a photo stream. The development process is as follows: 12 13 14 15## Sample Code 16 17For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 18 19```ts 20import { camera } from '@kit.CameraKit'; 21import { image } from '@kit.ImageKit'; 22import { BusinessError } from '@kit.BasicServicesKit'; 23import { common } from '@kit.AbilityKit'; 24import { fileIo as fs } from '@kit.CoreFileKit'; 25import { photoAccessHelper } from '@kit.MediaLibraryKit'; 26 27let context = getContext(this); 28 29// Flush the original image in write-file mode. 30async function savePicture(photoObj: camera.Photo): Promise<void> { 31 let accessHelper = photoAccessHelper.getPhotoAccessHelper(context); 32 let testFileName = 'testFile' + Date.now() + '.jpg'; 33 // To call createAsset(), the application must have the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. 34 let photoAsset = await accessHelper.createAsset(testFileName); 35 const fd = await photoAsset.open('rw'); 36 let buffer: ArrayBuffer | undefined = undefined; 37 photoObj.main.getComponent(image.ComponentType.JPEG, (errCode: BusinessError, component: image.Component): void => { 38 if (errCode || component === undefined) { 39 console.error('getComponent failed'); 40 return; 41 } 42 if (component.byteBuffer) { 43 buffer = component.byteBuffer; 44 } else { 45 console.error('byteBuffer is null'); 46 return; 47 } 48 }); 49 if (buffer) { 50 await fs.write(fd, buffer); 51 } 52 await photoAsset.close(fd); 53 await photoObj.release(); 54} 55 56// Flush the thumbnail by calling the media library API. 57async function saveDeferredPhoto(proxyObj: camera.DeferredPhotoProxy): Promise<void> { 58 try { 59 // Create a photoAsset. 60 let accessHelper = photoAccessHelper.getPhotoAccessHelper(context); 61 let testFileName = 'testFile' + Date.now() + '.jpg'; 62 let photoAsset = await accessHelper.createAsset(testFileName); 63 // Pass the thumbnail proxy class object to the media library. 64 let mediaRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset); 65 mediaRequest.addResource(photoAccessHelper.ResourceType.PHOTO_PROXY, proxyObj); 66 let res = await accessHelper.applyChanges(mediaRequest); 67 console.info('saveDeferredPhoto success.'); 68 } catch (err) { 69 console.error(`Failed to saveDeferredPhoto. error: ${JSON.stringify(err)}`); 70 } 71} 72 73async function deferredPhotoCase(baseContext: common.BaseContext, surfaceId: string): Promise<void> { 74 // Create a CameraManager object. 75 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 76 if (!cameraManager) { 77 console.error("camera.getCameraManager error"); 78 return; 79 } 80 // Listen for camera status changes. 81 cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { 82 if (err !== undefined && err.code !== 0) { 83 console.error(`cameraStatus with errorCode: ${err.code}`); 84 return; 85 } 86 console.info(`camera : ${cameraStatusInfo.camera.cameraId}`); 87 console.info(`status: ${cameraStatusInfo.status}`); 88 }); 89 90 // Obtain the camera list. 91 let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 92 if (cameraArray.length <= 0) { 93 console.error("cameraManager.getSupportedCameras error"); 94 return; 95 } 96 97 for (let index = 0; index < cameraArray.length; index++) { 98 console.info('cameraId : ' + cameraArray[index].cameraId); // Obtain the camera ID. 99 console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // Obtain the camera position. 100 console.info('cameraType : ' + cameraArray[index].cameraType); // Obtain the camera type. 101 console.info('connectionType : ' + cameraArray[index].connectionType); // Obtain the camera connection type. 102 } 103 104 // Create a camera input stream. 105 let cameraInput: camera.CameraInput | undefined = undefined; 106 try { 107 cameraInput = cameraManager.createCameraInput(cameraArray[0]); 108 } catch (error) { 109 let err = error as BusinessError; 110 console.error('Failed to createCameraInput errorCode = ' + err.code); 111 } 112 if (cameraInput === undefined) { 113 return; 114 } 115 116 // Listen for camera input errors. 117 let cameraDevice: camera.CameraDevice = cameraArray[0]; 118 cameraInput.on('error', cameraDevice, (error: BusinessError) => { 119 console.error(`Camera input error code: ${error.code}`); 120 }) 121 122 // Open a camera. 123 await cameraInput.open(); 124 125 // Obtain the supported modes. 126 let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraArray[0]); 127 let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0; 128 if (!isSupportPhotoMode) { 129 console.error('photo mode not support'); 130 return; 131 } 132 // Obtain the output streams supported by the camera. 133 let cameraOutputCap: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0], camera.SceneMode.NORMAL_PHOTO); 134 if (!cameraOutputCap) { 135 console.error("cameraManager.getSupportedOutputCapability error"); 136 return; 137 } 138 console.info("outputCapability: " + JSON.stringify(cameraOutputCap)); 139 140 let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles; 141 if (!previewProfilesArray) { 142 console.error("createOutput previewProfilesArray == null || undefined"); 143 } 144 145 let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles; 146 if (!photoProfilesArray) { 147 console.error("createOutput photoProfilesArray == null || undefined"); 148 } 149 150 // Create a preview output stream. For details about the surfaceId parameter, see the XComponent. The preview stream uses the surface provided by the XComponent. 151 let previewOutput: camera.PreviewOutput | undefined = undefined; 152 try { 153 previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId); 154 } catch (error) { 155 let err = error as BusinessError; 156 console.error(`Failed to create the PreviewOutput instance. error code: ${err.code}`); 157 } 158 if (previewOutput === undefined) { 159 return; 160 } 161 // Listen for preview output errors. 162 previewOutput.on('error', (error: BusinessError) => { 163 console.error(`Preview output error code: ${error.code}`); 164 }); 165 // Create a photo output stream. 166 let photoOutput: camera.PhotoOutput | undefined = undefined; 167 try { 168 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 169 } catch (error) { 170 let err = error as BusinessError; 171 console.error('Failed to createPhotoOutput errorCode = ' + err.code); 172 } 173 if (photoOutput === undefined) { 174 return; 175 } 176 // Create a session. 177 let photoSession: camera.PhotoSession | undefined = undefined; 178 try { 179 photoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 180 } catch (error) { 181 let err = error as BusinessError; 182 console.error('Failed to create the photoSession instance. errorCode = ' + err.code); 183 } 184 if (photoSession === undefined) { 185 return; 186 } 187 // Listen for session errors. 188 photoSession.on('error', (error: BusinessError) => { 189 console.error(`Capture session error code: ${error.code}`); 190 }); 191 192 // Start configuration for the session. 193 try { 194 photoSession.beginConfig(); 195 } catch (error) { 196 let err = error as BusinessError; 197 console.error('Failed to beginConfig. errorCode = ' + err.code); 198 } 199 200 // Add the camera input stream to the session. 201 try { 202 photoSession.addInput(cameraInput); 203 } catch (error) { 204 let err = error as BusinessError; 205 console.error('Failed to addInput. errorCode = ' + err.code); 206 } 207 208 // Add the preview output stream to the session. 209 try { 210 photoSession.addOutput(previewOutput); 211 } catch (error) { 212 let err = error as BusinessError; 213 console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code); 214 } 215 216 // Add the photo output stream to the session. 217 try { 218 photoSession.addOutput(photoOutput); 219 } catch (error) { 220 let err = error as BusinessError; 221 console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code); 222 } 223 224 // Register a callback to listen for original images. 225 photoOutput.on('photoAvailable', (err: BusinessError, photoObj: camera.Photo): void => { 226 if (err) { 227 console.info(`photoAvailable error: ${JSON.stringify(err)}.`); 228 return; 229 } 230 savePicture(photoObj).then(() => { 231 // Release the photo object after the flushing is complete. 232 photoObj.release(); 233 }); 234 }); 235 236 // Register a callback to listen for thumbnail proxies. 237 photoOutput.on('deferredPhotoProxyAvailable', (err: BusinessError, proxyObj: camera.DeferredPhotoProxy): void => { 238 if (err) { 239 console.info(`deferredPhotoProxyAvailable error: ${JSON.stringify(err)}.`); 240 return; 241 } 242 console.info('photoOutPutCallBack deferredPhotoProxyAvailable'); 243 // Obtain the pixel map of a thumbnail. 244 proxyObj.getThumbnail().then((thumbnail: image.PixelMap) => { 245 AppStorage.setOrCreate('proxyThumbnail', thumbnail); 246 }); 247 // Call the media library API to flush the thumbnail. 248 saveDeferredPhoto(proxyObj).then(() => { 249 // Release the thumbnail proxy class object after the flushing is complete. 250 proxyObj.release(); 251 }); 252 }); 253 254 // Check whether deferred photo delivery is supported. 255 let isSupportDeferred: boolean = photoOutput.isDeferredImageDeliverySupported(camera.DeferredDeliveryImageType.PHOTO); 256 console.info('isDeferredImageDeliverySupported res:' + isSupportDeferred); 257 if (isSupportDeferred) { 258 // Enable deferred photo delivery. 259 photoOutput.deferImageDelivery(camera.DeferredDeliveryImageType.PHOTO); 260 // Check whether deferred photo delivery is enabled. 261 let isSupportEnabled: boolean = photoOutput.isDeferredImageDeliveryEnabled(camera.DeferredDeliveryImageType.PHOTO); 262 console.info('isDeferredImageDeliveryEnabled res:' + isSupportEnabled); 263 } 264 265 // Commit the session configuration. 266 await photoSession.commitConfig(); 267 268 // Start the session. 269 await photoSession.start().then(() => { 270 console.info('Promise returned to indicate the session start success.'); 271 }); 272 // Check whether the camera has flash. 273 let flashStatus: boolean = false; 274 try { 275 flashStatus = photoSession.hasFlash(); 276 } catch (error) { 277 let err = error as BusinessError; 278 console.error('Failed to hasFlash. errorCode = ' + err.code); 279 } 280 console.info('Returned with the flash light support status:' + flashStatus); 281 282 if (flashStatus) { 283 // Check whether the auto flash mode is supported. 284 let flashModeStatus: boolean = false; 285 try { 286 let status: boolean = photoSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 287 flashModeStatus = status; 288 } catch (error) { 289 let err = error as BusinessError; 290 console.error('Failed to check whether the flash mode is supported. errorCode = ' + err.code); 291 } 292 if(flashModeStatus) { 293 // Set the flash mode to auto. 294 try { 295 photoSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 296 } catch (error) { 297 let err = error as BusinessError; 298 console.error('Failed to set the flash mode. errorCode = ' + err.code); 299 } 300 } 301 } 302 303 // Check whether the continuous auto focus is supported. 304 let focusModeStatus: boolean = false; 305 try { 306 let status: boolean = photoSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 307 focusModeStatus = status; 308 } catch (error) { 309 let err = error as BusinessError; 310 console.error('Failed to check whether the focus mode is supported. errorCode = ' + err.code); 311 } 312 313 if (focusModeStatus) { 314 // Set the focus mode to continuous auto focus. 315 try { 316 photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); 317 } catch (error) { 318 let err = error as BusinessError; 319 console.error('Failed to set the focus mode. errorCode = ' + err.code); 320 } 321 } 322 323 // Obtain the zoom ratio range supported by the camera. 324 let zoomRatioRange: Array<number> = []; 325 try { 326 zoomRatioRange = photoSession.getZoomRatioRange(); 327 } catch (error) { 328 let err = error as BusinessError; 329 console.error('Failed to get the zoom ratio range. errorCode = ' + err.code); 330 } 331 if (zoomRatioRange.length <= 0) { 332 return; 333 } 334 // Set a zoom ratio. 335 try { 336 photoSession.setZoomRatio(zoomRatioRange[0]); 337 } catch (error) { 338 let err = error as BusinessError; 339 console.error('Failed to set the zoom ratio value. errorCode = ' + err.code); 340 } 341 let photoCaptureSetting: camera.PhotoCaptureSetting = { 342 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // Set the photo quality to high. 343 rotation: camera.ImageRotation.ROTATION_0 // Set the rotation angle of the photo to 0. 344 } 345 // Use the current photo capture settings to take photos. 346 photoOutput.capture(photoCaptureSetting, (err: BusinessError) => { 347 if (err) { 348 console.error(`Failed to capture the photo ${err.message}`); 349 return; 350 } 351 console.info('Callback invoked to indicate the photo capture request success.'); 352 }); 353 354 // After the photo capture is complete, call the following APIs to close the camera and release the session. Do not release the session before the photo capture is complete. 355 // Stop the session. 356 await photoSession.stop(); 357 358 // Release the camera input stream. 359 await cameraInput.close(); 360 361 // Release the preview output stream. 362 await previewOutput.release(); 363 364 // Release the photo output stream. 365 await photoOutput.release(); 366 367 // Release the session. 368 await photoSession.release(); 369 370 // Set the session to null. 371 photoSession = undefined; 372} 373``` 374