1e41f4b71Sopenharmony_ci# Using Performance Improvement Features (for System Applications Only) (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci​These features are involved in the processes of starting the camera device, configuring streams, and taking photos. This topic describes the three scenarios.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Deferred Stream Configuration
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciA 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>**.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciThe 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.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci![deferred-surface-scene](figures/deferred-surface-scene.png)
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciBefore 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.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciAfter 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.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci### Available APIs
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| API| Description|
24e41f4b71Sopenharmony_ci| ---- | ---- |
25e41f4b71Sopenharmony_ci| 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.|
26e41f4b71Sopenharmony_ci| 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.|
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci### Development Example
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciThe figure below shows the recommended API call process.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci![](figures/deferred-surface-sequence-diagram.png)
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci```ts
37e41f4b71Sopenharmony_ciimport { camera } from '@kit.CameraKit';
38e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ciasync function preview(baseContext: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, previewSurfaceId: string): Promise<void> {
41e41f4b71Sopenharmony_ci  const cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
42e41f4b71Sopenharmony_ci  const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo);
43e41f4b71Sopenharmony_ci  const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile);
44e41f4b71Sopenharmony_ci  const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile);
45e41f4b71Sopenharmony_ci  const session: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
46e41f4b71Sopenharmony_ci  session.beginConfig();
47e41f4b71Sopenharmony_ci  session.addInput(cameraInput);
48e41f4b71Sopenharmony_ci  session.addOutput(previewOutput);
49e41f4b71Sopenharmony_ci  session.addOutput(photoOutput);
50e41f4b71Sopenharmony_ci  await session.commitConfig();
51e41f4b71Sopenharmony_ci  await session.start();
52e41f4b71Sopenharmony_ci  previewOutput.addDeferredSurface(previewSurfaceId);
53e41f4b71Sopenharmony_ci}
54e41f4b71Sopenharmony_ci```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci## Quick Thumbnail
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciThe photographing performance depends on the algorithm processing speed. A complex algorithm chain provides better image effect while requiring longer processing time.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ciTo 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.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ciIn 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.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### Available APIs
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| API| Description|
69e41f4b71Sopenharmony_ci| ---- | ---- |
70e41f4b71Sopenharmony_ci| isQuickThumbnailSupported() : boolean | Checks whether the quick thumbnail feature is supported.|
71e41f4b71Sopenharmony_ci| enableQuickThumbnail(enabled:bool): void | Enables or disables the quick thumbnail feature.|
72e41f4b71Sopenharmony_ci| on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void | Listens for camera thumbnails.|
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci> **NOTE**
75e41f4b71Sopenharmony_ci>
76e41f4b71Sopenharmony_ci> - [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).
77e41f4b71Sopenharmony_ci> - **on()** takes effect after **enableQuickThumbnail(true)** is called.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci### Development Example
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ciThe figure below shows the recommended API call process.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci![](figures/quick-thumbnail-sequence-diagram.png)
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
86e41f4b71Sopenharmony_ci```ts
87e41f4b71Sopenharmony_ciimport { camera } from '@kit.CameraKit';
88e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
89e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
90e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ciasync function enableQuickThumbnail(baseContext: common.BaseContext, photoProfile: camera.Profile): Promise<void> {
93e41f4b71Sopenharmony_ci  let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
94e41f4b71Sopenharmony_ci  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
95e41f4b71Sopenharmony_ci  // Create a PhotoSession instance.
96e41f4b71Sopenharmony_ci  let photoSession: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
97e41f4b71Sopenharmony_ci  // Start configuration for the session.
98e41f4b71Sopenharmony_ci  photoSession.beginConfig();
99e41f4b71Sopenharmony_ci  // Add a CameraInput instance to the session.
100e41f4b71Sopenharmony_ci  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
101e41f4b71Sopenharmony_ci  cameraInput.open();
102e41f4b71Sopenharmony_ci  photoSession.addInput(cameraInput);
103e41f4b71Sopenharmony_ci  // Add a PhotoOutput instance to the session.
104e41f4b71Sopenharmony_ci  let photoOutPut: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile);
105e41f4b71Sopenharmony_ci  photoSession.addOutput(photoOutPut);
106e41f4b71Sopenharmony_ci  let isSupported: boolean = photoOutPut.isQuickThumbnailSupported();
107e41f4b71Sopenharmony_ci  if (isSupported) {
108e41f4b71Sopenharmony_ci    // Enable the quick thumbnail feature.
109e41f4b71Sopenharmony_ci    photoOutPut.enableQuickThumbnail(true);
110e41f4b71Sopenharmony_ci    photoOutPut.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => {
111e41f4b71Sopenharmony_ci      if (err || pixelMap === undefined) {
112e41f4b71Sopenharmony_ci        console.error('photoOutPut on thumbnail failed');
113e41f4b71Sopenharmony_ci        return;
114e41f4b71Sopenharmony_ci      }
115e41f4b71Sopenharmony_ci      // Display or save the PixelMap instance.
116e41f4b71Sopenharmony_ci      showOrSavePicture(pixelMap);
117e41f4b71Sopenharmony_ci    });
118e41f4b71Sopenharmony_ci  }
119e41f4b71Sopenharmony_ci}
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_cifunction showOrSavePicture(pixelMap: image.PixelMap): void {
122e41f4b71Sopenharmony_ci  //do something
123e41f4b71Sopenharmony_ci}
124e41f4b71Sopenharmony_ci```
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci## Prelaunch
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciGenerally, 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.
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci​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,
131e41f4b71Sopenharmony_cithe 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.
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci![prelaunch-scene](figures/prelaunch-scene.png)
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci### Available APIs
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference.
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci| API| Description|
140e41f4b71Sopenharmony_ci| ---- | ---- |
141e41f4b71Sopenharmony_ci| isPrelaunchSupported(camera: CameraDevice) : boolean |  Checks whether the camera supports prelaunch.|
142e41f4b71Sopenharmony_ci| setPrelaunchConfig(prelaunchConfig: PrelaunchConfig) : void | Sets the prelaunch parameters.|
143e41f4b71Sopenharmony_ci| prelaunch() : void | Prelaunches the camera. This API is called when a user touches the system camera icon to start the camera application.|
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci### Development Example
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ciThe figure below shows the recommended API call process.
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci![](figures/prelaunch-sequence-diagram.png)
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ciFor details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci- **Home screen**
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci  ```ts
156e41f4b71Sopenharmony_ci  import { camera } from '@kit.CameraKit';
157e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
158e41f4b71Sopenharmony_ci  import { common } from '@kit.AbilityKit';
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci  function preLaunch(baseContext: common.BaseContext): void {
161e41f4b71Sopenharmony_ci    let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
162e41f4b71Sopenharmony_ci    try {
163e41f4b71Sopenharmony_ci      cameraManager.prelaunch();
164e41f4b71Sopenharmony_ci    } catch (error) {
165e41f4b71Sopenharmony_ci      let err = error as BusinessError;
166e41f4b71Sopenharmony_ci      console.error(`catch error: Code: ${err.code}, message: ${err.message}`);
167e41f4b71Sopenharmony_ci    }
168e41f4b71Sopenharmony_ci  }
169e41f4b71Sopenharmony_ci  ```
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci- **Camera application**
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci  To use the prelaunch feature, the camera application must configure the **ohos.permission.CAMERA** permission.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci  For details about how to request and verify the permissions, see [Requesting User Authorization](../../security/AccessToken/request-user-authorization.md).
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci  ```ts
178e41f4b71Sopenharmony_ci  import { camera } from '@kit.CameraKit';
179e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
180e41f4b71Sopenharmony_ci  import { common } from '@kit.AbilityKit';
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci  function setPreLaunchConfig(baseContext: common.BaseContext): void {
183e41f4b71Sopenharmony_ci    let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
184e41f4b71Sopenharmony_ci    let cameras: Array<camera.CameraDevice> = [];
185e41f4b71Sopenharmony_ci    try {
186e41f4b71Sopenharmony_ci      cameras = cameraManager.getSupportedCameras();
187e41f4b71Sopenharmony_ci    } catch (error) {
188e41f4b71Sopenharmony_ci      let err = error as BusinessError;
189e41f4b71Sopenharmony_ci      console.error(`getSupportedCameras catch error: Code: ${err.code}, message: ${err.message}`);
190e41f4b71Sopenharmony_ci    }
191e41f4b71Sopenharmony_ci    if (cameras.length <= 0) {
192e41f4b71Sopenharmony_ci      return;
193e41f4b71Sopenharmony_ci    }
194e41f4b71Sopenharmony_ci    if(cameraManager.isPrelaunchSupported(cameras[0])) {
195e41f4b71Sopenharmony_ci      try {
196e41f4b71Sopenharmony_ci        cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]});
197e41f4b71Sopenharmony_ci      } catch (error) {
198e41f4b71Sopenharmony_ci        let err = error as BusinessError;
199e41f4b71Sopenharmony_ci        console.error(`setPrelaunchConfig catch error: Code: ${err.code}, message: ${err.message}`);
200e41f4b71Sopenharmony_ci      }
201e41f4b71Sopenharmony_ci    }
202e41f4b71Sopenharmony_ci  }
203e41f4b71Sopenharmony_ci  ```
204