1# Camera Device Management (ArkTS)
2
3Before developing a camera application, you must call the camera APIs to create an independent camera device.
4
5## How to Develop
6
7Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference.
8
91. Import the camera module, which provides camera-related attributes and methods.
10
11   ```ts
12   import { camera } from '@kit.CameraKit';
13   import { BusinessError } from '@kit.BasicServicesKit';
14   import { common } from '@kit.AbilityKit';
15   ```
16
172. Call [getCameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameragetcameramanager) to obtain a **CameraManager** object.
18
19   For details about how to obtain the context, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
20
21   ```ts
22   function getCameraManager(context: common.BaseContext): camera.CameraManager {
23     let cameraManager: camera.CameraManager = camera.getCameraManager(context);
24     return cameraManager;
25   }
26   ```
27
28   > **NOTE**
29   >
30   > If obtaining the object fails, the camera device may be occupied or unusable. If it is occupied, wait until it is released.
31
323. Call [getSupportedCameras](../../reference/apis-camera-kit/js-apis-camera.md#getsupportedcameras) in the [cameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to obtain the list of cameras supported by the current device. The list stores the IDs of all cameras supported. If the list is not empty, each ID in the list can be used to create an independent camera object. If the list is empty, no camera is available for the current device and subsequent operations cannot be performed.
33
34   ```ts
35   function getCameraDevices(cameraManager: camera.CameraManager): Array<camera.CameraDevice> {
36     let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
37     if (cameraArray != undefined && cameraArray.length > 0) {
38       for (let index = 0; index < cameraArray.length; index++) {
39         console.info('cameraId : ' + cameraArray[index].cameraId);  // Obtain the camera ID.
40         console.info('cameraPosition : ' + cameraArray[index].cameraPosition);  // Obtain the camera position.
41         console.info('cameraType : ' + cameraArray[index].cameraType);  // Obtain the camera type.
42         console.info('connectionType : ' + cameraArray[index].connectionType);  // Obtain the camera connection type.
43       }
44       return cameraArray;
45     } else {
46       console.error("cameraManager.getSupportedCameras error");
47       return [];
48     }
49   }
50   ```
51
52
53## Status Listening
54
55During camera application development, you can listen for the camera status, including the appearance of a new camera, removal of a camera, and availability of a camera. The camera ID and camera status are included in the callback function. When a new camera appears, the new camera can be added to the supported camera list.
56
57Register the **'cameraStatus'** event and return the listening result through a callback, which carries the **CameraStatusInfo** parameter. For details about the parameter, see [CameraStatusInfo](../../reference/apis-camera-kit/js-apis-camera.md#camerastatusinfo).
58
59```ts
60function onCameraStatusChange(cameraManager: camera.CameraManager): void {
61  cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
62    if (err !== undefined && err.code !== 0) {
63      console.error(`Callback Error, errorCode: ${err.code}`);
64      return;
65    }
66    // If a camera is connected through a USB cable, the callback function returns the CAMERA_STATUS_APPEAR status.
67    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_APPEAR) {
68      console.info(`New Camera device appear.`);
69    }
70    // If the USB connection of the camera is interrupted, the callback function returns the CAMERA_STATUS_DISAPPEAR status.
71    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_DISAPPEAR) {
72      console.info(`Camera device has been removed.`);
73    }
74    // If the camera is closed, the callback function returns the CAMERA_STATUS_AVAILABLE status.
75    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_AVAILABLE) {
76      console.info(`Current Camera is available.`);
77    }
78    // If the camera is opened or occupied, the callback function returns the CAMERA_STATUS_UNAVAILABLE status.
79    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_UNAVAILABLE) {
80      console.info(`Current Camera has been occupied.`);
81    }
82    console.info(`camera: ${cameraStatusInfo.camera.cameraId}`);
83    console.info(`status: ${cameraStatusInfo.status}`);
84  });
85}
86```
87