1# Device Input Management (ArkTS) 2 3Before developing a camera application, you must create an independent camera object. The application invokes and controls the camera object to perform basic operations such as preview, photographing, and video recording. 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 '@ohos.multimedia.camera'; 13 import { BusinessError } from '@ohos.base'; 14 import common from '@ohos.app.ability.common'; 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 hardware 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** 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 524. Call [getSupportedOutputCapability](../reference/apis-camera-kit/js-apis-camera.md#getsupportedoutputcapability11) to obtain all output streams supported by the current device, such as preview streams and photo streams. The output streams supported are the value of each **profile** field under [CameraOutputCapability](../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability). 53 54 ```ts 55 async function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): Promise<camera.CameraOutputCapability | undefined> { 56 // Create a camera input stream. 57 let cameraInput: camera.CameraInput | undefined = undefined; 58 try { 59 cameraInput = cameraManager.createCameraInput(cameraDevice); 60 } catch (error) { 61 let err = error as BusinessError; 62 console.error('Failed to createCameraInput errorCode = ' + err.code); 63 } 64 if (cameraInput === undefined) { 65 return undefined; 66 } 67 // Listen for camera input errors. 68 cameraInput.on('error', cameraDevice, (error: BusinessError) => { 69 console.error(`Camera input error code: ${error.code}`); 70 }); 71 // Open the camera. 72 await cameraInput.open(); 73 // Obtain the output streams supported by the camera. 74 let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice, sceneMode); 75 if (!cameraOutputCapability) { 76 console.error("cameraManager.getSupportedOutputCapability error"); 77 return undefined; 78 } 79 console.info("outputCapability: " + JSON.stringify(cameraOutputCapability)); 80 return cameraOutputCapability; 81 } 82 ``` 83 84 85## Status Listening 86 87During 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. 88 89Register 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). 90 91```ts 92function onCameraStatus(cameraManager: camera.CameraManager): void { 93 cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { 94 console.info(`camera: ${cameraStatusInfo.camera.cameraId}`); 95 console.info(`status: ${cameraStatusInfo.status}`); 96 }); 97} 98``` 99