1# High-Performance Camera Photographing (for System Applications Only) (ArkTS) 2 3As an important feature of the camera, high-performance photographing enables deferred delivery for photo capture and further reduces the response delay, delivering a better user experience. High-performance photographing is implemented as follows: After an application delivers a photographing 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 photographing, perform the following steps: 6 7- Check whether the device supports deferred delivery of a certain type. 8- Enable deferred 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 delivery varies according to the device and type. Therefore, if the device or type is changed, you must enable the corresponding deferred delivery capability. 14> - Deferred delivery must be enabled during the stream configuration. After the stream configuration is complete, enabling deferred delivery does not take effect. 15 16## How to Develop 17 18Read [Camera](../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 19 201. Import dependencies. Specifically, import the camera, image, and mediaLibrary modules. 21 22 ```ts 23 import camera from '@ohos.multimedia.camera'; 24 import image from '@ohos.multimedia.image'; 25 import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 26 import fs from '@ohos.file.fs'; 27 import photoAccessHelper from '@ohos.file.photoAccessHelper'; 28 import { BusinessError } from '@ohos.base'; 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 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 delivery. 67 68 ```ts 69 function EnableDeferredPhotoAbility(photoOutput: camera.PhotoOutput): void { 70 photoOutput.deferImageDelivery(camera.DeferredDeliveryImageType.PHOTO); 71 } 72 ``` 73 745. Check whether deferred 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 photographing. This procedure is the same as that in the common photographing mode. For details, see [Camera Photographing](camera-shooting.md). 88 89## Status Listening 90 911. Listen for thumbnails. 92 93 ```ts 94 function onPhotoOutputDeferredPhotoProxyAvailable(photoOutput: camera.PhotoOutput): void { 95 photoOutput.on('deferredPhotoProxyAvailable', (err: BusinessError, proxyObj: camera.DeferredPhotoProxy): void => { 96 if (err) { 97 console.info(`deferredPhotoProxyAvailable error: ${JSON.stringify(err)}.`); 98 return; 99 } 100 console.info('photoOutPutCallBack deferredPhotoProxyAvailable'); 101 // Obtain the pixel map of a thumbnail. 102 proxyObj.getThumbnail().then((thumbnail: image.PixelMap) => { 103 AppStorage.setOrCreate('proxyThumbnail', thumbnail); 104 }); 105 // Call the mediaLibrary APIs to flush the thumbnail to the disk. See the code below. 106 saveDeferredPhoto(proxyObj); 107 }); 108 } 109 ``` 110 111 112 1132. Call the mediaLibrary APIs to flush the thumbnail to the disk 114 115 For details about how to obtain the context, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 116 117 ```ts 118 let context = getContext(this); 119 120 async function saveDeferredPhoto(proxyObj: camera.DeferredPhotoProxy) { 121 try { 122 // Create a photoAsset. 123 let photoAccessHelper = PhotoAccessHelper.getPhotoAccessHelper(context); 124 let testFileName = 'testFile' + Date.now() + '.jpg'; 125 let photoAsset = await photoAccessHelper.createAsset(testFileName); 126 // Pass the thumbnail proxy class object to the mediaLibrary. 127 let mediaRequest: PhotoAccessHelper.MediaAssetChangeRequest = new PhotoAccessHelper.MediaAssetChangeRequest(photoAsset); 128 mediaRequest.addResource(PhotoAccessHelper.ResourceType.PHOTO_PROXY, proxyObj); 129 let res = await photoAccessHelper.applyChanges(mediaRequest); 130 console.info('saveDeferredPhoto success.'); 131 } catch (err) { 132 console.error(`Failed to saveDeferredPhoto. error: ${JSON.stringify(err)}`); 133 } 134 } 135 ``` 136