1# Using Performance Improvement Features (for System Applications Only) (ArkTS) 2 3The camera startup performance is affected by time-consuming operations such as power-on of underlying components and initialization of the process pipeline. To improve the camera startup speed and thumbnail display speed, OpenHarmony introduces some features. The capabilities of these features are related to underlying components. You need to check whether your underlying components support these capabilities before using the capabilities. 4 5These features are involved in the processes of starting the camera device, configuring streams, and taking photos. This topic describes the three scenarios. 6 7## Deferred Stream Configuration 8 9A typical camera startup process includes starting the camera device, configuring a data stream, and starting the data stream. Before configuring the data stream, you need to obtain the surface ID of the **\<XComponent>**. 10 11The deferred stream configuration feature decouples stream configuration and start from the surface. Before the **\<XComponent>** provides the surface for the camera application, the system configures and starts the stream. This way, the surface only needs to be available before the stream is started. This improves the startup speed and prevents the implementation of other startup optimization schemas from being affected. 12 13 14 15Before optimization: Stream configuration depends on a **Surface** object, which is available after UI loading is complete. In other words, you can create a session, configure input and output streams, and start the session only after the UI is loaded. The camera HDI is responsible for stream configuration. 16 17After optimization: Stream configuration does not depend on the **Surface** object. UI loading and stream configuration are executed concurrently. After the parameters are prepared, you can create a session. 18 19### Available APIs 20 21Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 22 23| API| Description| 24| ---- | ---- | 25| createDeferredPreviewOutput(profile: Profile): Promise\<PreviewOutput> | Creates a deferred **PreviewOutput** instance and adds it, instead of a common **PreviewOutput** instance, to the data stream during stream configuration.| 26| addDeferredSurface(surfaceId: string): Promise\<void> | Adds a surface for delayed preview. This API can run after [session.commitConfig](../../reference/apis-camera-kit/js-apis-camera.md#commitconfig11) or [session.start](../../reference/apis-camera-kit/js-apis-camera.md#start11) is called.| 27 28### Development Example 29 30The figure below shows the recommended API call process. 31 32 33 34For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 35 36```ts 37import { camera } from '@kit.CameraKit'; 38import { common } from '@kit.AbilityKit'; 39 40async function preview(baseContext: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, previewSurfaceId: string): Promise<void> { 41 const cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 42 const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo); 43 const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 44 const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile); 45 const session: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 46 session.beginConfig(); 47 session.addInput(cameraInput); 48 session.addOutput(previewOutput); 49 session.addOutput(photoOutput); 50 await session.commitConfig(); 51 await session.start(); 52 previewOutput.addDeferredSurface(previewSurfaceId); 53} 54``` 55 56## Quick Thumbnail 57 58The photographing performance depends on the algorithm processing speed. A complex algorithm chain provides better image effect while requiring longer processing time. 59 60To improve the photographing speed perceived by end users, the quick thumbnail feature is introduced. When the user takes a photo, a thumbnail is output and reported to the camera application for display before a real image is reported. 61 62In this way, the photographing process is optimized, which fulfills the processing requirements of the post-processing algorithm without blocking the photographing speed of the foreground. 63 64### Available APIs 65 66Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 67 68| API| Description| 69| ---- | ---- | 70| isQuickThumbnailSupported() : boolean | Checks whether the quick thumbnail feature is supported.| 71| enableQuickThumbnail(enabled:bool): void | Enables or disables the quick thumbnail feature.| 72| on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void | Listens for camera thumbnails.| 73 74> **NOTE** 75> 76> - [isQuickThumbnailSupported](../../reference/apis-camera-kit/js-apis-camera-sys.md#isquickthumbnailsupported) and [enableQuickThumbnail](../../reference/apis-camera-kit/js-apis-camera-sys.md#enablequickthumbnail) must be called after [addOutput](../../reference/apis-camera-kit/js-apis-camera.md#addoutput11) and [addInput](../../reference/apis-camera-kit/js-apis-camera.md#addinput11) but before [commitConfig](../../reference/apis-camera-kit/js-apis-camera.md#commitconfig11). 77> - **on()** takes effect after **enableQuickThumbnail(true)** is called. 78 79### Development Example 80 81The figure below shows the recommended API call process. 82 83 84 85For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 86```ts 87import { camera } from '@kit.CameraKit'; 88import { BusinessError } from '@kit.BasicServicesKit'; 89import { image } from '@kit.ImageKit'; 90import { common } from '@kit.AbilityKit'; 91 92async function enableQuickThumbnail(baseContext: common.BaseContext, photoProfile: camera.Profile): Promise<void> { 93 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 94 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 95 // Create a PhotoSession instance. 96 let photoSession: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 97 // Start configuration for the session. 98 photoSession.beginConfig(); 99 // Add a CameraInput instance to the session. 100 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 101 cameraInput.open(); 102 photoSession.addInput(cameraInput); 103 // Add a PhotoOutput instance to the session. 104 let photoOutPut: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile); 105 photoSession.addOutput(photoOutPut); 106 let isSupported: boolean = photoOutPut.isQuickThumbnailSupported(); 107 if (isSupported) { 108 // Enable the quick thumbnail feature. 109 photoOutPut.enableQuickThumbnail(true); 110 photoOutPut.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => { 111 if (err || pixelMap === undefined) { 112 console.error('photoOutPut on thumbnail failed'); 113 return; 114 } 115 // Display or save the PixelMap instance. 116 showOrSavePicture(pixelMap); 117 }); 118 } 119} 120 121function showOrSavePicture(pixelMap: image.PixelMap): void { 122 //do something 123} 124``` 125 126## Prelaunch 127 128Generally, the startup of the camera application is triggered when the user touches the camera icon on the home screen. The home screen senses the touch event and instructs the application manager to start the camera application. This takes a relatively long time. After the camera application is started, the camera startup process starts. A typical camera startup process includes starting the camera device, configuring a data stream, and starting the data stream, which is also time-consuming. 129 130The prelaunch feature triggers the action of starting the camera device before the camera application is started. In other words, when the user touches the camera icon on the home screen, 131the system starts the camera device. At this time, the camera application is not started yet. The figure below shows the camera application process before and after the prelaunch feature is introduced. 132 133 134 135### Available APIs 136 137Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 138 139| API| Description| 140| ---- | ---- | 141| isPrelaunchSupported(camera: CameraDevice) : boolean | Checks whether the camera supports prelaunch.| 142| setPrelaunchConfig(prelaunchConfig: PrelaunchConfig) : void | Sets the prelaunch parameters.| 143| prelaunch() : void | Prelaunches the camera. This API is called when a user touches the system camera icon to start the camera application.| 144 145### Development Example 146 147The figure below shows the recommended API call process. 148 149 150 151For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 152 153- **Home screen** 154 155 ```ts 156 import { camera } from '@kit.CameraKit'; 157 import { BusinessError } from '@kit.BasicServicesKit'; 158 import { common } from '@kit.AbilityKit'; 159 160 function preLaunch(baseContext: common.BaseContext): void { 161 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 162 try { 163 cameraManager.prelaunch(); 164 } catch (error) { 165 let err = error as BusinessError; 166 console.error(`catch error: Code: ${err.code}, message: ${err.message}`); 167 } 168 } 169 ``` 170 171- **Camera application** 172 173 To use the prelaunch feature, the camera application must configure the **ohos.permission.CAMERA** permission. 174 175 For details about how to request and verify the permissions, see [Requesting User Authorization](../../security/AccessToken/request-user-authorization.md). 176 177 ```ts 178 import { camera } from '@kit.CameraKit'; 179 import { BusinessError } from '@kit.BasicServicesKit'; 180 import { common } from '@kit.AbilityKit'; 181 182 function setPreLaunchConfig(baseContext: common.BaseContext): void { 183 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 184 let cameras: Array<camera.CameraDevice> = []; 185 try { 186 cameras = cameraManager.getSupportedCameras(); 187 } catch (error) { 188 let err = error as BusinessError; 189 console.error(`getSupportedCameras catch error: Code: ${err.code}, message: ${err.message}`); 190 } 191 if (cameras.length <= 0) { 192 return; 193 } 194 if(cameraManager.isPrelaunchSupported(cameras[0])) { 195 try { 196 cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); 197 } catch (error) { 198 let err = error as BusinessError; 199 console.error(`setPrelaunchConfig catch error: Code: ${err.code}, message: ${err.message}`); 200 } 201 } 202 } 203 ``` 204