1# Device Input Management (ArkTS) 2 3Before developing a camera application, request permissions by following the instructions provided in [Camera Development Preparations](camera-preparation.md). 4 5A camera application invokes and controls a camera device to perform basic operations such as preview, photo capture, and video recording. 6 7## How to Develop 8 9Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 10 111. Import the camera module, which provides camera-related attributes and methods. 12 13 ```ts 14 import { camera } from '@kit.CameraKit'; 15 import { BusinessError } from '@kit.BasicServicesKit'; 16 ``` 17 18 > **NOTE** 19 > 20 > Before any camera device input, you must complete camera management by following the instructions provided in [Camera Device Management](camera-device-management.md). 21 222. Call [createCameraInput](../../reference/apis-camera-kit/js-apis-camera.md#createcamerainput) in the [cameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager) class to create a camera input stream. 23 24 ```ts 25 async function createInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): Promise<camera.CameraInput | undefined> { 26 // Create a camera input stream. 27 let cameraInput: camera.CameraInput | undefined = undefined; 28 try { 29 cameraInput = cameraManager.createCameraInput(cameraDevice); 30 } catch (error) { 31 let err = error as BusinessError; 32 console.error('Failed to createCameraInput errorCode = ' + err.code); 33 } 34 if (cameraInput === undefined) { 35 return undefined; 36 } 37 // Listen for camera input errors. 38 cameraInput.on('error', cameraDevice, (error: BusinessError) => { 39 console.error(`Camera input error code: ${error.code}`); 40 }); 41 // Open the camera. 42 await cameraInput.open(); 43 return cameraInput; 44 } 45 ``` 46 473. Call [getSupportedSceneModes](../../reference/apis-camera-kit/js-apis-camera.md#getsupportedscenemodes11) to obtain the list of scene modes supported by the current camera device. The list stores all the [SceneModes](../../reference/apis-camera-kit/js-apis-camera.md#scenemode11) supported by the camera device. 48 49 ```ts 50 function getSupportedSceneMode(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): Array<camera.SceneMode> { 51 // Obtain the list of scene modes supported by the camera device. 52 let sceneModeArray: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraDevice); 53 if (sceneModeArray != undefined && sceneModeArray.length > 0) { 54 for (let index = 0; index < sceneModeArray.length; index++) { 55 console.info('Camera SceneMode : ' + sceneModeArray[index]); 56 } 57 return sceneModeArray; 58 } else { 59 console.error("cameraManager.getSupportedSceneModes error"); 60 return []; 61 } 62 } 63 ``` 64 654. Call [getSupportedOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#getsupportedoutputcapability11) to obtain all output streams supported by the current camera device, such as preview streams, photo streams, and video streams. The supported output streams are listed in the **profile** field in [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability). Different types of output streams must be added based on the value of [SceneMode](../../reference/apis-camera-kit/js-apis-camera.md#scenemode11) specified by the camera device. 66 67 ```ts 68 async function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): Promise<camera.CameraOutputCapability | undefined> { 69 // Obtain the output streams supported by the camera device. 70 let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice, sceneMode); 71 if (!cameraOutputCapability) { 72 console.error("cameraManager.getSupportedOutputCapability error"); 73 return undefined; 74 } 75 console.info("outputCapability: " + JSON.stringify(cameraOutputCapability)); 76 // The following uses the NORMAL_PHOTO mode as an example. You need to add the preview stream and photo stream. 77 // previewProfiles is the preview output streams supported by the current camera device. 78 let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles; 79 if (!previewProfilesArray) { 80 console.error("createOutput previewProfilesArray == null || undefined"); 81 } 82 // photoProfiles is the photo output streams supported by the current camera device. 83 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 84 if (!photoProfilesArray) { 85 console.error("createOutput photoProfilesArray == null || undefined"); 86 } 87 return cameraOutputCapability; 88 } 89 ``` 90