• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# High-Performance Photo Capture (for System Applications Only) (ArkTS)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8As 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.
9
10To develop high-performance photo capture, perform the following steps:
11
12- Check whether the device supports deferred photo delivery of a certain type.
13- Enable deferred photo delivery (if supported).
14- Listen for thumbnails, obtain a thumbnail proxy class object, and save the thumbnail to the mediaLibrary.
15
16> **NOTE**
17>
18> - 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.
19> - Deferred photo delivery must be enabled during the stream configuration. After the stream configuration is complete, enabling deferred photo delivery does not take effect.
20
21
22
23## How to Develop
24
25Read [Module Description](../../reference/apis-camera-kit/arkts-apis-camera.md) for the API reference.
26
271. Import dependencies. Specifically, import the camera, image, and mediaLibrary modules.
28
29   ```ts
30   import { camera } from '@kit.CameraKit';
31   import { image } from '@kit.ImageKit';
32   import { BusinessError } from '@kit.BasicServicesKit';
33   import { photoAccessHelper } from '@kit.MediaLibraryKit';
34   ```
35
362. Determine the photo output stream.
37
38   You can use the **photoProfiles** property of the [CameraOutputCapability](../../reference/apis-camera-kit/arkts-apis-camera-i.md#cameraoutputcapability) class to obtain the photo output streams supported by the device and use [createPhotoOutput](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#createphotooutput11) to create a photo output stream.
39
40   ```ts
41   function getPhotoOutput(cameraManager: camera.CameraManager,
42                           cameraOutputCapability: camera.CameraOutputCapability): 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 | undefined = undefined;
48     try {
49       photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]);
50     } catch (error) {
51       let err = error as BusinessError;
52       console.error(`Failed to createPhotoOutput. error: ${err}`);
53     }
54     return photoOutput;
55   }
56   ```
57
583. Check whether the device supports deferred photo delivery of a certain type.
59
60   ```ts
61   function isDeferredImageDeliverySupported(photoOutput: camera.PhotoOutput): boolean {
62     let isSupported: boolean = false;
63     if (photoOutput !== null) {
64       isSupported = photoOutput.isDeferredImageDeliverySupported(camera.DeferredDeliveryImageType.PHOTO);
65     }
66     console.info(`isDeferredImageDeliverySupported isSupported: ${isSupported}`);
67     return isSupported;
68   }
69   ```
70
714. Enable deferred photo delivery.
72
73   ```ts
74   function EnableDeferredPhotoAbility(photoOutput: camera.PhotoOutput): void {
75     photoOutput.deferImageDelivery(camera.DeferredDeliveryImageType.PHOTO);
76   }
77   ```
78
795. Check whether deferred photo delivery is enabled.
80
81   ```ts
82   function isDeferredImageDeliveryEnabled(photoOutput: camera.PhotoOutput): boolean {
83   	 let isEnabled: boolean = false;
84     if (photoOutput !== null) {
85   	   isEnabled = photoOutput.isDeferredImageDeliveryEnabled(camera.DeferredDeliveryImageType.PHOTO);
86     }
87     console.info(`isDeferredImageDeliveryEnabled isEnabled: ${isEnabled}`);
88     return isEnabled;
89   }
90   ```
91
926. Trigger photo capture. This procedure is the same as that in the common photo capture mode. For details, see [Photo Capture](camera-shooting.md).
93
94
95
96## Status Listening
97
981. Listen for thumbnails.
99
100   ```ts
101   function onPhotoOutputDeferredPhotoProxyAvailable(photoOutput: camera.PhotoOutput, context: Context): void {
102     photoOutput.on('deferredPhotoProxyAvailable', (err: BusinessError, proxyObj: camera.DeferredPhotoProxy): void => {
103       if (err) {
104         console.error(`deferredPhotoProxyAvailable error: ${err}.`);
105         return;
106       }
107       console.info('photoOutPutCallBack deferredPhotoProxyAvailable');
108       // Obtain the PixelMap of a thumbnail.
109       proxyObj.getThumbnail().then((thumbnail: image.PixelMap) => {
110         AppStorage.setOrCreate('proxyThumbnail', thumbnail);
111       });
112       // Call the mediaLibrary APIs to flush the thumbnail to the disk. See the code below.
113       saveDeferredPhoto(proxyObj, context);
114     });
115   }
116   ```
117
118
119
1202. Call the mediaLibrary APIs to flush the thumbnail to the disk.
121
122   For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
123
124   ```ts
125   async function saveDeferredPhoto(proxyObj: camera.DeferredPhotoProxy, context: Context) {
126     try {
127       // Create a photoAsset.
128       let accessHelper = photoAccessHelper.getPhotoAccessHelper(context);
129       let testFileName = 'testFile' + Date.now() + '.jpg';
130       let photoAsset = await accessHelper.createAsset(testFileName);
131       // Pass the thumbnail proxy class object to the media library.
132       let mediaRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
133       mediaRequest.addResource(photoAccessHelper.ResourceType.PHOTO_PROXY, proxyObj);
134       let res = await accessHelper.applyChanges(mediaRequest);
135       console.info('saveDeferredPhoto success.');
136     } catch (err) {
137       console.error(`Failed to saveDeferredPhoto. error: ${err}`);
138     }
139   }
140   ```
141