• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Preview
2
3Preview is the image you see after you start the camera application but before you take photos or record videos.
4
5## How to Develop
6
7Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
8
91. Create a surface.
10
11     The XComponent, the capabilities of which are provided by the UI, offers the surface for preview streams. For details, see [XComponent](../reference/arkui-ts/ts-basic-components-xcomponent.md).
12
13   ```ts
14   // Create an XComponentController object.
15   mXComponentController: XComponentController = new XComponentController;
16   build() {
17       Flex() {
18           // Create an XComponent.
19           XComponent({
20               id: '',
21               type: 'surface',
22               libraryname: '',
23               controller: this.mXComponentController
24           })
25           .onLoad(() => {
26               // Set the surface width and height (1920 x 1080). For details about how to set the preview size, see the preview resolutions supported by the current device, which are obtained from previewProfilesArray.
27               this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080});
28               // Obtain the surface ID.
29               globalThis.surfaceId = this.mXComponentController.getXComponentSurfaceId();
30           })
31           .width('1920px')
32           .height('1080px')
33       }
34   }
35   ```
36
372. Call **previewProfiles()** in the **CameraOutputCapability** class to obtain the preview capabilities, in the format of an **previewProfilesArray** array, supported by the current device. Then call **createPreviewOutput()** to create a preview output stream, with the first parameter set to the first item in the **previewProfilesArray** array and the second parameter set to the surface ID obtained in step 1.
38
39   ```ts
40   let previewProfilesArray = cameraOutputCapability.previewProfiles;
41   let previewOutput;
42   try {
43       previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
44   }
45   catch (error) {
46       console.error("Failed to create the PreviewOutput instance." + error);
47   }
48   ```
49
503. Call **start()** to start outputting the preview stream. If the call fails, an error code is returned. For details, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
51
52   ```ts
53   previewOutput.start().then(() => {
54       console.info('Callback returned with previewOutput started.');
55   }).catch((err) => {
56       console.info('Failed to previewOutput start '+ err.code);
57   });
58   ```
59
60
61## Status Listening
62
63During camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors.
64
65- Register the 'frameStart' event to listen for preview start events This event can be registered when a **PreviewOutput** object is created and is triggered when the bottom layer starts exposure for the first time. The preview stream is started as long as a result is returned.
66
67  ```ts
68  previewOutput.on('frameStart', () => {
69      console.info('Preview frame started');
70  })
71  ```
72
73- Register the 'frameEnd' event to listen for preview end events. This event can be registered when a **PreviewOutput** object is created and is triggered when the last frame of preview ends. The preview stream ends as long as a result is returned.
74
75  ```ts
76  previewOutput.on('frameEnd', () => {
77      console.info('Preview frame ended');
78  })
79  ```
80
81- Register the 'error' event to listen for preview output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
82
83  ```ts
84  previewOutput.on('error', (previewOutputError) => {
85      console.info(`Preview output error code: ${previewOutputError.code}`);
86  })
87  ```
88