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