1# Interface (CameraInput) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8> **NOTE** 9> 10> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11 12CameraInput defines the camera input object. 13 14It provides camera device information used in [Session](arkts-apis-camera-Session.md). 15 16## Modules to Import 17 18```ts 19import { camera } from '@kit.CameraKit'; 20``` 21 22## open 23 24open(callback: AsyncCallback\<void\>): void 25 26Opens this camera device. This API uses an asynchronous callback to return the result. 27 28**Atomic service API**: This API can be used in atomic services since API version 19. 29 30**System capability**: SystemCapability.Multimedia.Camera.Core 31 32**Parameters** 33 34| Name | Type | Mandatory| Description | 35| -------- | -------------------- | ---- | ------------------- | 36| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 37 38**Error codes** 39 40For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 41 42| ID | Error Message | 43| --------------- | --------------- | 44| 7400107 | Can not use camera cause of conflict. | 45| 7400108 | Camera disabled cause of security reason. | 46| 7400201 | Camera service fatal error. | 47 48**Example** 49 50```ts 51import { BusinessError } from '@kit.BasicServicesKit'; 52 53function openCameraInput(cameraInput: camera.CameraInput): void { 54 cameraInput.open((err: BusinessError) => { 55 if (err) { 56 console.error(`Failed to open the camera, error code: ${err.code}.`); 57 return; 58 } 59 console.info('Callback returned with camera opened.'); 60 }); 61} 62``` 63 64## open 65 66open(): Promise\<void\> 67 68Opens this camera device. This API uses a promise to return the result. 69 70**Atomic service API**: This API can be used in atomic services since API version 19. 71 72**System capability**: SystemCapability.Multimedia.Camera.Core 73 74**Return value** 75 76| Type | Description | 77| -------------- | ----------------------- | 78| Promise\<void\> | Promise that returns no value.| 79 80**Error codes** 81 82For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 83 84| ID | Error Message | 85|---------|-------------------------------------------| 86| 7400102 | Operation not allowed. | 87| 7400107 | Can not use camera cause of conflict. | 88| 7400108 | Camera disabled cause of security reason. | 89| 7400201 | Camera service fatal error. | 90 91**Example** 92 93```ts 94import { BusinessError } from '@kit.BasicServicesKit'; 95 96function openCameraInput(cameraInput: camera.CameraInput): void { 97 cameraInput.open().then(() => { 98 console.info('Promise returned with camera opened.'); 99 }).catch((error: BusinessError) => { 100 console.error(`Failed to open the camera, error code: ${error.code}.`); 101 }); 102} 103``` 104 105## open<sup>12+</sup> 106 107open(isSecureEnabled: boolean): Promise\<bigint\> 108 109Opens this camera device and obtains the handle to the camera in secure mode. 110 111**Atomic service API**: This API can be used in atomic services since API version 19. 112 113**System capability**: SystemCapability.Multimedia.Camera.Core 114 115**Parameters** 116 117| Name | Type | Mandatory| Description | 118| -------- | -------------------- | ---- |-------------------------------------------------------------------------| 119| isSecureEnabled | boolean | Yes | Whether to open the camera device in secure mode. **true** to open in secure mode, **false** otherwise. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 120 121**Return value** 122 123| Type | Description | 124| -------------- | ----------------------- | 125| Promise\<bigint\> | Promise used to return the handle to the camera.| 126 127**Error codes** 128 129For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 130 131| ID | Error Message | 132| --------------- | --------------- | 133| 7400107 | Can not use camera cause of conflict. | 134| 7400108 | Camera disabled cause of security reason. | 135| 7400201 | Camera service fatal error. | 136 137**Example** 138 139```ts 140import { BusinessError } from '@kit.BasicServicesKit'; 141 142function openCameraInput(cameraInput: camera.CameraInput): void { 143 cameraInput.open(true).then(() => { 144 console.info('Promise returned with camera opened.'); 145 }).catch((error: BusinessError) => { 146 console.error(`Failed to open the camera, error code: ${error.code}.`); 147 }); 148} 149``` 150 151## open<sup>18+</sup> 152 153open(type: CameraConcurrentType): Promise\<void\> 154 155Opens the camera with the specified concurrency type. 156 157**Atomic service API**: This API can be used in atomic services since API version 19. 158 159**System capability**: SystemCapability.Multimedia.Camera.Core 160 161**Parameters** 162 163| Name | Type | Mandatory| Description | 164| -------- | -------------------- | ---- |-------------------------------------------------------------------------| 165| type | [CameraConcurrentType](arkts-apis-camera-e.md#cameraconcurrenttype18) | Yes | Concurrency type. If the API fails to be called, an error code is returned.| 166 167**Return value** 168 169| Type | Description | 170| -------------- | ----------------------- | 171| Promise\<void\> | Promise that returns no value.| 172 173**Error codes** 174 175For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 176 177| ID | Error Message | 178|---------|-------------------------------------------| 179| 7400102 | Operation not allowed. | 180| 7400107 | Can not use camera cause of conflict. | 181| 7400108 | Camera disabled cause of security reason. | 182| 7400201 | Camera service fatal error. | 183 184**Example** 185 186```ts 187import { BusinessError } from '@kit.BasicServicesKit'; 188 189function openCameraInput(cameraInput: camera.CameraInput): void { 190 cameraInput.open(0).then(() => { 191 console.info('Promise returned with camera opened.'); 192 }).catch((error: BusinessError) => { 193 console.error(`Failed to open the camera, error code: ${error.code}.`); 194 }); 195} 196``` 197 198## close 199 200close(callback: AsyncCallback\<void\>\): void 201 202Closes this camera device. This API uses an asynchronous callback to return the result. 203 204**Atomic service API**: This API can be used in atomic services since API version 19. 205 206**System capability**: SystemCapability.Multimedia.Camera.Core 207 208**Parameters** 209 210| Name | Type | Mandatory| Description | 211| -------- | -------------------- | ---- | -------------------- | 212| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 213 214**Error codes** 215 216For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 217 218| ID | Error Message | 219| --------------- | --------------- | 220| 7400201 | Camera service fatal error. | 221 222**Example** 223 224```ts 225import { BusinessError } from '@kit.BasicServicesKit'; 226 227function closeCameraInput(cameraInput: camera.CameraInput): void { 228 cameraInput.close((err: BusinessError) => { 229 if (err) { 230 console.error(`Failed to close the cameras, error code: ${err.code}.`); 231 return; 232 } 233 console.info('Callback returned with camera closed.'); 234 }); 235} 236``` 237 238## close 239 240close(): Promise\<void\> 241 242Closes this camera device. This API uses a promise to return the result. 243 244**Atomic service API**: This API can be used in atomic services since API version 19. 245 246**System capability**: SystemCapability.Multimedia.Camera.Core 247 248**Return value** 249 250| Type | Description | 251| -------------- | ----------------------- | 252| Promise\<void\> | Promise that returns no value.| 253 254**Error codes** 255 256For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 257 258| ID | Error Message | 259| --------------- | --------------- | 260| 7400201 | Camera service fatal error. | 261 262**Example** 263 264```ts 265import { BusinessError } from '@kit.BasicServicesKit'; 266 267function closeCameraInput(cameraInput: camera.CameraInput): void { 268 cameraInput.close().then(() => { 269 console.info('Promise returned with camera closed.'); 270 }).catch((error: BusinessError) => { 271 console.error(`Failed to close the cameras, error code: ${error.code}.`); 272 }); 273} 274``` 275 276## on('error') 277 278on(type: 'error', camera: CameraDevice, callback: ErrorCallback): void 279 280Subscribes to CameraInput error events. This API uses an asynchronous callback to return the result. 281 282> **NOTE** 283> 284> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**. 285 286**Atomic service API**: This API can be used in atomic services since API version 19. 287 288**System capability**: SystemCapability.Multimedia.Camera.Core 289 290**Parameters** 291 292| Name | Type | Mandatory| Description | 293| -------- | -------------------------------- | --- | ------------------------------------------- | 294| type | string | Yes | Event type. The value is fixed at **'error'**. The event can be listened for when a CameraInput instance is created. This event is triggered and the result is returned when an error occurs on the camera device. For example, if the camera device is unavailable or a conflict occurs, the error information is returned.| 295| camera | [CameraDevice](arkts-apis-camera-i.md#cameradevice) | Yes | Camera device.| 296| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | Yes | Callback used to return an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode). | 297 298**Example** 299 300```ts 301import { BusinessError } from '@kit.BasicServicesKit'; 302 303function callback(err: BusinessError): void { 304 console.error(`Camera input error code: ${err.code}`); 305} 306 307function registerCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void { 308 cameraInput.on('error', camera, callback); 309} 310``` 311 312## off('error') 313 314off(type: 'error', camera: CameraDevice, callback?: ErrorCallback): void 315 316Unsubscribes from CameraInput error events. 317 318**Atomic service API**: This API can be used in atomic services since API version 19. 319 320**System capability**: SystemCapability.Multimedia.Camera.Core 321 322**Parameters** 323 324| Name | Type | Mandatory| Description | 325| -------- | -------------------------------- | --- | ------------------------------------------- | 326| type | string | Yes | Event type. The value is fixed at **'error'**. The event can be listened for when a CameraInput instance is created. This event is triggered and the result is returned when an error occurs on the camera device. For example, if the camera device is unavailable or a conflict occurs, the error information is returned.| 327| camera | [CameraDevice](arkts-apis-camera-i.md#cameradevice) | Yes | Camera device.| 328| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.| 329 330**Example** 331 332```ts 333function unregisterCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void { 334 cameraInput.off('error', camera); 335} 336``` 337