1# Interface (Session) 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> - 本Interface首批接口从API version 11开始支持。 13 14会话类,保存一次相机运行所需要的所有资源[CameraInput](arkts-apis-camera-CameraInput.md)、[CameraOutput](arkts-apis-camera-CameraOutput.md),并向相机设备申请完成相机功能(录像,拍照)。 15 16## 导入模块 17 18```ts 19import { camera } from '@kit.CameraKit'; 20``` 21 22## beginConfig<sup>11+</sup> 23 24beginConfig(): void 25 26开始配置会话。 27 28**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 29 30**系统能力:** SystemCapability.Multimedia.Camera.Core 31 32**错误码:** 33 34以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 35 36| 错误码ID | 错误信息 | 37| --------------- | --------------- | 38| 7400105 | Session config locked. | 39| 7400201 | Camera service fatal error. | 40 41**示例:** 42 43```ts 44import { BusinessError } from '@kit.BasicServicesKit'; 45 46function beginConfig(session: camera.Session): void { 47 try { 48 session.beginConfig(); 49 } catch (error) { 50 // 失败返回错误码error.code并处理。 51 let err = error as BusinessError; 52 console.error(`The beginConfig call failed. error code: ${err.code}`); 53 } 54} 55``` 56 57## commitConfig<sup>11+</sup> 58 59commitConfig(callback: AsyncCallback\<void\>): void 60 61提交配置信息,通过注册回调函数获取结果。使用callback异步回调。 62 63**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 64 65**系统能力:** SystemCapability.Multimedia.Camera.Core 66 67**参数:** 68 69| 参数名 | 类型 | 必填 | 说明 | 70| -------- | -------------------- | ---- | -------------------- | 71| callback | AsyncCallback\<void\> | 是 | 回调函数。当提交配置信息成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode),比如预览流与录像输出流的分辨率的宽高比不一致,会返回7400201。 | 72 73**错误码:** 74 75以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 76 77| 错误码ID | 错误信息 | 78| --------------- | --------------- | 79| 7400102 | Operation not allowed. | 80| 7400201 | Camera service fatal error. | 81 82**示例:** 83 84```ts 85import { BusinessError } from '@kit.BasicServicesKit'; 86 87function commitConfig(session: camera.Session): void { 88 session.commitConfig((err: BusinessError) => { 89 if (err) { 90 console.error(`The commitConfig call failed. error code: ${err.code}`); 91 return; 92 } 93 console.info('Callback invoked to indicate the commit config success.'); 94 }); 95} 96``` 97 98## commitConfig<sup>11+</sup> 99 100commitConfig(): Promise\<void\> 101 102提交配置信息。使用Promise异步回调。 103 104**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 105 106**系统能力:** SystemCapability.Multimedia.Camera.Core 107 108**返回值:** 109 110| 类型 | 说明 | 111| -------------- | ------------------------ | 112| Promise\<void\> | Promise对象,无返回结果。 | 113 114**错误码:** 115 116以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 117 118| 错误码ID | 错误信息 | 119| --------------- | --------------- | 120| 7400102 | Operation not allowed. | 121| 7400201 | Camera service fatal error. | 122 123**示例:** 124 125```ts 126import { BusinessError } from '@kit.BasicServicesKit'; 127 128function commitConfig(session: camera.Session): void { 129 session.commitConfig().then(() => { 130 console.info('Promise returned to indicate the commit config success.'); 131 }).catch((error: BusinessError) => { 132 // 失败返回错误码error.code并处理。 133 console.error(`The commitConfig call failed. error code: ${error.code}`); 134 }); 135} 136``` 137 138## canAddInput<sup>11+</sup> 139 140canAddInput(cameraInput: CameraInput): boolean 141 142判断当前cameraInput是否可以添加到session中。当前函数需要在[beginConfig](#beginconfig11)和[commitConfig](#commitconfig11-1)之间生效。 143 144**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 145 146**系统能力:** SystemCapability.Multimedia.Camera.Core 147 148**参数:** 149 150| 参数名 | 类型 | 必填 | 说明 | 151| ----------- | --------------------------- | ---- | ------------------------ | 152| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要添加的CameraInput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 | 153 154**返回值:** 155 156| 类型 | 说明 | 157| -------------- | ------------------------ | 158| boolean | 判断当前cameraInput是否可以添加到session中。true表示支持添加当前cameraInput,false表示不支持添加。 | 159 160**示例:** 161 162```ts 163import { BusinessError } from '@kit.BasicServicesKit'; 164 165function canAddInput(session: camera.Session, cameraInput: camera.CameraInput): void { 166 let canAdd: boolean = session.canAddInput(cameraInput); 167 console.info(`The input canAddInput: ${canAdd}`); 168} 169``` 170 171## addInput<sup>11+</sup> 172 173addInput(cameraInput: CameraInput): void 174 175把[CameraInput](arkts-apis-camera-CameraInput.md)加入到会话。 176 177**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 178 179**系统能力:** SystemCapability.Multimedia.Camera.Core 180 181**参数:** 182 183| 参数名 | 类型 | 必填 | 说明 | 184| ----------- | --------------------------- | ---- | ------------------------ | 185| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要添加的CameraInput实例。 | 186 187**错误码:** 188 189以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 190 191| 错误码ID | 错误信息 | 192| --------------- | --------------- | 193| 7400101 | Parameter missing or parameter type incorrect. | 194| 7400102 | Operation not allowed. | 195| 7400201 | Camera service fatal error. | 196 197**示例:** 198 199```ts 200import { BusinessError } from '@kit.BasicServicesKit'; 201 202function addInput(session: camera.Session, cameraInput: camera.CameraInput): void { 203 try { 204 session.addInput(cameraInput); 205 } catch (error) { 206 // 失败返回错误码error.code并处理。 207 let err = error as BusinessError; 208 console.error(`The addInput call failed. error code: ${err.code}`); 209 } 210} 211``` 212 213## removeInput<sup>11+</sup> 214 215removeInput(cameraInput: CameraInput): void 216 217移除[CameraInput](arkts-apis-camera-CameraInput.md)。当前函数需要在[beginConfig](#beginconfig11)和[commitConfig](#commitconfig11-1)之间生效。 218 219**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 220 221**系统能力:** SystemCapability.Multimedia.Camera.Core 222 223**参数:** 224 225| 参数名 | 类型 | 必填 | 说明 | 226| ----------- | --------------------------- | ---- | ------------------------ | 227| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要移除的CameraInput实例。 | 228 229**错误码:** 230 231以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 232 233| 错误码ID | 错误信息 | 234| --------------- | --------------- | 235| 7400101 | Parameter missing or parameter type incorrect. | 236| 7400102 | Operation not allowed. | 237| 7400201 | Camera service fatal error. | 238 239**示例:** 240 241```ts 242import { BusinessError } from '@kit.BasicServicesKit'; 243 244function removeInput(session: camera.Session, cameraInput: camera.CameraInput): void { 245 try { 246 session.removeInput(cameraInput); 247 } catch (error) { 248 // 失败返回错误码error.code并处理。 249 let err = error as BusinessError; 250 console.error(`The removeInput call failed. error code: ${err.code}`); 251 } 252} 253``` 254 255## canAddOutput<sup>11+</sup> 256 257canAddOutput(cameraOutput: CameraOutput): boolean 258 259判断当前cameraOutput是否可以添加到session中。当前函数需要在[addInput](#addinput11)和[commitConfig](#commitconfig11-1)之间生效。 260 261**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 262 263**系统能力:** SystemCapability.Multimedia.Camera.Core 264 265**参数:** 266 267| 参数名 | 类型 | 必填 | 说明 | 268| ----------- | --------------------------- | ---- | ------------------------ | 269| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要添加的CameraOutput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 | 270 271**返回值:** 272 273| 类型 | 说明 | 274| -------------- | ------------------------ | 275| boolean | 是否可以添加当前cameraOutput到session中,true为可添加,false为不可添加。 | 276 277**示例:** 278 279```ts 280import { BusinessError } from '@kit.BasicServicesKit'; 281 282function canAddOutput(session: camera.Session, cameraOutput: camera.CameraOutput): void { 283 let canAdd: boolean = session.canAddOutput(cameraOutput); 284 console.info(`This addOutput can add: ${canAdd}`); 285} 286``` 287 288## addOutput<sup>11+</sup> 289 290addOutput(cameraOutput: CameraOutput): void 291 292把[CameraOutput](arkts-apis-camera-CameraOutput.md)加入到会话。 293 294**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 295 296**系统能力:** SystemCapability.Multimedia.Camera.Core 297 298**参数:** 299 300| 参数名 | 类型 | 必填 | 说明 | 301| ------------- | ------------------------------- | ---- | ------------------------ | 302| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要添加的CameraOutput实例。 | 303 304**错误码:** 305 306以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 307 308| 错误码ID | 错误信息 | 309| --------------- | --------------- | 310| 7400101 | Parameter missing or parameter type incorrect. | 311| 7400102 | Operation not allowed. | 312| 7400201 | Camera service fatal error. | 313 314**示例:** 315 316```ts 317import { BusinessError } from '@kit.BasicServicesKit'; 318 319function addOutput(session: camera.Session, cameraOutput: camera.CameraOutput): void { 320 try { 321 session.addOutput(cameraOutput); 322 } catch (error) { 323 // 失败返回错误码error.code并处理。 324 let err = error as BusinessError; 325 console.error(`The addOutput call failed. error code: ${err.code}`); 326 } 327} 328``` 329 330## removeOutput<sup>11+</sup> 331 332removeOutput(cameraOutput: CameraOutput): void 333 334从会话中移除[CameraOutput](arkts-apis-camera-CameraOutput.md)。 335 336**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 337 338**系统能力:** SystemCapability.Multimedia.Camera.Core 339 340**参数:** 341 342| 参数名 | 类型 | 必填 | 说明 | 343| ------------- | ------------------------------- | ---- | ------------------------ | 344| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要移除的CameraOutput实例。 | 345 346**错误码:** 347 348以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 349 350| 错误码ID | 错误信息 | 351| --------------- | --------------- | 352| 7400101 | Parameter missing or parameter type incorrect. | 353| 7400102 | Operation not allowed. | 354| 7400201 | Camera service fatal error. | 355 356**示例:** 357 358```ts 359import { BusinessError } from '@kit.BasicServicesKit'; 360 361function removeOutput(session: camera.Session, previewOutput: camera.PreviewOutput): void { 362 try { 363 session.removeOutput(previewOutput); 364 } catch (error) { 365 // 失败返回错误码error.code并处理。 366 let err = error as BusinessError; 367 console.error(`The removeOutput call failed. error code: ${err.code}`); 368 } 369} 370``` 371 372## start<sup>11+</sup> 373 374start(callback: AsyncCallback\<void\>): void 375 376开始会话工作,通过注册回调函数获取结果。使用callback异步回调。 377 378**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 379 380**系统能力:** SystemCapability.Multimedia.Camera.Core 381 382**参数:** 383 384| 参数名 | 类型 | 必填 | 说明 | 385| -------- | -------------------- | ---- | -------------------- | 386| callback | AsyncCallback\<void\> | 是 | 回调函数。当开始会话工作成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 387 388**错误码:** 389 390以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 391 392| 错误码ID | 错误信息 | 393| --------------- | --------------- | 394| 7400102 | Operation not allowed. | 395| 7400103 | Session not config. | 396| 7400201 | Camera service fatal error. | 397 398**示例:** 399 400```ts 401import { BusinessError } from '@kit.BasicServicesKit'; 402 403function startCaptureSession(session: camera.Session): void { 404 session.start((err: BusinessError) => { 405 if (err) { 406 console.error(`Failed to start the session, error code: ${err.code}.`); 407 return; 408 } 409 console.info('Callback invoked to indicate the session start success.'); 410 }); 411} 412``` 413 414## start<sup>11+</sup> 415 416start(): Promise\<void\> 417 418开始会话工作。使用Promise异步回调。 419 420**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 421 422**系统能力:** SystemCapability.Multimedia.Camera.Core 423 424**返回值:** 425 426| 类型 | 说明 | 427| -------------- | ------------------------ | 428| Promise\<void\> | Promise对象,无返回结果。 | 429 430**错误码:** 431 432以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 433 434| 错误码ID | 错误信息 | 435| --------------- | --------------- | 436| 7400102 | Operation not allowed. | 437| 7400103 | Session not config. | 438| 7400201 | Camera service fatal error. | 439 440**示例:** 441 442```ts 443import { BusinessError } from '@kit.BasicServicesKit'; 444 445function startCaptureSession(session: camera.Session): void { 446 session.start().then(() => { 447 console.info('Promise returned to indicate the session start success.'); 448 }).catch((error: BusinessError) => { 449 console.error(`Failed to start the session, error code: ${error.code}.`); 450 }); 451} 452``` 453 454## stop<sup>11+</sup> 455 456stop(callback: AsyncCallback\<void\>): void 457 458停止会话工作,通过注册回调函数获取结果。使用callback异步回调。 459 460**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 461 462**系统能力:** SystemCapability.Multimedia.Camera.Core 463 464**参数:** 465 466| 参数名 | 类型 | 必填 | 说明 | 467| -------- | -------------------- | ---- | ------------------- | 468| callback | AsyncCallback\<void\> | 是 | 回调函数。当停止会话工作成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 469 470**错误码:** 471 472以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 473 474| 错误码ID | 错误信息 | 475| --------------- | --------------- | 476| 7400201 | Camera service fatal error. | 477 478**示例:** 479 480```ts 481import { BusinessError } from '@kit.BasicServicesKit'; 482 483function stopCaptureSession(session: camera.Session): void { 484 session.stop((err: BusinessError) => { 485 if (err) { 486 console.error(`Failed to stop the session, error code: ${err.code}.`); 487 return; 488 } 489 console.info('Callback invoked to indicate the session stop success.'); 490 }); 491} 492``` 493 494## stop<sup>11+</sup> 495 496stop(): Promise\<void\> 497 498停止会话工作。使用Promise异步回调。 499 500**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 501 502**系统能力:** SystemCapability.Multimedia.Camera.Core 503 504**返回值:** 505 506| 类型 | 说明 | 507| -------------- |-------------------| 508| Promise\<void\> | Promise对象,无返回结果。 | 509 510**错误码:** 511 512以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 513 514| 错误码ID | 错误信息 | 515| --------------- | --------------- | 516| 7400201 | Camera service fatal error. | 517 518**示例:** 519 520```ts 521import { BusinessError } from '@kit.BasicServicesKit'; 522 523function stopCaptureSession(session: camera.Session): void { 524 session.stop().then(() => { 525 console.info('Promise returned to indicate the session stop success.'); 526 }).catch((error: BusinessError) => { 527 console.error(`Failed to stop the session, error code: ${error.code}.`); 528 }); 529} 530``` 531 532## release<sup>11+</sup> 533 534release(callback: AsyncCallback\<void\>): void 535 536释放会话资源,通过注册回调函数获取结果。使用callback异步回调。 537 538**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 539 540**系统能力:** SystemCapability.Multimedia.Camera.Core 541 542**参数:** 543 544| 参数名 | 类型 | 必填 | 说明 | 545| -------- | -------------------- | ---- | -------------------- | 546| callback | AsyncCallback\<void\> | 是 | 回调函数。当释放会话资源成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 | 547 548**错误码:** 549 550以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 551 552| 错误码ID | 错误信息 | 553| --------------- | --------------- | 554| 7400201 | Camera service fatal error. | 555 556**示例:** 557 558```ts 559import { BusinessError } from '@kit.BasicServicesKit'; 560 561function releaseCaptureSession(session: camera.Session): void { 562 session.release((err: BusinessError) => { 563 if (err) { 564 console.error(`Failed to release the session instance, error code: ${err.code}.`); 565 return; 566 } 567 console.info('Callback invoked to indicate that the session instance is released successfully.'); 568 }); 569} 570``` 571 572## release<sup>11+</sup> 573 574release(): Promise\<void\> 575 576释放会话资源。使用Promise异步回调。 577 578**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 579 580**系统能力:** SystemCapability.Multimedia.Camera.Core 581 582**返回值:** 583 584| 类型 | 说明 | 585| -------------- | ------------------------ | 586| Promise\<void\> | Promise对象,无返回结果。 | 587 588**错误码:** 589 590以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。 591 592| 错误码ID | 错误信息 | 593| --------------- | --------------- | 594| 7400201 | Camera service fatal error. | 595 596**示例:** 597 598```ts 599import { BusinessError } from '@kit.BasicServicesKit'; 600 601function releaseCaptureSession(session: camera.Session): void { 602 session.release().then(() => { 603 console.info('Promise returned to indicate that the session instance is released successfully.'); 604 }).catch((error: BusinessError) => { 605 console.error(`Failed to release the session instance, error code: ${error.code}.`); 606 }); 607} 608``` 609