1 # Using Performance Improvement Features (for System Applications Only) (ArkTS) 2 3 The 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 5 These 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 9 A 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 11 The 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 15 Before 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 17 After 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 21 Read [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 30 The figure below shows the recommended API call process. 31 32  33 34 For 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 37 import camera from '@ohos.multimedia.camera'; 38 import common from '@ohos.app.ability.common'; 39 40 async 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 58 The photographing performance depends on the algorithm processing speed. A complex algorithm chain provides better image effect while requiring longer processing time. 59 60 To 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 62 In 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 66 Read [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 81 The figure below shows the recommended API call process. 82 83  84 85 For 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 87 import camera from '@ohos.multimedia.camera'; 88 import { BusinessError } from '@ohos.base'; 89 import image from '@ohos.multimedia.image'; 90 import common from '@ohos.app.ability.common'; 91 92 async 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 121 function showOrSavePicture(pixelMap: image.PixelMap): void { 122 //do something 123 } 124 ``` 125 126 ## Prelaunch 127 128 Generally, 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 130 The 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, the 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. 131 132  133 134 ### Available APIs 135 136 Read [Camera](../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 137 138 | API| Description| 139 | ---- | ---- | 140 | isPrelaunchSupported(camera: CameraDevice) : boolean | Checks whether the camera supports prelaunch.| 141 | setPrelaunchConfig(prelaunchConfig: PrelaunchConfig) : void | Sets the prelaunch parameters.| 142 | prelaunch() : void | Prelaunches the camera. This API is called when a user touches the system camera icon to start the camera application.| 143 144 ### Development Example 145 146 The figure below shows the recommended API call process. 147 148  149 150 For details about how to obtain the context, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 151 152 - **Home screen** 153 154 ```ts 155 import camera from '@ohos.multimedia.camera'; 156 import { BusinessError } from '@ohos.base'; 157 import common from '@ohos.app.ability.common'; 158 159 function preLaunch(baseContext: common.BaseContext): void { 160 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 161 try { 162 cameraManager.prelaunch(); 163 } catch (error) { 164 let err = error as BusinessError; 165 console.error(`catch error: Code: ${err.code}, message: ${err.message}`); 166 } 167 } 168 ``` 169 170 - **Camera application** 171 172 To use the prelaunch feature, the camera application must configure the **ohos.permission.CAMERA** permission. 173 174 For details about how to request and verify the permissions, see [Requesting User Authorization](../security/AccessToken/request-user-authorization.md). 175 176 ```ts 177 import camera from '@ohos.multimedia.camera'; 178 import { BusinessError } from '@ohos.base'; 179 import common from '@ohos.app.ability.common'; 180 181 function setPreLaunchConfig(baseContext: common.BaseContext): void { 182 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 183 let cameras: Array<camera.CameraDevice> = []; 184 try { 185 cameras = cameraManager.getSupportedCameras(); 186 } catch (error) { 187 let err = error as BusinessError; 188 console.error(`getSupportedCameras catch error: Code: ${err.code}, message: ${err.message}`); 189 } 190 if (cameras.length <= 0) { 191 return; 192 } 193 if(cameraManager.isPrelaunchSupported(cameras[0])) { 194 try { 195 cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); 196 } catch (error) { 197 let err = error as BusinessError; 198 console.error(`setPrelaunchConfig catch error: Code: ${err.code}, message: ${err.message}`); 199 } 200 } 201 } 202 ``` 203