1# High-Performance Photo Capture (for System Applications Only) (ArkTS) 2 3As an important feature of the camera, high-performance photo capture enables deferred photo delivery and further reduces the response delay, delivering a better user experience. High-performance photo capture is implemented as follows: After an application delivers a phot capture request, the system quickly returns a thumbnail to the application, and the application stores the thumbnail and related information in the mediaLibrary. Then the subservice performs scheduling based on the system pressure and custom scenarios and sends the postprocessed original image to the mediaLibrary. 4 5To develop high-performance photo capture, perform the following steps: 6 7- Check whether the device supports deferred photo delivery of a certain type. 8- Enable deferred photo delivery (if supported). 9- Listen for thumbnails, obtain a thumbnail proxy class object, and save the thumbnail to the mediaLibrary. 10 11> **NOTE** 12> 13> - Deferred photo delivery varies according to the device and type. Therefore, if the device or type is changed, you must enable the corresponding deferred photo delivery capability. 14> - Deferred photo delivery must be enabled during the stream configuration. After the stream configuration is complete, enabling deferred photo delivery does not take effect. 15 16 17 18## How to Develop 19 20Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 21 221. Import dependencies. Specifically, import the camera, image, and mediaLibrary modules. 23 24 ```ts 25 import { camera } from '@kit.CameraKit'; 26 import { image } from '@kit.ImageKit'; 27 import { BusinessError } from '@kit.BasicServicesKit'; 28 import { photoAccessHelper } from '@kit.MediaLibraryKit'; 29 ``` 30 312. Determine the photo output stream. 32 33 You can use the **photoProfiles** attribute of the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class to obtain the photo output streams supported by the device and use [createPhotoOutput](../../reference/apis-camera-kit/js-apis-camera.md#createphotooutput11) to create a photo output stream. 34 35 ```ts 36 function getPhotoOutput(cameraManager: camera.CameraManager, 37 cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined { 38 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 39 if (!photoProfilesArray) { 40 console.error("createOutput photoProfilesArray == null || undefined"); 41 } 42 let photoOutput: camera.PhotoOutput | undefined = undefined; 43 try { 44 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 45 } catch (error) { 46 let err = error as BusinessError; 47 console.error(`Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); 48 } 49 return photoOutput; 50 } 51 ``` 52 533. Check whether the device supports deferred photo delivery of a certain type. 54 55 ```ts 56 function isDeferredImageDeliverySupported(photoOutput: camera.PhotoOutput): boolean { 57 let isSupported: boolean = false; 58 if (photoOutput !== null) { 59 isSupported = photoOutput.isDeferredImageDeliverySupported(camera.DeferredDeliveryImageType.PHOTO); 60 } 61 console.info(`isDeferredImageDeliverySupported isSupported: ${isSupported}`); 62 return isSupported; 63 } 64 ``` 65 664. Enable deferred photo delivery. 67 68 ```ts 69 function EnableDeferredPhotoAbility(photoOutput: camera.PhotoOutput): void { 70 photoOutput.deferImageDelivery(camera.DeferredDeliveryImageType.PHOTO); 71 } 72 ``` 73 745. Check whether deferred photo delivery is enabled. 75 76 ```ts 77 function isDeferredImageDeliveryEnabled(photoOutput: camera.PhotoOutput): boolean { 78 let isEnabled: boolean = false; 79 if (photoOutput !== null) { 80 isEnabled = photoOutput.isDeferredImageDeliveryEnabled(camera.DeferredDeliveryImageType.PHOTO); 81 } 82 console.info(`isDeferredImageDeliveryEnabled isEnabled: ${isEnabled}`); 83 return isEnabled; 84 } 85 ``` 86 876. Trigger photo capture. This procedure is the same as that in the common photo capture mode. For details, see [Photo Capture](camera-shooting.md). 88 89 90 91## Status Listening 92 931. Listen for thumbnails. 94 95 ```ts 96 function onPhotoOutputDeferredPhotoProxyAvailable(photoOutput: camera.PhotoOutput): void { 97 photoOutput.on('deferredPhotoProxyAvailable', (err: BusinessError, proxyObj: camera.DeferredPhotoProxy): void => { 98 if (err) { 99 console.info(`deferredPhotoProxyAvailable error: ${JSON.stringify(err)}.`); 100 return; 101 } 102 console.info('photoOutPutCallBack deferredPhotoProxyAvailable'); 103 // Obtain the pixel map of a thumbnail. 104 proxyObj.getThumbnail().then((thumbnail: image.PixelMap) => { 105 AppStorage.setOrCreate('proxyThumbnail', thumbnail); 106 }); 107 // Call the mediaLibrary APIs to flush the thumbnail to the disk. See the code below. 108 saveDeferredPhoto(proxyObj); 109 }); 110 } 111 ``` 112 113 114 1152. Call the mediaLibrary APIs to flush the thumbnail to the disk. 116 117 For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 118 119 ```ts 120 let context = getContext(this); 121 122 async function saveDeferredPhoto(proxyObj: camera.DeferredPhotoProxy) { 123 try { 124 // Create a photoAsset. 125 let accessHelper = photoAccessHelper.getPhotoAccessHelper(context); 126 let testFileName = 'testFile' + Date.now() + '.jpg'; 127 let photoAsset = await accessHelper.createAsset(testFileName); 128 // Pass the thumbnail proxy class object to the mediaLibrary. 129 let mediaRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset); 130 mediaRequest.addResource(photoAccessHelper.ResourceType.PHOTO_PROXY, proxyObj); 131 let res = await accessHelper.applyChanges(mediaRequest); 132 console.info('saveDeferredPhoto success.'); 133 } catch (err) { 134 console.error(`Failed to saveDeferredPhoto. error: ${JSON.stringify(err)}`); 135 } 136 } 137 ``` 138