1# Interface (CameraInput) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--Designer: @leo_ysl--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13相机设备输入对象。 14 15会话中[Session](arkts-apis-camera-Session.md)使用的相机信息。 16 17## 导入模块 18 19```ts 20import { camera } from '@kit.CameraKit'; 21``` 22 23## open 24 25open(callback: AsyncCallback\<void\>): void 26 27打开相机,通过注册回调函数获取状态。使用callback异步回调。 28 29**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 30 31**系统能力:** SystemCapability.Multimedia.Camera.Core 32 33**参数:** 34 35| 参数名 | 类型 | 必填 | 说明 | 36| -------- | -------------------- | ---- | ------------------- | 37| callback | AsyncCallback\<void\> | 是 | 回调函数。当打开相机成功,err为undefined,否则为错误对象,错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 38 39**错误码:** 40 41以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 42 43| 错误码ID | 错误信息 | 44| --------------- | --------------- | 45| 7400107 | Can not use camera cause of conflict. | 46| 7400108 | Camera disabled cause of security reason. | 47| 7400201 | Camera service fatal error. | 48 49**示例:** 50 51```ts 52import { BusinessError } from '@kit.BasicServicesKit'; 53 54function openCameraInput(cameraInput: camera.CameraInput): void { 55 cameraInput.open((err: BusinessError) => { 56 if (err) { 57 console.error(`Failed to open the camera, error code: ${err.code}.`); 58 return; 59 } 60 console.info('Callback returned with camera opened.'); 61 }); 62} 63``` 64 65## open 66 67open(): Promise\<void\> 68 69打开相机,使用Promise异步回调。 70 71**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 72 73**系统能力:** SystemCapability.Multimedia.Camera.Core 74 75**返回值:** 76 77| 类型 | 说明 | 78| -------------- | ----------------------- | 79| Promise\<void\> | Promise对象,无返回结果。 | 80 81**错误码:** 82 83以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 84 85| 错误码ID | 错误信息 | 86|---------|-------------------------------------------| 87| 7400102 | Operation not allowed. | 88| 7400107 | Can not use camera cause of conflict. | 89| 7400108 | Camera disabled cause of security reason. | 90| 7400201 | Camera service fatal error. | 91 92**示例:** 93 94```ts 95import { BusinessError } from '@kit.BasicServicesKit'; 96 97function openCameraInput(cameraInput: camera.CameraInput): void { 98 cameraInput.open().then(() => { 99 console.info('Promise returned with camera opened.'); 100 }).catch((error: BusinessError) => { 101 console.error(`Failed to open the camera, error code: ${error.code}.`); 102 }); 103} 104``` 105 106## open<sup>12+</sup> 107 108open(isSecureEnabled: boolean): Promise\<bigint\> 109 110打开相机。使用Promise异步回调。 111 112**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 113 114**系统能力:** SystemCapability.Multimedia.Camera.Core 115 116**参数:** 117 118| 参数名 | 类型 | 必填 | 说明 | 119| -------- | -------------------- | ---- |-------------------------------------------------------------------------| 120| isSecureEnabled | boolean | 是 | 设置true为使能以安全的方式打开相机,设置false则反之。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 121 122**返回值:** 123 124| 类型 | 说明 | 125| -------------- | ----------------------- | 126| Promise\<bigint\> | Promise对象,返回安全相机的句柄。 | 127 128**错误码:** 129 130以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 131 132| 错误码ID | 错误信息 | 133| --------------- | --------------- | 134| 7400107 | Can not use camera cause of conflict. | 135| 7400108 | Camera disabled cause of security reason. | 136| 7400201 | Camera service fatal error. | 137 138**示例:** 139 140```ts 141import { BusinessError } from '@kit.BasicServicesKit'; 142 143function openCameraInput(cameraInput: camera.CameraInput): void { 144 cameraInput.open(true).then(() => { 145 console.info('Promise returned with camera opened.'); 146 }).catch((error: BusinessError) => { 147 console.error(`Failed to open the camera, error code: ${error.code}.`); 148 }); 149} 150``` 151 152## open<sup>18+</sup> 153 154open(type: CameraConcurrentType): Promise\<void\> 155 156以指定的并发类型打开相机。使用Promise异步回调。 157 158**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 159 160**系统能力:** SystemCapability.Multimedia.Camera.Core 161 162**参数:** 163 164| 参数名 | 类型 | 必填 | 说明 | 165| -------- | -------------------- | ---- |-------------------------------------------------------------------------| 166| type | [CameraConcurrentType](arkts-apis-camera-e.md#cameraconcurrenttype18) | 是 | 以指定的并发类型打开相机。接口调用失败会返回相应错误码。| 167 168**返回值:** 169 170| 类型 | 说明 | 171| -------------- | ----------------------- | 172| Promise\<void\> | Promise对象,无返回结果。 | 173 174**错误码:** 175 176以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 177 178| 错误码ID | 错误信息 | 179|---------|-------------------------------------------| 180| 7400102 | Operation not allowed. | 181| 7400107 | Can not use camera cause of conflict. | 182| 7400108 | Camera disabled cause of security reason. | 183| 7400201 | Camera service fatal error. | 184 185**示例:** 186 187```ts 188import { BusinessError } from '@kit.BasicServicesKit'; 189 190function openCameraInput(cameraInput: camera.CameraInput): void { 191 cameraInput.open(0).then(() => { 192 console.info('Promise returned with camera opened.'); 193 }).catch((error: BusinessError) => { 194 console.error(`Failed to open the camera, error code: ${error.code}.`); 195 }); 196} 197``` 198 199## close 200 201close(callback: AsyncCallback\<void\>\): void 202 203关闭相机,通过注册回调函数获取状态。使用callback异步回调。 204 205**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 206 207**系统能力:** SystemCapability.Multimedia.Camera.Core 208 209**参数:** 210 211| 参数名 | 类型 | 必填 | 说明 | 212| -------- | -------------------- | ---- | -------------------- | 213| callback | AsyncCallback\<void\> | 是 | 回调函数。当关闭相机成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 214 215**错误码:** 216 217以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 218 219| 错误码ID | 错误信息 | 220| --------------- | --------------- | 221| 7400201 | Camera service fatal error. | 222 223**示例:** 224 225```ts 226import { BusinessError } from '@kit.BasicServicesKit'; 227 228function closeCameraInput(cameraInput: camera.CameraInput): void { 229 cameraInput.close((err: BusinessError) => { 230 if (err) { 231 console.error(`Failed to close the cameras, error code: ${err.code}.`); 232 return; 233 } 234 console.info('Callback returned with camera closed.'); 235 }); 236} 237``` 238 239## close 240 241close(): Promise\<void\> 242 243关闭相机,使用Promise异步回调。 244 245**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 246 247**系统能力:** SystemCapability.Multimedia.Camera.Core 248 249**返回值:** 250 251| 类型 | 说明 | 252| -------------- | ----------------------- | 253| Promise\<void\> | Promise对象,无返回结果。 | 254 255**错误码:** 256 257以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 258 259| 错误码ID | 错误信息 | 260| --------------- | --------------- | 261| 7400201 | Camera service fatal error. | 262 263**示例:** 264 265```ts 266import { BusinessError } from '@kit.BasicServicesKit'; 267 268function closeCameraInput(cameraInput: camera.CameraInput): void { 269 cameraInput.close().then(() => { 270 console.info('Promise returned with camera closed.'); 271 }).catch((error: BusinessError) => { 272 console.error(`Failed to close the cameras, error code: ${error.code}.`); 273 }); 274} 275``` 276 277## on('error') 278 279on(type: 'error', camera: CameraDevice, callback: ErrorCallback): void 280 281监听CameraInput的错误事件,通过注册回调函数获取结果。使用callback异步回调。 282 283> **说明:** 284> 285> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。 286 287**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 288 289**系统能力:** SystemCapability.Multimedia.Camera.Core 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| -------- | -------------------------------- | --- | ------------------------------------------- | 295| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如设备不可用或者冲突等返回对应错误信息。 | 296| camera | [CameraDevice](arkts-apis-camera-i.md#cameradevice) | 是 | CameraDevice对象。 | 297| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取结果。返回错误码,错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 298 299**示例:** 300 301```ts 302import { BusinessError } from '@kit.BasicServicesKit'; 303 304function callback(err: BusinessError): void { 305 console.error(`Camera input error code: ${err.code}`); 306} 307 308function registerCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void { 309 cameraInput.on('error', camera, callback); 310} 311``` 312 313## off('error') 314 315off(type: 'error', camera: CameraDevice, callback?: ErrorCallback): void 316 317注销监听CameraInput的错误事件。 318 319**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 320 321**系统能力:** SystemCapability.Multimedia.Camera.Core 322 323**参数:** 324 325| 参数名 | 类型 | 必填 | 说明 | 326| -------- | -------------------------------- | --- | ------------------------------------------- | 327| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如设备不可用或者冲突等返回对应错误信息。 | 328| camera | [CameraDevice](arkts-apis-camera-i.md#cameradevice) | 是 | CameraDevice对象。 | 329| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 | 330 331**示例:** 332 333```ts 334function unregisterCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void { 335 cameraInput.off('error', camera); 336} 337``` 338