1e41f4b71Sopenharmony_ci# Camera Preview (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciPreview is the image you see after you start the camera application but before you take photos or record videos. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci1. Import the camera module, which provides camera-related attributes and methods. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci ```ts 12e41f4b71Sopenharmony_ci import { camera } from '@kit.CameraKit'; 13e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 14e41f4b71Sopenharmony_ci ``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci2. Create a surface. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci The **\<XComponent>**, the capabilities of which are provided by the UI, offers the surface for preview streams. For details about how to obtain the surface ID, see [getXcomponentSurfaceId](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md#getxcomponentsurfaceid). For details about the component, see [XComponent](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci > **NOTE** 21e41f4b71Sopenharmony_ci > The preview stream and video output stream must have the same aspect ratio of the resolution. For example, the aspect ratio of the surface of the **\<XComponent>** is 1920:1080 (which is equal to 16:9), then the aspect ratio of the resolution of the preview stream must also be 16:9. This means that the resolution can be 640:360, 960:540, 1920:1080, or the like. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci3. Use **previewProfiles** in the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class to obtain the preview output capabilities, in the format of an **previewProfilesArray** array, supported by the current device. Then call [createPreviewOutput](../../reference/apis-camera-kit/js-apis-camera.md#createpreviewoutput) to create a **PreviewOutput** object, 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 2. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci ```ts 26e41f4b71Sopenharmony_ci function getPreviewOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability, surfaceId: string): camera.PreviewOutput | undefined { 27e41f4b71Sopenharmony_ci let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles; 28e41f4b71Sopenharmony_ci let previewOutput: camera.PreviewOutput | undefined = undefined; 29e41f4b71Sopenharmony_ci try { 30e41f4b71Sopenharmony_ci previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId); 31e41f4b71Sopenharmony_ci } catch (error) { 32e41f4b71Sopenharmony_ci let err = error as BusinessError; 33e41f4b71Sopenharmony_ci console.error("Failed to create the PreviewOutput instance. error code: " + err.code); 34e41f4b71Sopenharmony_ci } 35e41f4b71Sopenharmony_ci return previewOutput; 36e41f4b71Sopenharmony_ci } 37e41f4b71Sopenharmony_ci ``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci4. Call [Session.start](../../reference/apis-camera-kit/js-apis-camera.md#start11) to start outputting the preview stream. If the call fails, an error code is returned. For details, see [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```ts 42e41f4b71Sopenharmony_ci async function startPreviewOutput(cameraManager: camera.CameraManager, previewOutput: camera.PreviewOutput): Promise<void> { 43e41f4b71Sopenharmony_ci let cameraArray: Array<camera.CameraDevice> = []; 44e41f4b71Sopenharmony_ci cameraArray = cameraManager.getSupportedCameras(); 45e41f4b71Sopenharmony_ci if (cameraArray.length == 0) { 46e41f4b71Sopenharmony_ci console.error('no camera.'); 47e41f4b71Sopenharmony_ci return; 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci // Obtain the supported modes. 50e41f4b71Sopenharmony_ci let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraArray[0]); 51e41f4b71Sopenharmony_ci let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0; 52e41f4b71Sopenharmony_ci if (!isSupportPhotoMode) { 53e41f4b71Sopenharmony_ci console.error('photo mode not support'); 54e41f4b71Sopenharmony_ci return; 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci let cameraInput: camera.CameraInput | undefined = undefined; 57e41f4b71Sopenharmony_ci cameraInput = cameraManager.createCameraInput(cameraArray[0]); 58e41f4b71Sopenharmony_ci if (cameraInput === undefined) { 59e41f4b71Sopenharmony_ci console.error('cameraInput is undefined'); 60e41f4b71Sopenharmony_ci return; 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci // Open a camera. 63e41f4b71Sopenharmony_ci await cameraInput.open(); 64e41f4b71Sopenharmony_ci let session: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 65e41f4b71Sopenharmony_ci session.beginConfig(); 66e41f4b71Sopenharmony_ci session.addInput(cameraInput); 67e41f4b71Sopenharmony_ci session.addOutput(previewOutput); 68e41f4b71Sopenharmony_ci await session.commitConfig(); 69e41f4b71Sopenharmony_ci await session.start(); 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci ``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci## Status Listening 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci- 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 starts as long as a result is returned. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ```ts 81e41f4b71Sopenharmony_ci function onPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { 82e41f4b71Sopenharmony_ci previewOutput.on('frameStart', (err: BusinessError) => { 83e41f4b71Sopenharmony_ci if (err !== undefined && err.code !== 0) { 84e41f4b71Sopenharmony_ci return; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci console.info('Preview frame started'); 87e41f4b71Sopenharmony_ci }); 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci ``` 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci- 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. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci ```ts 94e41f4b71Sopenharmony_ci function onPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { 95e41f4b71Sopenharmony_ci previewOutput.on('frameEnd', (err: BusinessError) => { 96e41f4b71Sopenharmony_ci if (err !== undefined && err.code !== 0) { 97e41f4b71Sopenharmony_ci return; 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci console.info('Preview frame ended'); 100e41f4b71Sopenharmony_ci }); 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci ``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci- 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-camera-kit/js-apis-camera.md#cameraerrorcode). 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci ```ts 107e41f4b71Sopenharmony_ci function onPreviewOutputError(previewOutput: camera.PreviewOutput): void { 108e41f4b71Sopenharmony_ci previewOutput.on('error', (previewOutputError: BusinessError) => { 109e41f4b71Sopenharmony_ci console.error(`Preview output error code: ${previewOutputError.code}`); 110e41f4b71Sopenharmony_ci }); 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci ``` 113