1# @ohos.multimedia.camera (相机管理) 2 3> **说明:** 4> 5> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6 7## 导入模块 8 9```ts 10import camera from '@ohos.multimedia.camera'; 11``` 12 13## camera.getCameraManager 14 15getCameraManager(context: BaseContext): CameraManager 16 17获取相机管理器实例,同步返回结果。 18 19**系统能力:** SystemCapability.Multimedia.Camera.Core 20 21**参数:** 22 23| 参数名 | 类型 | 必填 | 说明 | 24| -------- | ----------------------------------------------- | ---- | ---------------------------- | 25| context | [BaseContext](js-apis-inner-application-baseContext.md) | 是 | 应用上下文。 | 26 27**返回值:** 28 29| 类型 | 说明 | 30| ----------------------------------------------- | ---------------------------- | 31| [CameraManager](#cameramanager) | 相机管理器。 | 32 33**错误码:** 34 35以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 36 37| 错误码ID | 错误信息 | 38| --------------- | --------------- | 39| 7400101 | Parameter missing or parameter type incorrect | 40| 7400201 | Camera service fatal error. | 41 42**示例:** 43 44```ts 45import common from '@ohos.app.ability.common'; 46import { BusinessError } from '@ohos.base'; 47 48function getCameraManager(context: common.BaseContext): camera.CameraManager | undefined { 49 let cameraManager: camera.CameraManager | undefined = undefined; 50 try { 51 cameraManager = camera.getCameraManager(context); 52 } catch (error) { 53 let err = error as BusinessError; 54 console.error(`The getCameraManager call failed. error code: ${err.code}`); 55 } 56 return cameraManager; 57} 58``` 59## camera.getModeManager 60 61getModeManager(context: BaseContext): ModeManager 62 63获取模式化管理器实例,同步返回结果。 64 65模式化管理是对于cameraManager功能的增强与扩充,主要用于一些高级功能的管理(如人像模式)。 66 67**系统接口:** 此接口为系统接口。 68 69**系统能力:** SystemCapability.Multimedia.Camera.Core 70 71**参数:** 72 73| 参数名 | 类型 | 必填 | 说明 | 74| -------- | ----------------------------------------------- | ---- | ---------------------------- | 75| context | [BaseContext](js-apis-inner-application-baseContext.md) | 是 | 应用上下文。 | 76 77**返回值:** 78 79| 类型 | 说明 | 80| --------------------------------------| -----------| 81| [ModeManager](#modemanager) | 模式化管理器。| 82 83**错误码:** 84 85以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md) 86 87| 错误码ID | 错误信息 | 88| --------------- | --------------- | 89| 7400101 | Parameter missing or parameter type incorrect | 90| 7400201 | Camera service fatal error. | 91 92**示例:** 93 94```ts 95import common from '@ohos.app.ability.common'; 96import { BusinessError } from '@ohos.base'; 97 98function getModeManager(context: common.BaseContext): camera.ModeManager | undefined { 99 let modeManager: camera.ModeManager | undefined = undefined; 100 try { 101 modeManager = camera.getModeManager(context); 102 } catch (error) { 103 let err = error as BusinessError; 104 console.error(`The getModeManager call failed. error code: ${err.code}`); 105 } 106 return modeManager; 107} 108``` 109 110## CameraStatus 111 112枚举,相机状态。 113 114**系统能力:** SystemCapability.Multimedia.Camera.Core 115 116| 名称 | 值 | 说明 | 117| ------------------------- | ---- | ------------ | 118| CAMERA_STATUS_APPEAR | 0 | 新的相机出现。 | 119| CAMERA_STATUS_DISAPPEAR | 1 | 相机被移除。 | 120| CAMERA_STATUS_AVAILABLE | 2 | 相机可用。 | 121| CAMERA_STATUS_UNAVAILABLE | 3 | 相机不可用。 | 122 123## Profile 124 125相机配置信息项。 126 127**系统能力:** SystemCapability.Multimedia.Camera.Core 128 129| 名称 | 类型 | 必填 | 说明 | 130| -------- | ----------------------------- |---- | ------------- | 131| format | [CameraFormat](#cameraformat) | 是 | 输出格式。 | 132| size | [Size](#size) | 是 | 分辨率。 | 133 134## FrameRateRange 135 136帧率范围。 137 138**系统能力:** SystemCapability.Multimedia.Camera.Core 139 140| 名称 | 类型 | 必填 | 说明 | 141| -------- | ----------------------------- |---- | ------------- | 142| min | number | 是 | 最小帧率。 | 143| max | number | 是 | 最大帧率。 | 144 145## VideoProfile 146 147视频配置信息项。 148 149**系统能力:** SystemCapability.Multimedia.Camera.Core 150 151| 名称 | 类型 | 必填 | 说明 | 152| ------------------------- | ----------------------------------------- | --- |----------- | 153| frameRateRange | [FrameRateRange](#frameraterange) | 是 | 帧率范围,fps(frames per second)。 | 154 155## CameraOutputCapability 156 157相机输出能力项。 158 159**系统能力:** SystemCapability.Multimedia.Camera.Core 160 161| 名称 | 类型 | 必填 | 说明 | 162| ----------------------------- | -------------------------------------------------- | --- |------------------- | 163| previewProfiles | Array\<[Profile](#profile)\> | 是 | 支持的预览配置信息。 | 164| photoProfiles | Array\<[Profile](#profile)\> | 是 | 支持的拍照配置信息。 | 165| videoProfiles | Array\<[VideoProfile](#videoprofile)\> | 是 | 支持的录像配置信息。 | 166| supportedMetadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | 支持的metadata流类型信息。| 167 168## CameraErrorCode 169 170相机错误码。接口使用不正确以及on接口监听error状态返回。 171 172**系统能力:** SystemCapability.Multimedia.Camera.Core 173 174| 名称 | 值 | 说明 | 175| ------------------------- | ---- | ------------ | 176| INVALID_ARGUMENT | 7400101 | 参数缺失或者参数类型不对。 | 177| OPERATION_NOT_ALLOWED | 7400102 | 操作流程不对,不允许。 | 178| SESSION_NOT_CONFIG | 7400103 | session 未配置返回。 | 179| SESSION_NOT_RUNNING | 7400104 | session 未运行返回。 | 180| SESSION_CONFIG_LOCKED | 7400105 | session 配置已锁定返回。 | 181| DEVICE_SETTING_LOCKED | 7400106 | 设备设置已锁定返回。 | 182| CONFLICT_CAMERA | 7400107 | 设备重复打开返回。 | 183| DEVICE_DISABLED | 7400108 | 安全原因摄像头被禁用。 | 184| DEVICE_PREEMPTED | 7400109 | 相机被抢占导致无法使用 | 185| SERVICE_FATAL_ERROR | 7400201 | 相机服务错误返回。 | 186 187## CameraMode 188 189相机模式。 190 191**系统接口:** 此接口为系统接口。 192 193**系统能力:** SystemCapability.Multimedia.Camera.Core 194 195| 名称 | 值 | 说明 | 196| ----------------| ---- | ---------| 197| PORTRAIT | 1 | 人像模式 | 198 199## FilterType 200 201滤镜类型。 202 203**系统接口:** 此接口为系统接口。 204 205**系统能力:** SystemCapability.Multimedia.Camera.Core 206 207| 名称 | 值 | 说明 | 208| ----------------| ---- | ---------| 209| NONE | 0 | 原图。 | 210| CLASSIC | 1 | 经典。 | 211| DAWN | 2 | 晨光。 | 212| PURE | 3 | 清纯。 | 213| GREY | 4 | 灰调。 | 214| NATURAL | 5 | 自然。 | 215| MORI | 6 | 森系。 | 216| FAIR | 7 | 白皙。 | 217| PINK | 8 | 粉调。 | 218 219 220## PortraitEffect 221 222人像效果类型。 223 224**系统接口:** 此接口为系统接口。 225 226**系统能力:** SystemCapability.Multimedia.Camera.Core 227 228| 名称 | 值 | 说明 | 229| ----------------| ---- | ---------| 230| OFF | 0 | 关闭。 | 231| CIRCLES | 1 | 圆形。 | 232 233## BeautyType 234 235美颜类型。 236 237**系统接口:** 此接口为系统接口。 238 239**系统能力:** SystemCapability.Multimedia.Camera.Core 240 241| 名称 | 值 | 说明 | 242| ----------------| ---- | ---------| 243| AUTO | 0 | 自动。 | 244| SKIN_SMOOTH | 1 | 光滑。 | 245| FACE_SLENDER | 2 | 瘦脸。 | 246| SKIN_TONE | 3 | 肤色。 | 247 248## CameraManager 249 250相机管理器类,使用前需要通过getCameraManager获取相机管理实例。 251 252### getSupportedCameras 253 254getSupportedCameras(): Array\<CameraDevice\> 255 256获取支持指定的相机设备对象,同步返回结果。 257 258**系统能力:** SystemCapability.Multimedia.Camera.Core 259 260**返回值:** 261 262| 类型 | 说明 | 263| ----------------------------------------------- | ---------------------------- | 264| Array\<[CameraDevice](#cameradevice)> | 相机设备列表。 | 265 266**示例:** 267 268```ts 269import { BusinessError } from '@ohos.base'; 270 271function getSupportedCameras(cameraManager: camera.CameraManager): Array<camera.CameraDevice> { 272 let cameras: Array<camera.CameraDevice> = []; 273 try { 274 cameras = cameraManager.getSupportedCameras(); 275 } catch (error) { 276 let err = error as BusinessError; 277 console.error(`The getSupportedCameras call failed. error code: ${err.code}`); 278 } 279 return cameras; 280} 281``` 282 283### getSupportedOutputCapability 284 285getSupportedOutputCapability(camera:CameraDevice): CameraOutputCapability 286 287查询相机设备在模式下支持的输出能力,同步返回结果。 288 289**系统能力:** SystemCapability.Multimedia.Camera.Core 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 295| cameraDevice | [CameraDevice](#cameradevice) | 是 | 相机设备,通过 getSupportedCameras 接口获取 | 296 297**返回值:** 298 299| 类型 | 说明 | 300| ----------------------------------------------- | ---------------------------- | 301| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 | 302 303**示例:** 304 305```ts 306function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability { 307 let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice); 308 return cameraOutputCapability; 309} 310``` 311 312### isCameraMuted 313 314isCameraMuted(): boolean 315 316查询相机当前的禁用状态(禁用/未禁用)。 317 318在此之前,需要通过[isCameraMuteSupported](#iscameramutesupported)确认当前设备支持禁用相机。 319 320**系统能力:** SystemCapability.Multimedia.Camera.Core 321 322**返回值:** 323 324| 类型 | 说明 | 325| ---------- | -------------------------------------------- | 326| boolean | 返回true表示相机被禁用,返回false表示相机未被禁用。 | 327 328**示例:** 329 330```ts 331function isCameraMuted(cameraManager: camera.CameraManager): boolean { 332 let isMuted: boolean = cameraManager.isCameraMuted(); 333 return isMuted; 334} 335``` 336 337### isCameraMuteSupported 338 339isCameraMuteSupported(): boolean 340 341查询当前设备是否支持禁用相机,通过返回值返回结果。 342 343**系统接口:** 此接口为系统接口。 344 345**系统能力:** SystemCapability.Multimedia.Camera.Core 346 347**返回值:** 348 349| 类型 | 说明 | 350| ---------- | ----------------------------- | 351| boolean | 返回true表示相机可以被禁用,返回false表示相机不能被禁用。 | 352 353**示例:** 354 355```ts 356function isCameraMuteSupported(cameraManager: camera.CameraManager): boolean { 357 let isMuteSupported: boolean = cameraManager.isCameraMuteSupported(); 358 return isMuteSupported; 359} 360``` 361 362### muteCamera 363 364muteCamera(mute: boolean): void 365 366禁用相机。 367 368**系统接口:** 此接口为系统接口。 369 370**系统能力:** SystemCapability.Multimedia.Camera.Core 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| -------- | --------------------------------- | ---- | ---------- | 376| mute | boolean | 是 | 禁用相机。 | 377 378**示例:** 379 380```ts 381function muteCamera(cameraManager: camera.CameraManager): void { 382 let mute: boolean = true; 383 cameraManager.muteCamera(mute); 384} 385``` 386 387### createCameraInput 388 389createCameraInput(camera: CameraDevice): CameraInput 390 391使用CameraDevice对象创建CameraInput实例,同步返回结果。 392 393**需要权限:** ohos.permission.CAMERA 394 395**系统能力:** SystemCapability.Multimedia.Camera.Core 396 397**参数:** 398 399| 参数名 | 类型 | 必填 | 说明 | 400| -------- | ------------------------------------------- | ---- | --------------------------------- | 401| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象,通过 getSupportedCameras 接口获取 | 402 403**返回值:** 404 405| 类型 | 说明 | 406| ---------- | ----------------------------- | 407| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 408 409**错误码:** 410 411以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 412 413| 错误码ID | 错误信息 | 414| --------------- | --------------- | 415| 7400101 | Parameter missing or parameter type incorrect | 416 417**示例:** 418 419```ts 420import { BusinessError } from '@ohos.base'; 421 422function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | undefined { 423 let cameraInput: camera.CameraInput | undefined = undefined; 424 try { 425 cameraInput = cameraManager.createCameraInput(cameraDevice); 426 } catch (error) { 427 // 失败返回错误码error.code并处理 428 let err = error as BusinessError; 429 console.error(`The createCameraInput call failed. error code: ${err.code}`); 430 } 431 return cameraInput; 432} 433``` 434 435### createCameraInput 436 437createCameraInput(position: CameraPosition, type: CameraType): CameraInput 438 439根据相机位置和类型创建CameraInput实例,同步返回结果。 440 441**需要权限:** ohos.permission.CAMERA 442 443**系统能力:** SystemCapability.Multimedia.Camera.Core 444 445**参数:** 446 447| 参数名 | 类型 | 必填 | 说明 | 448| -------- | ------------------------------------------- | ---- | --------------------------------- | 449| position | [CameraPosition](#cameraposition) | 是 | 相机位置,通过 getSupportedCameras 接口获取设备,然后获取设备位置信息 | 450| type | [CameraType](#cameratype) | 是 | 相机类型,通过 getSupportedCameras 接口获取设备,然后获取设备类型信息 | 451 452**返回值:** 453 454| 类型 | 说明 | 455| ---------- | ----------------------------- | 456| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 457 458**错误码:** 459 460以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 461 462| 错误码ID | 错误信息 | 463| --------------- | --------------- | 464| 7400101 | Parameter missing or parameter type incorrect | 465 466**示例:** 467 468```ts 469import { BusinessError } from '@ohos.base'; 470 471function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | undefined { 472 let position: camera.CameraPosition = cameraDevice.cameraPosition; 473 let type: camera.CameraType = cameraDevice.cameraType; 474 let cameraInput: camera.CameraInput | undefined = undefined; 475 try { 476 cameraInput = cameraManager.createCameraInput(position, type); 477 } catch (error) { 478 // 失败返回错误码error.code并处理 479 let err = error as BusinessError; 480 console.error(`The createCameraInput call failed. error code: ${err.code}`); 481 } 482 return cameraInput; 483} 484``` 485 486### createPreviewOutput 487 488createPreviewOutput(profile: Profile, surfaceId: string): PreviewOutput 489 490创建预览输出对象,同步返回结果。 491 492**系统能力:** SystemCapability.Multimedia.Camera.Core 493 494**参数:** 495 496| 参数名 | 类型 | 必填 | 说明 | 497| -------- | ----------------------------------------------- | ---- | ------------------------------- | 498| profile | [Profile](#profile) | 是 | 支持的预览配置信息,通过getSupportedOutputCapability接口获取。| 499| surfaceId| string | 是 | 从[XComponent](../arkui-ts/ts-basic-components-xcomponent.md)或者[ImageReceiver](js-apis-image.md#imagereceiver9)组件获取的surfaceId。| 500 501**返回值:** 502 503| 类型 | 说明 | 504| ---------- | ----------------------------- | 505| [PreviewOutput](#previewoutput) | PreviewOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 506 507**错误码:** 508 509以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 510 511| 错误码ID | 错误信息 | 512| --------------- | --------------- | 513| 7400101 | Parameter missing or parameter type incorrect | 514 515**示例:** 516 517```ts 518import { BusinessError } from '@ohos.base'; 519 520function createPreviewOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PreviewOutput | undefined { 521 let profile: camera.Profile = cameraOutputCapability.previewProfiles[0]; 522 let previewOutput: camera.PreviewOutput | undefined = undefined; 523 try { 524 previewOutput = cameraManager.createPreviewOutput(profile, surfaceId); 525 } catch (error) { 526 // 失败返回错误码error.code并处理 527 let err = error as BusinessError; 528 console.error(`The createPreviewOutput call failed. error code: ${err.code}`); 529 } 530 return previewOutput; 531} 532``` 533 534### createPhotoOutput 535 536createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput 537 538创建拍照输出对象,同步返回结果。 539 540**系统能力:** SystemCapability.Multimedia.Camera.Core 541 542**参数:** 543 544| 参数名 | 类型 | 必填 | 说明 | 545| -------- | ------------------------------------------- | ---- | ----------------------------------- | 546| profile | [Profile](#profile) | 是 | 支持的拍照配置信息,通过getSupportedOutputCapability接口获取。| 547| surfaceId| string | 是 | 从[ImageReceiver](js-apis-image.md#imagereceiver9)获取的surfaceId。| 548 549**返回值:** 550 551| 类型 | 说明 | 552| ---------- | ----------------------------- | 553| [PhotoOutput](#photooutput) | PhotoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 554 555**错误码:** 556 557以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 558 559| 错误码ID | 错误信息 | 560| --------------- | --------------- | 561| 7400101 | Parameter missing or parameter type incorrect | 562 563**示例:** 564 565```ts 566import { BusinessError } from '@ohos.base'; 567 568function createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PhotoOutput | undefined { 569 let profile: camera.Profile = cameraOutputCapability.photoProfiles[0]; 570 let photoOutput: camera.PhotoOutput | undefined = undefined; 571 try { 572 photoOutput = cameraManager.createPhotoOutput(profile, surfaceId); 573 } catch (error) { 574 // 失败返回错误码error.code并处理 575 let err = error as BusinessError; 576 console.error(`The createPhotoOutput call failed. error code: ${err.code}`); 577 } 578 return photoOutput; 579} 580``` 581 582### createVideoOutput 583 584createVideoOutput(profile: VideoProfile, surfaceId: string): VideoOutput 585 586创建录像输出对象,同步返回结果。 587 588**系统能力:** SystemCapability.Multimedia.Camera.Core 589 590**参数:** 591 592| 参数名 | 类型 | 必填 | 说明 | 593| -------- | ------------------------------------------- | ---- | ------------------------------ | 594| profile | [VideoProfile](#videoprofile) | 是 | 支持的录像配置信息,通过getSupportedOutputCapability接口获取。 | 595| surfaceId| string | 是 | 从[VideoRecorder](js-apis-media.md#videorecorder9)获取的surfaceId。| 596 597**返回值:** 598 599| 类型 | 说明 | 600| ---------- | ----------------------------- | 601| [VideoOutput](#videooutput) | VideoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 602 603**错误码:** 604 605以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 606 607| 错误码ID | 错误信息 | 608| --------------- | --------------- | 609| 7400101 | Parameter missing or parameter type incorrect | 610 611**示例:** 612 613```ts 614import { BusinessError } from '@ohos.base'; 615 616function createVideoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.VideoOutput | undefined { 617 let profile: camera.VideoProfile = cameraOutputCapability.videoProfiles[0]; 618 let videoOutput: camera.VideoOutput | undefined = undefined; 619 try { 620 videoOutput = cameraManager.createVideoOutput(profile, surfaceId); 621 } catch (error) { 622 // 失败返回错误码error.code并处理 623 let err = error as BusinessError; 624 console.error(`The createPhotoOutput call failed. error code: ${err.code}`); 625 } 626 return videoOutput; 627} 628``` 629 630### createMetadataOutput 631 632createMetadataOutput(metadataObjectTypes:Array\<MetadataObjectType\>): MetadataOutput 633 634创建metadata流输出对象,同步返回结果。 635 636**系统能力:** SystemCapability.Multimedia.Camera.Core 637 638**参数:** 639 640| 参数名 | 类型 | 必填 | 说明 | 641| -------------------- | -------------------------------------------------- | --- | ---------------------------- | 642| metadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | metadata流类型信息,通过getSupportedOutputCapability接口获取。 | 643 644**返回值:** 645 646| 类型 | 说明 | 647| ---------- | ----------------------------- | 648| [MetadataOutput](#metadataoutput) | MetadataOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 649 650**错误码:** 651 652以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 653 654| 错误码ID | 错误信息 | 655| --------------- | --------------- | 656| 7400101 | Parameter missing or parameter type incorrect | 657 658**示例:** 659 660```ts 661import { BusinessError } from '@ohos.base'; 662 663function createMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): void { 664 let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes; 665 let metadataOutput: camera.MetadataOutput | undefined = undefined; 666 try { 667 metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); 668 } catch (error) { 669 // 失败返回错误码error.code并处理 670 let err = error as BusinessError; 671 console.error(`createMetadataOutput error. error code: ${err.code}`); 672 } 673} 674``` 675 676### createCaptureSession 677 678createCaptureSession(): CaptureSession 679 680创建CaptureSession实例,通过注册回调函数获取结果。 681 682**系统能力:** SystemCapability.Multimedia.Camera.Core 683 684**返回值:** 685 686| 类型 | 说明 | 687| ---------- | ----------------------------- | 688| [CaptureSession](#capturesession) | CaptureSession实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 689 690**错误码:** 691 692以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 693 694| 错误码ID | 错误信息 | 695| --------------- | --------------- | 696| 7400201 | Camera service fatal error. | 697 698**示例:** 699 700```ts 701import { BusinessError } from '@ohos.base'; 702 703function createCaptureSession(cameraManager: camera.CameraManager): camera.CaptureSession | undefined { 704 let captureSession: camera.CaptureSession | undefined = undefined; 705 try { 706 captureSession = cameraManager.createCaptureSession(); 707 } catch (error) { 708 // 失败返回错误码error.code并处理 709 let err = error as BusinessError; 710 console.error(`createCaptureSession error. error code: ${err.code}`); 711 } 712 return captureSession; 713} 714``` 715 716### on('cameraStatus') 717 718on(type: 'cameraStatus', callback: AsyncCallback\<CameraStatusInfo\>): void 719 720相机设备状态回调,通过注册回调函数获取相机的状态变化。 721 722**系统能力:** SystemCapability.Multimedia.Camera.Core 723 724**参数:** 725 726| 参数名 | 类型 | 必填 | 说明 | 727| -------- | -----------------| ---- | --------- | 728| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。目前只支持对设备打开或者关闭会触发该事件并返回对应信息 | 729| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 是 | 回调函数,用于获取镜头状态变化信息。 | | 730 731**示例:** 732 733```ts 734import { BusinessError } from '@ohos.base'; 735 736function registerCameraStatus(cameraManager: camera.CameraManager): void { 737 cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { 738 console.info(`camera : ${cameraStatusInfo.camera.cameraId}`); 739 console.info(`status: ${cameraStatusInfo.status}`); 740 }); 741} 742``` 743 744### off('cameraStatus') 745 746off(type: 'cameraStatus', callback?: AsyncCallback\<CameraStatusInfo\>): void 747 748相机设备状态注销回调,通过注销回调函数取消获取相机的状态变化。 749 750**系统能力:** SystemCapability.Multimedia.Camera.Core 751 752**参数:** 753 754| 参数名 | 类型 | 必填 | 说明 | 755| -------- | -----------------| ---- | --------- | 756| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。 | 757| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 否 | 可选参数,有表明匹配on接口callback。 | | 758 759**示例:** 760 761```ts 762function unregisterCameraStatus(cameraManager: camera.CameraManager): void { 763 cameraManager.off('cameraStatus'); 764} 765``` 766 767### on('cameraMute') 768 769on(type: 'cameraMute', callback: AsyncCallback\<boolean\>): void 770 771禁用回调,通过注册回调函数获取相机禁用状态变化。 772 773**系统接口:** 此接口为系统接口。 774 775**系统能力:** SystemCapability.Multimedia.Camera.Core 776 777**参数:** 778 779| 参数名 | 类型 | 必填 | 说明 | 780| -------- | --------------- | ---- | --------- | 781| type | string | 是 | 监听事件,固定为'cameraMute',系统相机摄像头开关,cameraManager对象获取成功后可监听。系统设置打开或禁用相机会触发该事件并返回状态 | 782| callback | AsyncCallback\<boolean> | 是 | 回调函数,用于获取禁用状态变化信息,返回true是开启状态,返回false是禁用状态。 | 783 784**示例:** 785 786```ts 787import { BusinessError } from '@ohos.base'; 788 789function registerCameraMute(cameraManager: camera.CameraManager): void { 790 cameraManager.on('cameraMute', (err: BusinessError, curMuted: boolean) => { 791 let isMuted: boolean = curMuted; 792 console.info(`cameraMute status: ${isMuted}`); 793 }) 794} 795``` 796 797### off('cameraMute') 798 799off(type: 'cameraMute', callback?: AsyncCallback\<boolean\>): void 800 801禁用回调,通过注销注册回调函数获取相机禁用状态变化。 802 803**系统接口:** 此接口为系统接口。 804 805**系统能力:** SystemCapability.Multimedia.Camera.Core 806 807**参数:** 808 809| 参数名 | 类型 | 必填 | 说明 | 810| -------- | --------------- | ---- | --------- | 811| type | string | 是 | 监听事件,固定为'cameraMute',系统相机摄像头开关,cameraManager对象获取成功后可监听。 | 812| callback | AsyncCallback\<boolean> | 否 | 回调函数,可选参数,有表明匹配on接口callback。 | 813 814**示例:** 815 816```ts 817import { BusinessError } from '@ohos.base'; 818 819function callback(err: BusinessError, curMuetd: boolean): void { 820 let isMuted: boolean = curMuetd; 821} 822 823function unregisterCameraMute(cameraManager: camera.CameraManager): void { 824 cameraManager.on('cameraMute', callback); 825 cameraManager.off('cameraMute', callback); 826} 827``` 828 829### isPrelaunchSupported 830 831isPrelaunchSupported(camera: CameraDevice): boolean 832 833检查指定cameraDevice是否支持预热启动。 834 835 836**系统接口:** 此接口为系统接口。 837 838**系统能力:** SystemCapability.Multimedia.Camera.Core 839 840**参数:** 841 842| 参数名 | 类型 | 必填 | 说明 | 843| -------- | --------------- | ---- | --------- | 844| camera | [CameraDevice](#cameradevice) | 是 | 相机信息。| 845 846**返回值:** 847 848| 类型 | 说明 | 849| -------- | --------------- | 850| boolean | 返回指定cameraDevice是否支持预热启动。| 851 852**错误码:** 853 854以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 855 856| 错误码ID | 错误信息 | 857| --------------- | --------------- | 858| 7400101 | Parameter missing or parameter type incorrect. | 859 860**示例:** 861 862```ts 863import common from '@ohos.app.ability.common'; 864 865function isPreLaunchSupported(context: common.BaseContext): boolean { 866 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 867 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 868 let isSupported: boolean = false; 869 if (cameras && cameras.length >= 1) { 870 isSupported = cameraManager.isPrelaunchSupported(cameras[0]); 871 console.info(`PreLaunch supported states: ${isSupported}`); 872 return isSupported; 873 } 874 return isSupported; 875} 876``` 877 878### setPrelaunchConfig 879 880setPrelaunchConfig(prelaunchConfig: PrelaunchConfig): void 881 882在调用setPrelaunchConfig接口前,需要先检查设备是否支持预热启动,使用[isPrelaunchSupported](#isprelaunchsupported)判断指定cameraDevice是否支持预热启动。 883 884**系统接口:** 此接口为系统接口。 885 886**需要权限:** ohos.permission.CAMERA 887 888**系统能力:** SystemCapability.Multimedia.Camera.Core 889 890**参数:** 891 892| 参数名 | 类型 | 必填 | 说明 | 893| -------- | --------------- | ---- | --------- | 894| prelaunchConfig | [PrelaunchConfig](#prelaunchconfig) | 是 | 预启动配置参数。| 895 896**错误码:** 897 898以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 899 900| 错误码ID | 错误信息 | 901| --------------- | --------------- | 902| 7400101 | Parameter missing or parameter type incorrect. | 903| 7400102 | Operation not allow. | 904 905**示例:** 906 907```ts 908import common from '@ohos.app.ability.common'; 909import { BusinessError } from '@ohos.base'; 910 911function setPrelaunchConfig(context: common.BaseContext): void { 912 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 913 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 914 if (cameras && cameras.length >= 1) { 915 let cameraDevice: camera.CameraDevice = cameras[0]; 916 if(cameraManager.isPrelaunchSupported(cameraDevice)) { 917 try { 918 cameraManager.setPrelaunchConfig({cameraDevice: cameraDevice}); 919 } catch (error) { 920 let err = error as BusinessError; 921 console.error(`setPrelaunchConfig error. Code: ${err.code}, message: ${err.message}`); 922 } 923 } 924 } 925} 926``` 927 928### prelaunch 929 930prelaunch(): void 931 932用户点击系统相机图标,拉起相机应用同时调用,下发预热请求,使能相机预热启动。 933 934**系统接口:** 此接口为系统接口。 935 936**系统能力:** SystemCapability.Multimedia.Camera.Core 937 938**示例:** 939 940```ts 941import common from '@ohos.app.ability.common'; 942import { BusinessError } from '@ohos.base'; 943 944function preLaunch(context: common.BaseContext): void { 945 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 946 try { 947 cameraManager.prelaunch(); 948 } catch (error) { 949 let err = error as BusinessError; 950 console.error(`prelaunch error. Code: ${err.code}, message: ${err.message}`); 951 } 952} 953``` 954 955### createDeferredPreviewOutput 956 957createDeferredPreviewOutput(profile: Profile): PreviewOutput 958 959创建延迟预览输出对象,在配流时替代普通的预览输出对象加入数据流。 960 961**系统接口:** 此接口为系统接口。 962 963**系统能力:** SystemCapability.Multimedia.Camera.Core 964 965**参数:** 966 967| 参数名 | 类型 | 必填 | 说明 | 968| -------- | --------------- | ---- | --------- | 969| profile | [Profile](#profile) | 是 | 相机预览流的配置文件。 | 970 971**返回值:** 972 973| 类型 | 说明 | 974| -------- | --------------- | 975| [PreviewOutput](#previewoutput) | 返回预览输出对象。 | 976 977**错误码:** 978 979以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 980 981| 错误码ID | 错误信息 | 982| --------------- | --------------- | 983| 7400101 | Parameter missing or parameter type incorrect. | 984 985**示例:** 986 987```ts 988import common from '@ohos.app.ability.common'; 989 990function getDeferredPreviewOutput(context: common.BaseContext, previewProfile: camera.Profile): camera.PreviewOutput { 991 const cameraManager: camera.CameraManager = camera.getCameraManager(context); 992 const output: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 993 return output; 994} 995``` 996## ModeManager 997 998相机模式化管理器类,使用前需要通过[getModeManager](#cameragetmodemanager)获取相机模式化管理实例。 999 1000### getSupportedModes 1001 1002getSupportedModes(device: CameraDevice): Array\<CameraMode\> 1003 1004获取指定相机设备支持的模式列表,同步返回结果。 1005 1006**系统接口:** 此接口为系统接口。 1007 1008**系统能力:** SystemCapability.Multimedia.Camera.Core 1009 1010**参数:** 1011 1012| 参数名 | 类型 | 必填 | 说明 | 1013| -------- | -------------------------- | ---- | ------------------- | 1014| device | [CameraDevice](#cameradevice) | 是 | 相机设备实例,通过[getSupportedCameras](#getsupportedcameras)接口获取。 | 1015 1016**返回值:** 1017 1018| 类型 | 说明 | 1019| ----------------------------------------------- | ---------------------------- | 1020| Array\<[CameraMode](#cameramode)> | 支持的模式列表。 | 1021 1022**示例:** 1023 1024```ts 1025function getSupportedModes(modeManager: camera.ModeManager, cameraDevice: camera.CameraDevice): Array<camera.CameraMode> { 1026 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1027 return cameraModes; 1028} 1029``` 1030 1031### getSupportedOutputCapability 1032 1033getSupportedOutputCapability(device:CameraDevice, mode: CameraMode): CameraOutputCapability 1034 1035获取指定模式下相机设备支持的输出能力,同步返回结果。 1036 1037**系统接口:** 此接口为系统接口。 1038 1039**系统能力:** SystemCapability.Multimedia.Camera.Core 1040 1041**参数:** 1042 1043| 参数名 | 类型 | 必填 | 说明 | 1044| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 1045| device | [CameraDevice](#cameradevice) | 是 | 相机设备,通过[getSupportedCameras](#getsupportedcameras)接口获取。 | 1046| mode | [CameraMode](#cameramode) | 是 | 指定模式,通过[getSupportedModes](#getsupportedmodes)接口获取。 | 1047 1048**返回值:** 1049 1050| 类型 | 说明 | 1051| ----------------------------------------------- | ---------------------------- | 1052| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 | 1053 1054**示例:** 1055 1056```ts 1057function getSupportedOutputCapability(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CameraOutputCapability | undefined { 1058 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 1059 if (cameras == undefined || cameras.length <= 0) { 1060 return undefined; 1061 } 1062 let cameraDevice: camera.CameraDevice = cameras[0]; 1063 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1064 if (cameraModes === undefined || cameraModes.length <= 0) { 1065 return undefined; 1066 } 1067 let mode: camera.CameraMode = cameraModes[0]; 1068 let cameraOutputCapability: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraDevice, mode); 1069 return cameraOutputCapability; 1070} 1071``` 1072### createCaptureSession 1073 1074createCaptureSession(mode: CameraMode): CaptureSession 1075 1076根据当前的模式名,创建指定模式的会话。 1077 1078**系统接口:** 此接口为系统接口。 1079 1080**系统能力:** SystemCapability.Multimedia.Camera.Core 1081 1082**参数:** 1083 1084| 参数名 | 类型 | 必填 | 说明 | 1085| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 1086| mode | [CameraMode](#cameramode) | 是 | 指定模式,通过[getSupportedModes](#getsupportedmodes)获取。 | 1087 1088**返回值:** 1089 1090| 类型 | 说明 | 1091| ----------------------------------------------- | ---------------------------- | 1092| [CaptureSession](#capturesession) | 指定模式的会话实例。 | 1093 1094**示例:** 1095 1096```ts 1097import { BusinessError } from '@ohos.base'; 1098 1099function createCaptureSession(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CaptureSession | undefined { 1100 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 1101 if (cameras == undefined || cameras.length <= 0) { 1102 return; 1103 } 1104 let cameraDevice: camera.CameraDevice = cameras[0]; 1105 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1106 if (cameraModes === undefined || cameraModes.length <= 0) { 1107 return; 1108 } 1109 let mode: camera.CameraMode = cameraModes[0]; 1110 let captureSession: camera.CaptureSession | undefined = undefined; 1111 try { 1112 captureSession = modeManager.createCaptureSession(mode); 1113 } catch (error) { 1114 let err = error as BusinessError; 1115 console.error(`The createCaptureSession call failed. error code: ${err.code}`); 1116 } 1117 return captureSession; 1118} 1119``` 1120 1121## PrelaunchConfig 1122 1123相机预启动配置参数。 1124 1125当前sensor级别预热,待扩展流预热参数。 1126 1127**系统接口:** 此接口为系统接口。 1128 1129**系统能力:** SystemCapability.Multimedia.Camera.Core 1130 1131| 名称 | 类型 | 必填 | 说明 | 1132| ------ | ----------------------------- | -------------- | ---------- | 1133| cameraDevice | [CameraDevice](#cameradevice) | 是 | 相机信息。 | 1134 1135## CameraStatusInfo 1136 1137相机管理器回调返回的接口实例,表示相机状态信息。 1138 1139**系统能力:** SystemCapability.Multimedia.Camera.Core。 1140 1141| 名称 | 类型 | 必填 | 说明 | 1142| ------ | ----------------------------- | -------------- | ---------- | 1143| camera | [CameraDevice](#cameradevice) | 是 | 相机信息。 | 1144| status | [CameraStatus](#camerastatus) | 是 | 相机状态。 | 1145 1146## CameraPosition 1147 1148枚举,相机位置。 1149 1150**系统能力:** SystemCapability.Multimedia.Camera.Core 1151 1152| 名称 | 值 | 说明 | 1153| --------------------------- | ---- | -------------- | 1154| CAMERA_POSITION_UNSPECIFIED | 0 | 相机位置未指定。 | 1155| CAMERA_POSITION_BACK | 1 | 后置相机。 | 1156| CAMERA_POSITION_FRONT | 2 | 前置相机。 | 1157 1158## CameraType 1159 1160枚举,相机类型。 1161 1162**系统能力:** SystemCapability.Multimedia.Camera.Core 1163 1164| 名称 | 值 | 说明 | 1165| ----------------------- | ---- | -------------- | 1166| CAMERA_TYPE_DEFAULT | 0 | 相机类型未指定。 | 1167| CAMERA_TYPE_WIDE_ANGLE | 1 | 广角相机。 | 1168| CAMERA_TYPE_ULTRA_WIDE | 2 | 超广角相机。 | 1169| CAMERA_TYPE_TELEPHOTO | 3 | 长焦相机。 | 1170| CAMERA_TYPE_TRUE_DEPTH | 4 | 带景深信息的相机。 | 1171 1172## ConnectionType 1173 1174枚举,相机连接类型。 1175 1176**系统能力:** SystemCapability.Multimedia.Camera.Core 1177 1178| 名称 | 值 | 说明 | 1179| ---------------------------- | ---- | ------------- | 1180| CAMERA_CONNECTION_BUILT_IN | 0 | 内置相机。 | 1181| CAMERA_CONNECTION_USB_PLUGIN | 1 | USB连接的相机。 | 1182| CAMERA_CONNECTION_REMOTE | 2 | 远程连接的相机。 | 1183 1184## HostDeviceType 1185 1186枚举,远端相机设备类型。 1187 1188**系统接口:** 此接口为系统接口。 1189 1190**系统能力:** SystemCapability.Multimedia.Camera.Core 1191 1192| 名称 | 值 | 说明 | 1193| ---------------------------- | ---- | ------------- | 1194| UNKNOWN_TYPE | 0 | 未知设备类型。 | 1195| PHONE | 0x0E | 智能手机相机设备类型。 | 1196| TABLET | 0x11 | 平板电脑相机设备类型。 | 1197 1198## CameraDevice 1199 1200相机设备信息。 1201 1202**系统能力:** SystemCapability.Multimedia.Camera.Core。 1203 1204| 名称 | 类型 | 必填 | 说明 | 1205| -------------- | --------------------------------- | ---- | ---------- | 1206| cameraId | string | 是 | CameraDevice对象| 1207| cameraPosition | [CameraPosition](#cameraposition) | 是 | 相机位置。 | 1208| cameraType | [CameraType](#cameratype) | 是 | 相机类型。 | 1209| connectionType | [ConnectionType](#connectiontype) | 是 | 相机连接类型。 | 1210| hostDeviceName | string | 是 | 远端设备名称,**系统接口:** 此接口为系统接口。 | 1211| hostDeviceType | [hostDeviceType](#hostdevicetype) | 是 | 远端相机设备类型,**系统接口:** 此接口为系统接口。 | 1212 1213## Size 1214 1215枚举,输出能力查询。 1216 1217**系统能力:** SystemCapability.Multimedia.Camera.Core 1218 1219| 名称 | 类型 | 可读 | 可写 | 说明 | 1220| ------ | ------ | ---- | ---- | ------------ | 1221| height | number | 是 | 是 | 图像尺寸高(像素)。 | 1222| width | number | 是 | 是 | 图像尺寸宽(像素)。 | 1223 1224## Point 1225 1226枚举,点坐标用于对焦、曝光配置。 1227 1228**系统能力:** SystemCapability.Multimedia.Camera.Core 1229 1230| 名称 | 类型 | 必填 | 说明 | 1231| ------ | ------ | ---- | ------------ | 1232| x | number | 是 | 点的x坐标。 | 1233| y | number | 是 | 点的y坐标。 | 1234 1235## CameraFormat 1236 1237枚举,输出格式。 1238 1239**系统能力:** SystemCapability.Multimedia.Camera.Core 1240 1241| 名称 | 值 | 说明 | 1242| ----------------------- | --------- | ------------ | 1243| CAMERA_FORMAT_RGBA_8888 | 3 | RGB格式的图片。 | 1244| CAMERA_FORMAT_YUV_420_SP| 1003 | YUV 420 SP格式的图片。 | 1245| CAMERA_FORMAT_JPEG | 2000 | JPEG格式的图片。 | 1246 1247## CameraInput 1248 1249会话中[CaptureSession](#capturesession)使用的相机信息。 1250 1251### open 1252 1253open\(callback: AsyncCallback\<void\>\): void 1254 1255打开相机,通过注册回调函数获取状态。 1256 1257**系统能力:** SystemCapability.Multimedia.Camera.Core 1258 1259**参数:** 1260 1261| 参数名 | 类型 | 必填 | 说明 | 1262| -------- | -------------------- | ---- | ------------------- | 1263| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1264 1265**错误码:** 1266 1267以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1268 1269| 错误码ID | 错误信息 | 1270| --------------- | --------------- | 1271| 7400107 | Can not use camera cause of conflict. | 1272| 7400108 | Camera disabled cause of security reason. | 1273| 7400201 | Camera service fatal error. | 1274 1275**示例:** 1276 1277```ts 1278import { BusinessError } from '@ohos.base'; 1279 1280function openCameraInput(cameraInput: camera.CameraInput): void { 1281 cameraInput.open((err: BusinessError) => { 1282 if (err) { 1283 console.error(`Failed to open the camera. ${err.code}`); 1284 return; 1285 } 1286 console.info('Callback returned with camera opened.'); 1287 }); 1288} 1289``` 1290 1291### open 1292 1293open(): Promise\<void\> 1294 1295打开相机,通过Promise获取相机的状态。 1296 1297**系统能力:** SystemCapability.Multimedia.Camera.Core 1298 1299**返回值:** 1300 1301| 类型 | 说明 | 1302| -------------- | ----------------------- | 1303| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1304 1305**错误码:** 1306 1307以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1308 1309| 错误码ID | 错误信息 | 1310| --------------- | --------------- | 1311| 7400107 | Can not use camera cause of conflict. | 1312| 7400108 | Camera disabled cause of security reason. | 1313| 7400201 | Camera service fatal error. | 1314 1315**示例:** 1316 1317```ts 1318import { BusinessError } from '@ohos.base'; 1319 1320function openCameraInput(cameraInput: camera.CameraInput): void { 1321 cameraInput.open().then(() => { 1322 console.info('Promise returned with camera opened.'); 1323 }).catch((err: BusinessError) => { 1324 console.error(`Failed to open the camera. ${err.code}`); 1325 }); 1326} 1327``` 1328 1329### close 1330 1331close\(callback: AsyncCallback\<void\>\): void 1332 1333关闭相机,通过注册回调函数获取状态。 1334 1335**系统能力:** SystemCapability.Multimedia.Camera.Core 1336 1337**参数:** 1338 1339| 参数名 | 类型 | 必填 | 说明 | 1340| -------- | -------------------- | ---- | -------------------- | 1341| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1342 1343**错误码:** 1344 1345以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1346 1347| 错误码ID | 错误信息 | 1348| --------------- | --------------- | 1349| 7400201 | Camera service fatal error. | 1350 1351**示例:** 1352 1353```ts 1354import { BusinessError } from '@ohos.base'; 1355 1356function closeCameraInput(cameraInput: camera.CameraInput): void { 1357 cameraInput.close((err: BusinessError) => { 1358 if (err) { 1359 console.error(`Failed to close the cameras. ${err.code}`); 1360 return; 1361 } 1362 console.info('Callback returned with camera closed.'); 1363 }); 1364} 1365``` 1366 1367### close 1368 1369close(): Promise\<void\> 1370 1371关闭相机,通过Promise获取状态。 1372 1373**系统能力:** SystemCapability.Multimedia.Camera.Core 1374 1375**返回值:** 1376 1377| 类型 | 说明 | 1378| -------------- | ----------------------- | 1379| Promise\<void\> | 使用Promise的方式获取结果。 | 1380 1381**错误码:** 1382 1383以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1384 1385| 错误码ID | 错误信息 | 1386| --------------- | --------------- | 1387| 7400201 | Camera service fatal error. | 1388 1389**示例:** 1390 1391```ts 1392import { BusinessError } from '@ohos.base'; 1393 1394function closeCameraInput(cameraInput: camera.CameraInput): void { 1395 cameraInput.close().then(() => { 1396 console.info('Promise returned with camera closed.'); 1397 }).catch((err: BusinessError) => { 1398 console.error(`Failed to close the cameras. ${err.code}`); 1399 }); 1400} 1401``` 1402 1403### on('error') 1404 1405on(type: 'error', camera:CameraDevice, callback: ErrorCallback): void 1406 1407监听CameraInput的错误事件,通过注册回调函数获取结果。 1408 1409**系统能力:** SystemCapability.Multimedia.Camera.Core 1410 1411**参数:** 1412 1413| 参数名 | 类型 | 必填 | 说明 | 1414| -------- | -------------------------------- | --- | ------------------------------------------- | 1415| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如(设备不可用或者冲突等返回对应错误信息) | 1416| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 | 1417| callback | ErrorCallback | 是 | 回调函数,用于获取结果。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1418 1419**示例:** 1420 1421```ts 1422import { BusinessError } from '@ohos.base'; 1423 1424function registerCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { 1425 cameraInput.on('error', cameraDevice, (error: BusinessError) => { 1426 console.info(`Camera input error code: ${error.code}`); 1427 }); 1428} 1429``` 1430 1431### off('error') 1432 1433off(type: 'error', camera:CameraDevice, callback?: ErrorCallback): void 1434 1435注销监听CameraInput的错误事件。 1436 1437**系统能力:** SystemCapability.Multimedia.Camera.Core 1438 1439**参数:** 1440 1441| 参数名 | 类型 | 必填 | 说明 | 1442| -------- | -------------------------------- | --- | ------------------------------------------- | 1443| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如(设备不可用或者冲突等返回对应错误信息) | 1444| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 | 1445| callback | ErrorCallback | 否 | 回调函数,可选,有就是匹配on('error') callback | 1446 1447**示例:** 1448 1449```ts 1450 1451function unregisterCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { 1452 cameraInput.off('error', cameraDevice); 1453} 1454``` 1455 1456## FlashMode 1457 1458枚举,闪光灯模式。 1459 1460**系统能力:** SystemCapability.Multimedia.Camera.Core。 1461 1462| 名称 | 值 | 说明 | 1463| ---------------------- | ---- | ---------- | 1464| FLASH_MODE_CLOSE | 0 | 闪光灯关闭。 | 1465| FLASH_MODE_OPEN | 1 | 闪光灯打开。 | 1466| FLASH_MODE_AUTO | 2 | 自动闪光灯。 | 1467| FLASH_MODE_ALWAYS_OPEN | 3 | 闪光灯常亮。 | 1468 1469## ExposureMode 1470 1471枚举,曝光模式。 1472 1473**系统能力:** SystemCapability.Multimedia.Camera.Core。 1474 1475| 名称 | 值 | 说明 | 1476| ----------------------------- | ---- | ----------- | 1477| EXPOSURE_MODE_LOCKED | 0 | 锁定曝光模式。不支持曝光区域中心点设置。 | 1478| EXPOSURE_MODE_AUTO | 1 | 自动曝光模式。支持曝光区域中心点设置,可以使用[setMeteringPoint](#setmeteringpoint)设置曝光区域中心点。 | 1479| EXPOSURE_MODE_CONTINUOUS_AUTO | 2 | 连续自动曝光。不支持曝光区域中心点设置。 | 1480 1481## FocusMode 1482 1483枚举,焦距模式。 1484 1485**系统能力:** SystemCapability.Multimedia.Camera.Core。 1486 1487| 名称 | 值 | 说明 | 1488| -------------------------- | ---- | ------------ | 1489| FOCUS_MODE_MANUAL | 0 | 手动对焦。通过手动修改相机焦距来改变对焦位置,不支持对焦点设置。 | 1490| FOCUS_MODE_CONTINUOUS_AUTO | 1 | 连续自动对焦。不支持对焦点设置。 | 1491| FOCUS_MODE_AUTO | 2 | 自动对焦。支持对焦点设置,可以使用[setFocusPoint](#setfocuspoint)设置对焦点,根据对焦点执行一次自动对焦。对焦动作完成后(无论对焦成功或是对焦失败),都进入对焦锁定。应用层需要再次调用CONTINUOUS_AUTO后才能再次进入连续自动对焦。 | 1492| FOCUS_MODE_LOCKED | 3 | 对焦锁定。不支持对焦点设置。 | 1493 1494## FocusState 1495 1496枚举,焦距状态。 1497 1498**系统能力:** SystemCapability.Multimedia.Camera.Core。 1499 1500| 名称 | 值 | 说明 | 1501| --------------------- | ---- | --------- | 1502| FOCUS_STATE_SCAN | 0 | 触发对焦。 | 1503| FOCUS_STATE_FOCUSED | 1 | 对焦成功。 | 1504| FOCUS_STATE_UNFOCUSED | 2 | 未完成对焦。 | 1505 1506## VideoStabilizationMode 1507 1508枚举,视频防抖模式。 1509 1510**系统能力:** SystemCapability.Multimedia.Camera.Core。 1511 1512| 名称 | 值 | 说明 | 1513| --------- | ---- | ------------ | 1514| OFF | 0 | 关闭视频防抖功能。 | 1515| LOW | 1 | 使用基础防抖算法。 | 1516| MIDDLE | 2 | 使用防抖效果一般的防抖算法,防抖效果优于LOW类型。 | 1517| HIGH | 3 | 使用防抖效果最好的防抖算法,防抖效果优于MIDDLE类型。 | 1518| AUTO | 4 | 自动进行选择。 | 1519 1520## CaptureSession 1521 1522拍照会话类,保存一次相机运行所需要的所有资源[CameraInput](#camerainput)、[CameraOutput](#cameraoutput),并向相机设备申请完成相机功能(录像,拍照)。 1523 1524### beginConfig 1525 1526beginConfig(): void 1527 1528开始配置会话。 1529 1530**系统能力:** SystemCapability.Multimedia.Camera.Core 1531 1532**返回值:** 1533 1534| 类型 | 说明 | 1535| ---------- | ----------------------------- | 1536| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1537 1538**错误码:** 1539 1540以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1541 1542| 错误码ID | 错误信息 | 1543| --------------- | --------------- | 1544| 7400105 | Session config locked. | 1545 1546**示例:** 1547 1548```ts 1549import { BusinessError } from '@ohos.base'; 1550 1551function beginConfig(captureSession: camera.CaptureSession): void { 1552 try { 1553 captureSession.beginConfig(); 1554 } catch (error) { 1555 // 失败返回错误码error.code并处理 1556 let err = error as BusinessError; 1557 console.error(`The beginConfig call failed. error code: ${err.code}`); 1558 } 1559} 1560``` 1561 1562### commitConfig 1563 1564commitConfig(callback: AsyncCallback\<void\>): void 1565 1566提交配置信息,通过注册回调函数获取结果。 1567 1568**系统能力:** SystemCapability.Multimedia.Camera.Core 1569 1570**参数:** 1571 1572| 参数名 | 类型 | 必填 | 说明 | 1573| -------- | -------------------- | ---- | -------------------- | 1574| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1575 1576**错误码:** 1577 1578以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1579 1580| 错误码ID | 错误信息 | 1581| --------------- | --------------- | 1582| 7400102 | Operation not allow. | 1583| 7400201 | Camera service fatal error. | 1584 1585**示例:** 1586 1587```ts 1588import { BusinessError } from '@ohos.base'; 1589 1590function commitConfig(captureSession: camera.CaptureSession): void { 1591 captureSession.commitConfig((err: BusinessError) => { 1592 if (err) { 1593 console.error(`The commitConfig call failed. error code: ${err.code}`); 1594 return; 1595 } 1596 console.info('Callback invoked to indicate the commit config success.'); 1597 }); 1598} 1599``` 1600 1601### commitConfig 1602 1603commitConfig(): Promise\<void\> 1604 1605提交配置信息,通过Promise获取结果。 1606 1607**系统能力:** SystemCapability.Multimedia.Camera.Core 1608 1609**返回值:** 1610 1611| 类型 | 说明 | 1612| -------------- | ------------------------ | 1613| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1614 1615**错误码:** 1616 1617以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1618 1619| 错误码ID | 错误信息 | 1620| --------------- | --------------- | 1621| 7400102 | Operation not allow. | 1622| 7400201 | Camera service fatal error. | 1623 1624**示例:** 1625 1626```ts 1627import { BusinessError } from '@ohos.base'; 1628 1629function commitConfig(captureSession: camera.CaptureSession): void { 1630 captureSession.commitConfig().then(() => { 1631 console.info('Promise returned to indicate the commit config success.'); 1632 }).catch((err: BusinessError) => { 1633 // 失败返回错误码error.code并处理 1634 console.error(`The commitConfig call failed. error code: ${err.code}`); 1635 }); 1636} 1637``` 1638 1639### addInput 1640 1641addInput(cameraInput: CameraInput): void 1642 1643把[CameraInput](#camerainput)加入到会话。 1644 1645**系统能力:** SystemCapability.Multimedia.Camera.Core 1646 1647**参数:** 1648 1649| 参数名 | 类型 | 必填 | 说明 | 1650| ----------- | --------------------------- | ---- | ------------------------ | 1651| cameraInput | [CameraInput](#camerainput) | 是 | 需要添加的CameraInput实例。 | 1652 1653**返回值:** 1654 1655| 类型 | 说明 | 1656| ---------- | ----------------------------- | 1657| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1658 1659**错误码:** 1660 1661以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1662 1663| 错误码ID | 错误信息 | 1664| --------------- | --------------- | 1665| 7400101 | Parameter missing or parameter type incorrect | 1666| 7400102 | Operation not allow. | 1667 1668**示例:** 1669 1670```ts 1671import { BusinessError } from '@ohos.base'; 1672 1673function addInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { 1674 try { 1675 captureSession.addInput(cameraInput); 1676 } catch (error) { 1677 // 失败返回错误码error.code并处理 1678 let err = error as BusinessError; 1679 console.error(`The addInput call failed. error code: ${err.code}`); 1680 } 1681} 1682``` 1683 1684### removeInput 1685 1686removeInput(cameraInput: CameraInput): void 1687 1688移除[CameraInput](#camerainput)。 1689 1690**系统能力:** SystemCapability.Multimedia.Camera.Core 1691 1692**参数:** 1693 1694| 参数名 | 类型 | 必填 | 说明 | 1695| ----------- | --------------------------- | ---- | ------------------------ | 1696| cameraInput | [CameraInput](#camerainput) | 是 | 需要移除的CameraInput实例。 | 1697 1698**返回值:** 1699 1700| 类型 | 说明 | 1701| ---------- | ----------------------------- | 1702| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1703 1704**错误码:** 1705 1706以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1707 1708| 错误码ID | 错误信息 | 1709| --------------- | --------------- | 1710| 7400101 | Parameter missing or parameter type incorrect | 1711| 7400102 | Operation not allow. | 1712 1713**示例:** 1714 1715```ts 1716import { BusinessError } from '@ohos.base'; 1717 1718function removeInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { 1719 try { 1720 captureSession.removeInput(cameraInput); 1721 } catch (error) { 1722 // 失败返回错误码error.code并处理 1723 let err = error as BusinessError; 1724 console.error(`The removeInput call failed. error code: ${err.code}`); 1725 } 1726} 1727``` 1728 1729### addOutput 1730 1731addOutput(cameraOutput: CameraOutput): void 1732 1733把[CameraOutput](#cameraoutput)加入到会话。 1734 1735**系统能力:** SystemCapability.Multimedia.Camera.Core 1736 1737**参数:** 1738 1739| 参数名 | 类型 | 必填 | 说明 | 1740| ------------- | ------------------------------- | ---- | ------------------------ | 1741| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要添加的CameraOutput实例。 | 1742 1743**返回值:** 1744 1745| 类型 | 说明 | 1746| ---------- | ----------------------------- | 1747| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1748 1749**错误码:** 1750 1751以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1752 1753| 错误码ID | 错误信息 | 1754| --------------- | --------------- | 1755| 7400101 | Parameter missing or parameter type incorrect | 1756| 7400102 | Operation not allow. | 1757 1758**示例:** 1759 1760```ts 1761import { BusinessError } from '@ohos.base'; 1762 1763function addOutput(captureSession: camera.CaptureSession, cameraOutput: camera.CameraOutput): void { 1764 try { 1765 captureSession.addOutput(cameraOutput); 1766 } catch (error) { 1767 // 失败返回错误码error.code并处理 1768 let err = error as BusinessError; 1769 console.error(`The addOutput call failed. error code: ${err.code}`); 1770 } 1771} 1772``` 1773 1774### removeOutput 1775 1776removeOutput(cameraOutput: CameraOutput): void 1777 1778从会话中移除[CameraOutput](#cameraoutput)。 1779 1780**系统能力:** SystemCapability.Multimedia.Camera.Core 1781 1782**参数:** 1783 1784| 参数名 | 类型 | 必填 | 说明 | 1785| ------------- | ------------------------------- | ---- | ------------------------ | 1786| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要移除的CameraOutput实例。 | 1787 1788**返回值:** 1789 1790| 类型 | 说明 | 1791| ---------- | ----------------------------- | 1792| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1793 1794**错误码:** 1795 1796以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1797 1798| 错误码ID | 错误信息 | 1799| --------------- | --------------- | 1800| 7400101 | Parameter missing or parameter type incorrect | 1801| 7400102 | Operation not allow. | 1802 1803**示例:** 1804 1805```ts 1806import { BusinessError } from '@ohos.base'; 1807 1808function removeOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void { 1809 try { 1810 captureSession.removeOutput(previewOutput); 1811 } catch (error) { 1812 // 失败返回错误码error.code并处理 1813 let err = error as BusinessError; 1814 console.error(`The removeOutput call failed. error code: ${err.code}`); 1815 } 1816} 1817``` 1818 1819### start 1820 1821start\(callback: AsyncCallback\<void\>\): void 1822 1823开始会话工作,通过注册回调函数获取结果。 1824 1825**系统能力:** SystemCapability.Multimedia.Camera.Core 1826 1827**参数:** 1828 1829| 参数名 | 类型 | 必填 | 说明 | 1830| -------- | -------------------- | ---- | -------------------- | 1831| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1832 1833**错误码:** 1834 1835以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1836 1837| 错误码ID | 错误信息 | 1838| --------------- | --------------- | 1839| 7400103 | Session not config. | 1840| 7400201 | Camera service fatal error. | 1841 1842**示例:** 1843 1844```ts 1845import { BusinessError } from '@ohos.base'; 1846 1847function startCaptureSession(captureSession: camera.CaptureSession): void { 1848 captureSession.start((err: BusinessError) => { 1849 if (err) { 1850 console.error(`Failed to start the session ${err.code}`); 1851 return; 1852 } 1853 console.info('Callback invoked to indicate the session start success.'); 1854 }); 1855} 1856``` 1857 1858### start 1859 1860start\(\): Promise\<void\> 1861 1862开始会话工作,通过Promise获取结果。 1863 1864**系统能力:** SystemCapability.Multimedia.Camera.Core 1865 1866**返回值:** 1867 1868| 类型 | 说明 | 1869| -------------- | ------------------------ | 1870| Promise\<void\> | 使用Promise的方式获取结果。 | 1871 1872**错误码:** 1873 1874以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1875 1876| 错误码ID | 错误信息 | 1877| --------------- | --------------- | 1878| 7400103 | Session not config. | 1879| 7400201 | Camera service fatal error. | 1880 1881**示例:** 1882 1883```ts 1884import { BusinessError } from '@ohos.base'; 1885 1886function startCaptureSession(captureSession: camera.CaptureSession): void { 1887 captureSession.start().then(() => { 1888 console.info('Promise returned to indicate the session start success.'); 1889 }).catch((err: BusinessError) => { 1890 console.error(`Failed to start the session ${err.code}`); 1891 }); 1892} 1893``` 1894 1895### stop 1896 1897stop\(callback: AsyncCallback\<void\>\): void 1898 1899停止会话工作,通过注册回调函数获取结果。 1900 1901**系统能力:** SystemCapability.Multimedia.Camera.Core 1902 1903**参数:** 1904 1905| 参数名 | 类型 | 必填 | 说明 | 1906| -------- | -------------------- | ---- | ------------------- | 1907| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1908 1909**错误码:** 1910 1911以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1912 1913| 错误码ID | 错误信息 | 1914| --------------- | --------------- | 1915| 7400201 | Camera service fatal error. | 1916 1917**示例:** 1918 1919```ts 1920import { BusinessError } from '@ohos.base'; 1921 1922function stopCaptureSession(captureSession: camera.CaptureSession): void { 1923 captureSession.stop((err: BusinessError) => { 1924 if (err) { 1925 console.error(`Failed to stop the session ${err.code}`); 1926 return; 1927 } 1928 console.info('Callback invoked to indicate the session stop success.'); 1929 }); 1930} 1931``` 1932 1933### stop 1934 1935stop(): Promise\<void\> 1936 1937停止会话工作,通过Promise获取结果。 1938 1939**系统能力:** SystemCapability.Multimedia.Camera.Core 1940 1941**返回值:** 1942 1943| 类型 | 说明 | 1944| -------------- | ----------------------- | 1945| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1946 1947**错误码:** 1948 1949以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1950 1951| 错误码ID | 错误信息 | 1952| --------------- | --------------- | 1953| 7400201 | Camera service fatal error. | 1954 1955**示例:** 1956 1957```ts 1958import { BusinessError } from '@ohos.base'; 1959 1960function stopCaptureSession(captureSession: camera.CaptureSession): void { 1961 captureSession.stop().then(() => { 1962 console.info('Promise returned to indicate the session stop success.'); 1963 }).catch((err: BusinessError) => { 1964 console.error(`Failed to stop the session ${err.code}`); 1965 }); 1966} 1967``` 1968 1969### release 1970 1971release\(callback: AsyncCallback\<void\>\): void 1972 1973释放会话资源,通过注册回调函数获取结果。 1974 1975**系统能力:** SystemCapability.Multimedia.Camera.Core 1976 1977**参数:** 1978 1979| 参数名 | 类型 | 必填 | 说明 | 1980| -------- | -------------------- | ---- | -------------------- | 1981| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1982 1983**错误码:** 1984 1985以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1986 1987| 错误码ID | 错误信息 | 1988| --------------- | --------------- | 1989| 7400201 | Camera service fatal error. | 1990 1991**示例:** 1992 1993```ts 1994import { BusinessError } from '@ohos.base'; 1995 1996function releaseCaptureSession(captureSession: camera.CaptureSession): void { 1997 captureSession.release((err: BusinessError) => { 1998 if (err) { 1999 console.error(`Failed to release the CaptureSession instance ${err.code}`); 2000 return; 2001 } 2002 console.info('Callback invoked to indicate that the CaptureSession instance is released successfully.'); 2003 }); 2004} 2005``` 2006 2007### release 2008 2009release(): Promise\<void\> 2010 2011释放会话资源,通过Promise获取结果。 2012 2013**系统能力:** SystemCapability.Multimedia.Camera.Core 2014 2015**返回值:** 2016 2017| 类型 | 说明 | 2018| -------------- | ------------------------ | 2019| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2020 2021**错误码:** 2022 2023以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2024 2025| 错误码ID | 错误信息 | 2026| --------------- | --------------- | 2027| 7400201 | Camera service fatal error. | 2028 2029**示例:** 2030 2031```ts 2032import { BusinessError } from '@ohos.base'; 2033 2034function releaseCaptureSession(captureSession: camera.CaptureSession): void { 2035 captureSession.release().then(() => { 2036 console.info('Promise returned to indicate that the CaptureSession instance is released successfully.'); 2037 }).catch((err: BusinessError) => { 2038 console.error(`Failed to release the CaptureSession instance ${err.code}`); 2039 }); 2040} 2041``` 2042 2043### hasFlash 2044 2045hasFlash(): boolean 2046 2047检测是否有闪光灯,通过注册回调函数获取结果。 2048 2049**系统能力:** SystemCapability.Multimedia.Camera.Core 2050 2051**返回值:** 2052 2053| 类型 | 说明 | 2054| ---------- | ----------------------------- | 2055| boolean | 返回true表示设备支持闪光灯。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2056 2057**错误码:** 2058 2059以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2060 2061| 错误码ID | 错误信息 | 2062| --------------- | --------------- | 2063| 7400103 | Session not config. | 2064 2065**示例:** 2066 2067```ts 2068import { BusinessError } from '@ohos.base'; 2069 2070function hasFlash(captureSession: camera.CaptureSession): boolean { 2071 let status: boolean = false; 2072 try { 2073 status = captureSession.hasFlash(); 2074 } catch (error) { 2075 // 失败返回错误码error.code并处理 2076 let err = error as BusinessError; 2077 console.error(`The hasFlash call failed. error code: ${err.code}`); 2078 } 2079 return status; 2080} 2081``` 2082 2083### isFlashModeSupported 2084 2085isFlashModeSupported(flashMode: FlashMode): boolean 2086 2087检测闪光灯模式是否支持。 2088 2089**系统能力:** SystemCapability.Multimedia.Camera.Core 2090 2091**参数:** 2092 2093| 参数名 | 类型 | 必填 | 说明 | 2094| --------- | ----------------------- | ---- | --------------------------------- | 2095| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 | 2096 2097**返回值:** 2098 2099| 类型 | 说明 | 2100| ---------- | ----------------------------- | 2101| boolean | 返回true表示支持该闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2102 2103**错误码:** 2104 2105以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2106 2107| 错误码ID | 错误信息 | 2108| --------------- | --------------- | 2109| 7400103 | Session not config. | 2110 2111**示例:** 2112 2113```ts 2114import { BusinessError } from '@ohos.base'; 2115 2116function isFlashModeSupported(captureSession: camera.CaptureSession): boolean { 2117 let status: boolean = false; 2118 try { 2119 status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 2120 } catch (error) { 2121 // 失败返回错误码error.code并处理 2122 let err = error as BusinessError; 2123 console.error(`The isFlashModeSupported call failed. error code: ${err.code}`); 2124 } 2125 return status; 2126} 2127``` 2128 2129### setFlashMode 2130 2131setFlashMode(flashMode: FlashMode): void 2132 2133设置闪光灯模式。 2134 2135进行设置之前,需要先检查: 2136 21371. 设备是否支持闪光灯,可使用方法[hasFlash](#hasflash)。 21382. 设备是否支持指定的闪光灯模式,可使用方法[isFlashModeSupported](#isflashmodesupported)。 2139 2140**系统能力:** SystemCapability.Multimedia.Camera.Core 2141 2142**参数:** 2143 2144| 参数名 | 类型 | 必填 | 说明 | 2145| --------- | ----------------------- | ---- | --------------------- | 2146| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 | 2147 2148**返回值:** 2149 2150| 类型 | 说明 | 2151| ---------- | ----------------------------- | 2152| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2153 2154**错误码:** 2155 2156以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2157 2158| 错误码ID | 错误信息 | 2159| --------------- | --------------- | 2160| 7400103 | Session not config. | 2161 2162**示例:** 2163 2164```ts 2165import { BusinessError } from '@ohos.base'; 2166 2167function setFlashMode(captureSession: camera.CaptureSession): void { 2168 try { 2169 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 2170 } catch (error) { 2171 // 失败返回错误码error.code并处理 2172 let err = error as BusinessError; 2173 console.error(`The setFlashMode call failed. error code: ${err.code}`); 2174 } 2175} 2176``` 2177 2178### getFlashMode 2179 2180getFlashMode(): FlashMode 2181 2182获取当前设备的闪光灯模式。 2183 2184**系统能力:** SystemCapability.Multimedia.Camera.Core 2185 2186**返回值:** 2187 2188| 类型 | 说明 | 2189| ---------- | ----------------------------- | 2190| [FlashMode](#flashmode) | 获取当前设备的闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2191 2192**错误码:** 2193 2194以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2195 2196| 错误码ID | 错误信息 | 2197| --------------- | --------------- | 2198| 7400103 | Session not config. | 2199 2200**示例:** 2201 2202```ts 2203import { BusinessError } from '@ohos.base'; 2204 2205function getFlashMode(captureSession: camera.CaptureSession): camera.FlashMode | undefined { 2206 let flashMode: camera.FlashMode | undefined = undefined; 2207 try { 2208 flashMode = captureSession.getFlashMode(); 2209 } catch (error) { 2210 // 失败返回错误码error.code并处理 2211 let err = error as BusinessError; 2212 console.error(`The getFlashMode call failed.error code: ${err.code}`); 2213 } 2214 return flashMode; 2215} 2216``` 2217 2218### isExposureModeSupported 2219 2220isExposureModeSupported(aeMode: ExposureMode): boolean; 2221 2222检测曝光模式是否支持。 2223 2224**系统能力:** SystemCapability.Multimedia.Camera.Core 2225 2226**参数:** 2227 2228| 参数名 | 类型 | 必填 | 说明 | 2229| -------- | -------------------------------| ---- | ----------------------------- | 2230| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 | 2231 2232**返回值:** 2233 2234| 类型 | 说明 | 2235| ---------- | ----------------------------- | 2236| boolean | 获取是否支持曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2237 2238**错误码:** 2239 2240以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2241 2242| 错误码ID | 错误信息 | 2243| --------------- | --------------- | 2244| 7400103 | Session not config. | 2245 2246**示例:** 2247 2248```ts 2249import { BusinessError } from '@ohos.base'; 2250 2251function isExposureModeSupported(captureSession: camera.CaptureSession): boolean { 2252 let isSupported: boolean = false; 2253 try { 2254 isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED); 2255 } catch (error) { 2256 // 失败返回错误码error.code并处理 2257 let err = error as BusinessError; 2258 console.error(`The isExposureModeSupported call failed. error code: ${err.code}`); 2259 } 2260 return isSupported; 2261} 2262``` 2263 2264### getExposureMode 2265 2266getExposureMode(): ExposureMode 2267 2268获取当前曝光模式。 2269 2270**系统能力:** SystemCapability.Multimedia.Camera.Core 2271 2272**返回值:** 2273 2274| 类型 | 说明 | 2275| ---------- | ----------------------------- | 2276| [ExposureMode](#exposuremode) | 获取当前曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2277 2278**错误码:** 2279 2280以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2281 2282| 错误码ID | 错误信息 | 2283| --------------- | --------------- | 2284| 7400103 | Session not config. | 2285 2286**示例:** 2287 2288```ts 2289import { BusinessError } from '@ohos.base'; 2290 2291function getExposureMode(captureSession: camera.CaptureSession): camera.ExposureMode | undefined { 2292 let exposureMode: camera.ExposureMode | undefined = undefined; 2293 try { 2294 exposureMode = captureSession.getExposureMode(); 2295 } catch (error) { 2296 // 失败返回错误码error.code并处理 2297 let err = error as BusinessError; 2298 console.error(`The getExposureMode call failed. error code: ${err.code}`); 2299 } 2300 return exposureMode; 2301} 2302``` 2303 2304### setExposureMode 2305 2306setExposureMode(aeMode: ExposureMode): void 2307 2308设置曝光模式。进行设置之前,需要先检查设备是否支持指定的曝光模式,可使用方法[isExposureModeSupported](#isexposuremodesupported)。 2309 2310**系统能力:** SystemCapability.Multimedia.Camera.Core 2311 2312**参数:** 2313 2314| 参数名 | 类型 | 必填 | 说明 | 2315| -------- | -------------------------------| ---- | ----------------------- | 2316| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 | 2317 2318**返回值:** 2319 2320| 类型 | 说明 | 2321| ---------- | ----------------------------- | 2322| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2323 2324**错误码:** 2325 2326以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2327 2328| 错误码ID | 错误信息 | 2329| --------------- | --------------- | 2330| 7400103 | Session not config. | 2331 2332**示例:** 2333 2334```ts 2335import { BusinessError } from '@ohos.base'; 2336 2337function setExposureMode(captureSession: camera.CaptureSession): void { 2338 try { 2339 captureSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED); 2340 } catch (error) { 2341 // 失败返回错误码error.code并处理 2342 let err = error as BusinessError; 2343 console.error(`The setExposureMode call failed. error code: ${err.code}`); 2344 } 2345} 2346``` 2347 2348### getMeteringPoint 2349 2350getMeteringPoint(): Point 2351 2352查询曝光区域中心点。(该接口目前为预留,将在3.2版本开放) 2353 2354**系统能力:** SystemCapability.Multimedia.Camera.Core 2355 2356**返回值:** 2357 2358| 类型 | 说明 | 2359| ---------- | ----------------------------- | 2360| [Point](#point) | 获取当前曝光点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2361 2362**错误码:** 2363 2364以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2365 2366| 错误码ID | 错误信息 | 2367| --------------- | --------------- | 2368| 7400103 | Session not config. | 2369 2370**示例:** 2371 2372```ts 2373import { BusinessError } from '@ohos.base'; 2374 2375function getMeteringPoint(captureSession: camera.CaptureSession): camera.Point | undefined { 2376 let exposurePoint: camera.Point | undefined = undefined; 2377 try { 2378 exposurePoint = captureSession.getMeteringPoint(); 2379 } catch (error) { 2380 // 失败返回错误码error.code并处理 2381 let err = error as BusinessError; 2382 console.error(`The getMeteringPoint call failed. error code: ${err.code}`); 2383 } 2384 return exposurePoint; 2385} 2386``` 2387 2388### setMeteringPoint 2389 2390setMeteringPoint(point: Point): void 2391 2392设置曝光区域中心点,曝光点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。 2393此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以 2394设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y}, 2395则转换后的坐标点为{y/h,1-x/w}。 2396 2397**系统能力:** SystemCapability.Multimedia.Camera.Core 2398 2399**参数:** 2400 2401| 参数名 | 类型 | 必填 | 说明 | 2402| ------------- | -------------------------------| ---- | ------------------- | 2403| exposurePoint | [Point](#point) | 是 | 曝光点,x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 | 2404 2405**返回值:** 2406 2407| 类型 | 说明 | 2408| ---------- | ----------------------------- | 2409| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2410 2411**错误码:** 2412 2413以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2414 2415| 错误码ID | 错误信息 | 2416| --------------- | --------------- | 2417| 7400103 | Session not config. | 2418 2419**示例:** 2420 2421```ts 2422import { BusinessError } from '@ohos.base'; 2423 2424function setMeteringPoint(captureSession: camera.CaptureSession): void { 2425 const exposurePoint: camera.Point = {x: 1, y: 1}; 2426 try { 2427 captureSession.setMeteringPoint(exposurePoint); 2428 } catch (error) { 2429 // 失败返回错误码error.code并处理 2430 let err = error as BusinessError; 2431 console.error(`The setMeteringPoint call failed. error code: ${err.code}`); 2432 } 2433} 2434``` 2435 2436### getExposureBiasRange 2437 2438getExposureBiasRange(): Array\<number\> 2439 2440查询曝光补偿范围。 2441 2442**系统能力:** SystemCapability.Multimedia.Camera.Core 2443 2444**返回值:** 2445 2446| 类型 | 说明 | 2447| ---------- | ----------------------------- | 2448| Array\<number\> | 获取补偿范围的数组。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2449 2450**错误码:** 2451 2452以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2453 2454| 错误码ID | 错误信息 | 2455| --------------- | --------------- | 2456| 7400103 | Session not config. | 2457 2458**示例:** 2459 2460```ts 2461import { BusinessError } from '@ohos.base'; 2462 2463function getExposureBiasRange(captureSession: camera.CaptureSession): Array<number> { 2464 let biasRangeArray: Array<number> = []; 2465 try { 2466 biasRangeArray = captureSession.getExposureBiasRange(); 2467 } catch (error) { 2468 // 失败返回错误码error.code并处理 2469 let err = error as BusinessError; 2470 console.error(`The getExposureBiasRange call failed. error code: ${err.code}`); 2471 } 2472 return biasRangeArray; 2473} 2474``` 2475 2476### setExposureBias 2477 2478setExposureBias(exposureBias: number): void 2479 2480设置曝光补偿,曝光补偿值(EV)。 2481 2482进行设置之前,建议先通过方法[getExposureBiasRange](#getexposurebiasrange)查询支持的范围。 2483 2484**系统能力:** SystemCapability.Multimedia.Camera.Core 2485 2486**参数:** 2487 2488| 参数名 | 类型 | 必填 | 说明 | 2489| -------- | -------------------------------| ---- | ------------------- | 2490| exposureBias | number | 是 | 曝光补偿,getExposureBiasRange查询支持的范围,如果设置超过支持范围的值,自动匹配到就近临界点。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode), | 2491 2492**错误码:** 2493 2494以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2495 2496| 错误码ID | 错误信息 | 2497| --------------- | --------------- | 2498| 7400103 | Session not config. | 2499 2500**示例:** 2501 2502```ts 2503import { BusinessError } from '@ohos.base'; 2504 2505function setExposureBias(captureSession: camera.CaptureSession, biasRangeArray: Array<number>): void { 2506 if (biasRangeArray && biasRangeArray.length > 0) { 2507 let exposureBias = biasRangeArray[0]; 2508 try { 2509 captureSession.setExposureBias(exposureBias); 2510 } catch (error) { 2511 // 失败返回错误码error.code并处理 2512 let err = error as BusinessError; 2513 console.error(`The setExposureBias call failed. error code: ${err.code}`); 2514 } 2515 } 2516} 2517``` 2518 2519### getExposureValue 2520 2521getExposureValue(): number 2522 2523查询当前曝光值。 2524 2525**系统能力:** SystemCapability.Multimedia.Camera.Core 2526 2527**返回值:** 2528 2529| 类型 | 说明 | 2530| ---------- | ----------------------------- | 2531| number | 获取曝光值。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2532 2533**错误码:** 2534 2535以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2536 2537| 错误码ID | 错误信息 | 2538| --------------- | --------------- | 2539| 7400103 | Session not config. | 2540 2541**示例:** 2542 2543```ts 2544import { BusinessError } from '@ohos.base'; 2545 2546function getExposureValue(captureSession: camera.CaptureSession): number { 2547 const invalid_value: number = -1; 2548 let exposureValue: number = invalid_value; 2549 try { 2550 exposureValue = captureSession.getExposureValue(); 2551 } catch (error) { 2552 // 失败返回错误码error.code并处理 2553 let err = error as BusinessError; 2554 console.error(`The getExposureValue call failed. error code: ${err.code}`); 2555 } 2556 return exposureValue; 2557} 2558``` 2559 2560### isFocusModeSupported 2561 2562isFocusModeSupported(afMode: FocusMode): boolean 2563 2564检测对焦模式是否支持。 2565 2566**系统能力:** SystemCapability.Multimedia.Camera.Core 2567 2568**参数:** 2569 2570| 参数名 | 类型 | 必填 | 说明 | 2571| -------- | ----------------------- | ---- | -------------------------------- | 2572| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 | 2573 2574**返回值:** 2575 2576| 类型 | 说明 | 2577| ---------- | ----------------------------- | 2578| boolean | 返回true表示支持该焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2579 2580**错误码:** 2581 2582以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2583 2584| 错误码ID | 错误信息 | 2585| --------------- | --------------- | 2586| 7400103 | Session not config. | 2587 2588**示例:** 2589 2590```ts 2591import { BusinessError } from '@ohos.base'; 2592 2593function isFocusModeSupported(captureSession: camera.CaptureSession): boolean { 2594 let status: boolean = false; 2595 try { 2596 status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO); 2597 } catch (error) { 2598 // 失败返回错误码error.code并处理 2599 let err = error as BusinessError; 2600 console.error(`The isFocusModeSupported call failed. error code: ${err.code}`); 2601 } 2602 return status; 2603} 2604``` 2605 2606### setFocusMode 2607 2608setFocusMode(afMode: FocusMode): void 2609 2610设置对焦模式。 2611 2612进行设置之前,需要先检查设备是否支持指定的焦距模式,可使用方法[isFocusModeSupported](#isfocusmodesupported)。 2613 2614**系统能力:** SystemCapability.Multimedia.Camera.Core 2615 2616**参数:** 2617 2618| 参数名 | 类型 | 必填 | 说明 | 2619| -------- | ----------------------- | ---- | ------------------- | 2620| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 | 2621 2622**返回值:** 2623 2624| 类型 | 说明 | 2625| ---------- | ----------------------------- | 2626| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2627 2628**错误码:** 2629 2630以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2631 2632| 错误码ID | 错误信息 | 2633| --------------- | --------------- | 2634| 7400103 | Session not config. | 2635 2636**示例:** 2637 2638```ts 2639import { BusinessError } from '@ohos.base'; 2640 2641function setFocusMode(captureSession: camera.CaptureSession): void { 2642 try { 2643 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO); 2644 } catch (error) { 2645 // 失败返回错误码error.code并处理 2646 let err = error as BusinessError; 2647 console.error(`The setFocusMode call failed. error code: ${err.code}`); 2648 } 2649} 2650``` 2651 2652### getFocusMode 2653 2654getFocusMode(): FocusMode 2655 2656获取当前的对焦模式。 2657 2658**系统能力:** SystemCapability.Multimedia.Camera.Core 2659 2660**返回值:** 2661 2662| 类型 | 说明 | 2663| ---------- | ----------------------------- | 2664| [FocusMode](#focusmode) | 获取当前设备的焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2665 2666**错误码:** 2667 2668以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2669 2670| 错误码ID | 错误信息 | 2671| --------------- | --------------- | 2672| 7400103 | Session not config. | 2673 2674**示例:** 2675 2676```ts 2677import { BusinessError } from '@ohos.base'; 2678 2679function getFocusMode(captureSession: camera.CaptureSession): camera.FocusMode | undefined { 2680 let afMode: camera.FocusMode | undefined = undefined; 2681 try { 2682 afMode = captureSession.getFocusMode(); 2683 } catch (error) { 2684 // 失败返回错误码error.code并处理 2685 let err = error as BusinessError; 2686 console.error(`The getFocusMode call failed. error code: ${err.code}`); 2687 } 2688 return afMode; 2689} 2690``` 2691 2692### setFocusPoint 2693 2694setFocusPoint(point: Point): void 2695 2696设置焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。 2697此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以 2698设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y}, 2699则转换后的坐标点为{y/h,1-x/w}。 2700 2701**系统能力:** SystemCapability.Multimedia.Camera.Core 2702 2703**参数:** 2704 2705| 参数名 | 类型 | 必填 | 说明 | 2706| -------- | ----------------------- | ---- | ------------------- | 2707| Point1 | [Point](#point) | 是 | 焦点。x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 | 2708 2709**返回值:** 2710 2711| 类型 | 说明 | 2712| ---------- | ----------------------------- | 2713| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2714 2715**错误码:** 2716 2717以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2718 2719| 错误码ID | 错误信息 | 2720| --------------- | --------------- | 2721| 7400103 | Session not config. | 2722 2723**示例:** 2724 2725```ts 2726import { BusinessError } from '@ohos.base'; 2727 2728function setFocusPoint(captureSession: camera.CaptureSession): void { 2729 const focusPoint: camera.Point = {x: 1, y: 1}; 2730 try { 2731 captureSession.setFocusPoint(focusPoint); 2732 } catch (error) { 2733 // 失败返回错误码error.code并处理 2734 let err = error as BusinessError; 2735 console.error(`The setFocusPoint call failed. error code: ${err.code}`); 2736 } 2737} 2738``` 2739 2740### getFocusPoint 2741 2742getFocusPoint(): Point 2743 2744查询焦点。 2745 2746**系统能力:** SystemCapability.Multimedia.Camera.Core 2747 2748**返回值:** 2749 2750| 类型 | 说明 | 2751| ---------- | ----------------------------- | 2752| [Point](#point) | 用于获取当前焦点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2753 2754**错误码:** 2755 2756以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2757 2758| 错误码ID | 错误信息 | 2759| --------------- | --------------- | 2760| 7400103 | Session not config. | 2761 2762**示例:** 2763 2764```ts 2765import { BusinessError } from '@ohos.base'; 2766 2767function getFocusPoint(captureSession: camera.CaptureSession): camera.Point | undefined { 2768 let point: camera.Point | undefined = undefined; 2769 try { 2770 point = captureSession.getFocusPoint(); 2771 } catch (error) { 2772 // 失败返回错误码error.code并处理 2773 let err = error as BusinessError; 2774 console.error(`The getFocusPoint call failed. error code: ${err.code}`); 2775 } 2776 return point; 2777} 2778``` 2779 2780### getFocalLength 2781 2782getFocalLength(): number 2783 2784查询焦距值。 2785 2786**系统能力:** SystemCapability.Multimedia.Camera.Core 2787 2788**返回值:** 2789 2790| 类型 | 说明 | 2791| ---------- | ----------------------------- | 2792| number | 用于获取当前焦距。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2793 2794**错误码:** 2795 2796以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2797 2798| 错误码ID | 错误信息 | 2799| --------------- | --------------- | 2800| 7400103 | Session not config. | 2801 2802**示例:** 2803 2804```ts 2805import { BusinessError } from '@ohos.base'; 2806 2807function getFocalLength(captureSession: camera.CaptureSession): number { 2808 const invalid_value: number = -1; 2809 let focalLength: number = invalid_value; 2810 try { 2811 focalLength = captureSession.getFocalLength(); 2812 } catch (error) { 2813 // 失败返回错误码error.code并处理 2814 let err = error as BusinessError; 2815 console.error(`The getFocalLength call failed. error code: ${err.code}`); 2816 } 2817 return focalLength; 2818} 2819``` 2820 2821### getZoomRatioRange 2822 2823getZoomRatioRange(): Array\<number\> 2824 2825获取支持的变焦范围。 2826 2827**系统能力:** SystemCapability.Multimedia.Camera.Core 2828 2829**返回值:** 2830 2831| 类型 | 说明 | 2832| ---------- | ----------------------------- | 2833| Array\<number\> | 用于获取可变焦距比范围,返回的数组包括其最小值和最大值。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2834 2835**错误码:** 2836 2837以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2838 2839| 错误码ID | 错误信息 | 2840| --------------- | --------------- | 2841| 7400103 | Session not config. | 2842 2843**示例:** 2844 2845```ts 2846import { BusinessError } from '@ohos.base'; 2847 2848function getZoomRatioRange(captureSession: camera.CaptureSession): Array<number> { 2849 let zoomRatioRange: Array<number> = []; 2850 try { 2851 zoomRatioRange = captureSession.getZoomRatioRange(); 2852 } catch (error) { 2853 // 失败返回错误码error.code并处理 2854 let err = error as BusinessError; 2855 console.error(`The getZoomRatioRange call failed. error code: ${err.code}`); 2856 } 2857 return zoomRatioRange; 2858} 2859``` 2860 2861### setZoomRatio 2862 2863setZoomRatio(zoomRatio: number): void 2864 2865设置变焦比,变焦精度最高为小数点后两位,超过变焦精度的变焦值系统无法响应。 2866 2867**系统能力:** SystemCapability.Multimedia.Camera.Core 2868 2869**参数:** 2870 2871| 参数名 | 类型 | 必填 | 说明 | 2872| --------- | -------------------- | ---- | ------------------- | 2873| zoomRatio | number | 是 | 可变焦距比,通过getZoomRatioRange获取支持的变焦范围,如果设置超过支持范围的值,自动匹配到就近临界点。 | 2874 2875**返回值:** 2876 2877| 类型 | 说明 | 2878| ---------- | ----------------------------- | 2879| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2880 2881**错误码:** 2882 2883以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2884 2885| 错误码ID | 错误信息 | 2886| --------------- | --------------- | 2887| 7400103 | Session not config. | 2888 2889**示例:** 2890 2891```ts 2892import { BusinessError } from '@ohos.base'; 2893 2894function setZoomRatio(captureSession: camera.CaptureSession, zoomRatioRange: Array<number>): void { 2895 if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) { 2896 return; 2897 } 2898 let zoomRatio = zoomRatioRange[0]; 2899 try { 2900 captureSession.setZoomRatio(zoomRatio); 2901 } catch (error) { 2902 // 失败返回错误码error.code并处理 2903 let err = error as BusinessError; 2904 console.error(`The setZoomRatio call failed. error code: ${err.code}`); 2905 } 2906} 2907``` 2908 2909### getZoomRatio 2910 2911getZoomRatio(): number 2912 2913获取当前的变焦比。 2914 2915**系统能力:** SystemCapability.Multimedia.Camera.Core 2916 2917**返回值:** 2918 2919| 类型 | 说明 | 2920| ---------- | ----------------------------- | 2921| number | 获取当前的变焦比结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2922 2923**错误码:** 2924 2925以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2926 2927| 错误码ID | 错误信息 | 2928| --------------- | --------------- | 2929| 7400103 | Session not config. | 2930 2931**示例:** 2932 2933```ts 2934import { BusinessError } from '@ohos.base'; 2935 2936function getZoomRatio(captureSession: camera.CaptureSession): number { 2937 const invalid_value: number = -1; 2938 let zoomRatio: number = invalid_value; 2939 try { 2940 zoomRatio = captureSession.getZoomRatio(); 2941 } catch (error) { 2942 // 失败返回错误码error.code并处理 2943 let err = error as BusinessError; 2944 console.error(`The getZoomRatio call failed. error code: ${err.code}`); 2945 } 2946 return zoomRatio; 2947} 2948``` 2949 2950### isVideoStabilizationModeSupported 2951 2952isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean 2953 2954查询是否支持指定的视频防抖模式。 2955 2956**系统能力:** SystemCapability.Multimedia.Camera.Core 2957 2958**参数:** 2959 2960| 参数名 | 类型 | 必填 | 说明 | 2961| -------- | ------------------------------------------------- | ---- | ------------------------------ | 2962| vsMode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 视频防抖模式。 | 2963 2964**返回值:** 2965 2966| 类型 | 说明 | 2967| ---------- | ----------------------------- | 2968| boolean | 返回视频防抖模式是否支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2969 2970**错误码:** 2971 2972以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2973 2974| 错误码ID | 错误信息 | 2975| --------------- | --------------- | 2976| 7400103 | Session not config. | 2977 2978**示例:** 2979 2980```ts 2981import { BusinessError } from '@ohos.base'; 2982 2983function isVideoStabilizationModeSupported(captureSession: camera.CaptureSession): boolean { 2984 let isSupported: boolean = false; 2985 try { 2986 isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF); 2987 } catch (error) { 2988 // 失败返回错误码error.code并处理 2989 let err = error as BusinessError; 2990 console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`); 2991 } 2992 return isSupported; 2993} 2994``` 2995 2996### getActiveVideoStabilizationMode 2997 2998getActiveVideoStabilizationMode(): VideoStabilizationMode 2999 3000查询当前正在使用的视频防抖模式。 3001 3002**系统能力:** SystemCapability.Multimedia.Camera.Core 3003 3004**返回值:** 3005 3006| 类型 | 说明 | 3007| ---------- | ----------------------------- | 3008| [VideoStabilizationMode](#videostabilizationmode) | 视频防抖是否正在使用。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3009 3010**错误码:** 3011 3012以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3013 3014| 错误码ID | 错误信息 | 3015| --------------- | --------------- | 3016| 7400103 | Session not config. | 3017 3018**示例:** 3019 3020```ts 3021import { BusinessError } from '@ohos.base'; 3022 3023function getActiveVideoStabilizationMode(captureSession: camera.CaptureSession): camera.VideoStabilizationMode | undefined { 3024 let vsMode: camera.VideoStabilizationMode | undefined = undefined; 3025 try { 3026 vsMode = captureSession.getActiveVideoStabilizationMode(); 3027 } catch (error) { 3028 // 失败返回错误码error.code并处理 3029 let err = error as BusinessError; 3030 console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`); 3031 } 3032 return vsMode; 3033} 3034``` 3035 3036### setVideoStabilizationMode 3037 3038setVideoStabilizationMode(mode: VideoStabilizationMode): void 3039 3040设置视频防抖模式。需要先检查设备是否支持对应的防抖模式,可以通过[isVideoStabilizationModeSupported](#isvideostabilizationmodesupported)方法判断所设置的模式是否支持。 3041 3042**系统能力:** SystemCapability.Multimedia.Camera.Core 3043 3044**参数:** 3045 3046| 参数名 | 类型 | 必填 | 说明 | 3047| -------- | ------------------------------------------------- | ---- | --------------------- | 3048| mode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 需要设置的视频防抖模式。 | 3049 3050**返回值:** 3051 3052| 类型 | 说明 | 3053| ---------- | ----------------------------- | 3054| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3055 3056**错误码:** 3057 3058以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3059 3060| 错误码ID | 错误信息 | 3061| --------------- | --------------- | 3062| 7400103 | Session not config. | 3063 3064**示例:** 3065 3066```ts 3067import { BusinessError } from '@ohos.base'; 3068 3069function setVideoStabilizationMode(captureSession: camera.CaptureSession): void { 3070 try { 3071 captureSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF); 3072 } catch (error) { 3073 // 失败返回错误码error.code并处理 3074 let err = error as BusinessError; 3075 console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`); 3076 } 3077} 3078``` 3079### getSupportedFilters 3080 3081getSupportedFilters(): Array\<number> 3082 3083获取当前支持的滤镜效果列表。 3084 3085**系统接口:** 此接口为系统接口。 3086 3087**系统能力:** SystemCapability.Multimedia.Camera.Core 3088 3089**返回值:** 3090 3091| 类型 | 说明 | 3092| ---------- | ----------------------------- | 3093| Array\<FilterType\> | 返回支持的滤镜效果列表。 | 3094 3095**错误码:** 3096 3097以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3098 3099| 错误码ID | 错误信息 | 3100| --------------- | --------------- | 3101| 7400103 | Session not config. | 3102 3103**示例:** 3104 3105```ts 3106function getSupportedFilters(portraitSession: camera.PortraitSession): Array<camera.FilterType> { 3107 let filterTypes: Array<camera.FilterType> = portraitSession.getSupportedFilters(); 3108 return filterTypes; 3109} 3110``` 3111### setFilter 3112 3113setFilter(filter: number): void 3114 3115设置滤镜效果。需要先检查设备是否支持对应的滤镜效果,可以通过[getSupportedFilters](#getsupportedfilters)方法获取所支持的类型。 3116 3117**系统接口:** 此接口为系统接口。 3118 3119**系统能力:** SystemCapability.Multimedia.Camera.Core 3120 3121**参数:** 3122 3123| 参数名 | 类型 | 必填 | 说明 | 3124| -------- | ----------------------------| ---- | ---------------------| 3125| filter | [number](#filtertype) | 是 | 当前用户设置的滤镜类型。 | 3126 3127**错误码:** 3128 3129以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3130 3131| 错误码ID | 错误信息 | 3132| --------------- | --------------- | 3133| 7400101 | Parameter missing or parameter type incorrect. | 3134| 7400103 | Session not config. | 3135 3136**示例:** 3137 3138```ts 3139function setFilter(portraitSession: camera.PortraitSession, filterTypes: Array<camera.FilterType>): void { 3140 if (filterTypes === undefined || filterTypes.length <= 0) { 3141 return; 3142 } 3143 portraitSession.setFilter(filterTypes[0]); 3144} 3145``` 3146 3147### getFilter 3148 3149 getFilter(): number 3150 3151获取当前已设置的滤镜效果。 3152 3153**系统接口:** 此接口为系统接口。 3154 3155**系统能力:** SystemCapability.Multimedia.Camera.Core 3156 3157**返回值:** 3158 3159| 类型 | 说明 | 3160| ---------- | ----------------------------| 3161| [number](#filtertype)| 已设置的滤镜效果。可查阅[FilterType](#filtertype)。| 3162 3163**错误码:** 3164 3165以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3166 3167| 错误码ID | 错误信息 | 3168| --------------- | --------------- | 3169| 7400103 | Session not config. | 3170 3171**示例:** 3172 3173```ts 3174function getFilter(portraitSession: camera.PortraitSession): number { 3175 let filterType: number = portraitSession.getFilter(); 3176 return filterType; 3177} 3178``` 3179### getSupportedBeautyTypes 3180 3181getSupportedBeautyTypes(): Array<[BeautyType](#beautytype)> 3182 3183获取当前支持的美颜效果列表。 3184 3185**系统接口:** 此接口为系统接口。 3186 3187**系统能力:** SystemCapability.Multimedia.Camera.Core 3188 3189**返回值:** 3190 3191| 类型 | 说明 | 3192| ---------- | ----------------------------- | 3193| Array\<[BeautyType](#beautytype)\>| 返回当前支持的美颜效果列表。 | 3194 3195**错误码:** 3196 3197以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3198 3199| 错误码ID | 错误信息 | 3200| --------------- | --------------- | 3201| 7400103 | Session not config. | 3202 3203**示例:** 3204 3205```ts 3206function getSupportedBeautyTypes(portraitSession: camera.PortraitSession): Array<camera.BeautyType> { 3207 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3208 return beautyTypes; 3209} 3210``` 3211### getSupportedBeautyRange 3212 3213getSupportedBeautyRange(type: BeautyType): Array\<number\> 3214 3215获取指定美颜效果的范围值。在不同设备返回的美颜强度有所不同,下表仅做示例。 3216 3217| 传入参数 | 示例返回值 | 返回值说明 | 3218| ----------------| ---- | ---------| 3219| AUTO | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |美颜类型为自动时支持的美颜强度,0表明关闭美颜,其余正值表明自动的美颜强度。 | 3220| SKIN_SMOOTH | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | 美颜类型为光滑时支持的美颜强度,0表明关闭光滑,其余正值表明光滑的美颜强度。 | 3221| FACE_SLENDER | [0, 1, 2, 3, 4, 5] | 美颜类型为瘦脸时支持的美颜强度,0表明关闭瘦脸,其余正值表明瘦脸的美颜强度。 | 3222| SKIN_TONE | [-1, 16242611] | 美颜类型为美肤时支持的美颜强度,-1表明关闭美肤,其余非负值为使用RGB表示的美肤美颜强度,<br> 16242611转化为16进制为0xF7D7B3,F7为R通道值,D7为G通道值,B3位B通道值。 | 3223 3224**系统接口:** 此接口为系统接口。 3225 3226**系统能力:** SystemCapability.Multimedia.Camera.Core 3227 3228**参数:** 3229 3230| 参数名 | 类型 | 必填 | 说明 | 3231| -------- | --------------------------| ---- | ----------| 3232| type | [BeautyType](#beautytype) | 是 | 美颜类型。 | 3233 3234**返回值:** 3235 3236| 类型 | 说明 | 3237| ---------- | ----------------------------- | 3238| Array\<number\> | 当前美颜类型所支持的美颜强度。 | 3239 3240**错误码:** 3241 3242以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3243 3244| 错误码ID | 错误信息 | 3245| --------------- | --------------- | 3246| 7400103 | Session not config. | 3247 3248**示例:** 3249 3250```ts 3251function getSupportedBeautyRange(portraitSession: camera.PortraitSession): Array<number> { 3252 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3253 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3254 return []; 3255 } 3256 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3257 return beautyLevels; 3258} 3259``` 3260 3261### setBeauty 3262 3263setBeauty(type: BeautyType, value: number): void 3264 3265设置美颜类型以及对应的美颜强度。将通过[getSupportedBeautyTypes](#getsupportedbeautytypes)获取得到的[BeautyType](#beautytype)都关闭,表明当前美颜关闭;若有一种美颜类型未关闭,表明当前美颜打开。 3266 3267**系统接口:** 此接口为系统接口。 3268 3269**系统能力:** SystemCapability.Multimedia.Camera.Core 3270 3271**参数:** 3272 3273| 参数名 | 类型 | 必填 | 说明 | 3274| -------- | --------------------------| ---- | --------------------- | 3275| type | [BeautyType](#beautytype) | 是 | 美颜类型 | 3276| value | number | 是 | 美颜强度,通过[getSupportedBeautyRange](#getsupportedbeautyrange)接口获取。| 3277 3278**错误码:** 3279 3280以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3281 3282| 错误码ID | 错误信息 | 3283| --------------- | --------------- | 3284| 7400103 | Session not config. | 3285 3286**示例:** 3287 3288```ts 3289function setBeauty(portraitSession: camera.PortraitSession): void { 3290 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3291 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3292 return; 3293 } 3294 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3295 if (beautyLevels === undefined || beautyLevels.length <= 0) { 3296 return; 3297 } 3298 portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]); 3299} 3300``` 3301 3302### getBeauty 3303 3304getBeauty(type: BeautyType): number 3305 3306查询当前已设置的美颜效果对应的美颜强度。 3307 3308**系统接口:** 此接口为系统接口。 3309 3310**系统能力:** SystemCapability.Multimedia.Camera.Core 3311 3312**参数:** 3313 3314| 参数名 | 类型 | 必填 | 说明 | 3315| -------- | ------------------------------------------------- | ---- | --------------------- | 3316| type | [BeautyType](#beautytype) | 是 | 美颜类型 | 3317 3318**返回值:** 3319| 参数名 | 类型 | 必填 | 说明 | 3320| -------- | ------------------------------------------------- | ---- | --------------------- | 3321| value | number | 是 | 美颜强度 | 3322 3323**错误码:** 3324 3325以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3326 3327| 错误码ID | 错误信息 | 3328| --------------- | --------------- | 3329| 7400103 | Session not config. | 3330 3331**示例:** 3332 3333```ts 3334function getBeauty(portraitSession: camera.PortraitSession): number { 3335 const invalid_value: number = -1; 3336 let beautyTypes = portraitSession.getSupportedBeautyTypes(); 3337 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3338 return invalid_value; 3339 } 3340 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3341 if (beautyLevels === undefined || beautyLevels.length <= 0) { 3342 return invalid_value; 3343 } 3344 portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]); 3345 let beautyLevel: number = portraitSession.getBeauty(beautyTypes[0]); 3346 return beautyLevel; 3347} 3348``` 3349 3350### on('focusStateChange') 3351 3352on(type: 'focusStateChange', callback: AsyncCallback\<FocusState\>): void 3353 3354监听相机聚焦的状态变化,通过注册回调函数获取结果。 3355 3356**系统能力:** SystemCapability.Multimedia.Camera.Core 3357 3358**参数:** 3359 3360| 参数名 | 类型 | 必填 | 说明 | 3361| -------- | ----------------------------------------- | ---- | ------------------------ | 3362| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件 | 3363| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 | 3364 3365**示例:** 3366 3367```ts 3368import { BusinessError } from '@ohos.base'; 3369 3370function registerFocusStateChange(captureSession: camera.CaptureSession): void { 3371 captureSession.on('focusStateChange', (err: BusinessError, focusState: camera.FocusState) => { 3372 console.info(`Focus state: ${focusState}`); 3373 }); 3374} 3375``` 3376 3377### off('focusStateChange') 3378 3379off(type: 'focusStateChange', callback?: AsyncCallback\<FocusState\>): void 3380 3381注销监听相机聚焦的状态变化。 3382 3383**系统能力:** SystemCapability.Multimedia.Camera.Core 3384 3385**参数:** 3386 3387| 参数名 | 类型 | 必填 | 说明 | 3388| -------- | ----------------------------------------- | ---- | ------------------------ | 3389| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。| 3390| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,可选 | 3391 3392**示例:** 3393 3394```ts 3395function unregisterFocusStateChange(captureSession: camera.CaptureSession): void { 3396 captureSession.off('focusStateChange'); 3397} 3398``` 3399 3400### on('error') 3401 3402on(type: 'error', callback: ErrorCallback): void 3403 3404监听拍照会话的错误事件,通过注册回调函数获取结果。 3405 3406**系统能力:** SystemCapability.Multimedia.Camera.Core 3407 3408**参数:** 3409 3410| 参数名 | 类型 | 必填 | 说明 | 3411| -------- | ----------------------------------------------------------- | ---- | ------------------------------ | 3412| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用(beginConfig(),commitConfig(),addInput)等接口发生错误时返回错误信息。 | 3413| callback | ErrorCallback| 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3414 3415**示例:** 3416 3417```ts 3418import { BusinessError } from '@ohos.base'; 3419 3420function registerCaptureSessionError(captureSession: camera.CaptureSession): void { 3421 captureSession.on('error', (error: BusinessError) => { 3422 console.info(`Capture session error code: ${error.code}`); 3423 }); 3424} 3425``` 3426 3427### off('error') 3428 3429off(type: 'error', callback?: ErrorCallback): void 3430 3431注销监听拍照会话的错误事件,通过注册回调函数获取结果。 3432 3433**系统能力:** SystemCapability.Multimedia.Camera.Core 3434 3435**参数:** 3436 3437| 参数名 | 类型 | 必填 | 说明 | 3438| -------- | ----------------------------------------------------------- | ---- | ------------------------------ | 3439| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 | 3440| callback | ErrorCallback| 否 | 回调函数,可选 | 3441 3442**示例:** 3443 3444```ts 3445function unregisterCaptureSessionError(captureSession: camera.CaptureSession): void { 3446 captureSession.off('error'); 3447} 3448``` 3449## PortraitSession 3450 3451继承自[CaptureSession](#capturesession),用于设置人像模式的参数。 3452 3453### getSupportedPortraitEffects 3454 3455getSupportedPortraitEffects(): Array<[PortraitEffect](#portraiteffect)> 3456 3457获取支持的人像虚化效果列表。 3458 3459**系统接口:** 此接口为系统接口。 3460 3461**系统能力:** SystemCapability.Multimedia.Camera.Core 3462 3463**返回值:** 3464 3465| 类型 | 说明 | 3466| ----------------------------------------------- | ---------------------------- | 3467| Array<[PortraitEffect](#portraiteffect) > | 支持的人像虚化效果列表。 | 3468 3469**错误码:** 3470 3471以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3472 3473| 错误码ID | 错误信息 | 3474| --------------- | --------------- | 3475| 7400103 | Session not config. | 3476 3477**示例:** 3478 3479```ts 3480function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array<camera.PortraitEffect> { 3481 let portraitEffects: Array<camera.PortraitEffect> = portraitSession.getSupportedPortraitEffects(); 3482 return portraitEffects; 3483} 3484``` 3485### setPortraitEffect 3486 3487setPortraitEffect(effect: PortraitEffect): void 3488 3489设置人像虚化效果。需要先检查设备是否支持人像虚化模式,可以通过[getSupportedPortraitEffects](#getsupportedportraiteffects)获取当前设备所支持的PortraitEffect。 3490 3491**系统接口:** 此接口为系统接口。 3492 3493**系统能力:** SystemCapability.Multimedia.Camera.Core 3494 3495**参数:** 3496 3497| 参数名 | 类型 | 必填 | 说明 | 3498| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 3499| effect | [PortraitEffect](#portraiteffect) | 是 | 人像虚化效果,通过[getSupportedPortraitEffects](#getsupportedportraiteffects)接口获取。 | 3500 3501**错误码:** 3502 3503以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3504 3505| 错误码ID | 错误信息 | 3506| --------------- | --------------- | 3507| 7400103 | Session not config. | 3508 3509**示例:** 3510 3511```ts 3512import { BusinessError } from '@ohos.base'; 3513 3514function setPortraitEffect(portraitSession: camera.PortraitSession, portraitEffects: Array<camera.PortraitEffect>): void { 3515 if (portraitEffects === undefined || portraitEffects.length <= 0) { 3516 return; 3517 } 3518 try { 3519 portraitSession.setPortraitEffect(portraitEffects[0]); 3520 } catch (error) { 3521 let err = error as BusinessError; 3522 console.error(`The setPortraitEffect call failed. error code: ${err.code}`); 3523 } 3524} 3525``` 3526### getPortraitEffect 3527 3528getPortraitEffect(): PortraitEffect 3529 3530获取当前设置的人像虚化效果。 3531 3532**系统接口:** 此接口为系统接口。 3533 3534**系统能力:** SystemCapability.Multimedia.Camera.Core 3535 3536**返回值:** 3537 3538| 类型 | 说明 | 3539| ----------------------------------------------- | ---------------------------- | 3540| [PortraitEffect](#portraiteffect) | 当前设置的人像虚化效果。 | 3541 3542**错误码:** 3543 3544以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3545 3546| 错误码ID | 错误信息 | 3547| --------------- | --------------- | 3548| 7400103 | Session not config. | 3549 3550**示例:** 3551 3552```ts 3553function getPortraitEffect(portraitSession: camera.PortraitSession): camera.PortraitEffect { 3554 let portraitEffect: camera.PortraitEffect = portraitSession.getPortraitEffect(); 3555 return portraitEffect; 3556} 3557``` 3558 3559## CameraOutput 3560 3561会话中[CaptureSession](#capturesession)使用的输出信息,output的基类。 3562 3563## PreviewOutput 3564 3565预览输出类。继承[CameraOutput](#cameraoutput) 3566 3567### start 3568 3569start(callback: AsyncCallback\<void\>): void 3570 3571开始输出预览流,通过注册回调函数获取结果。 3572 3573**系统能力:** SystemCapability.Multimedia.Camera.Core 3574 3575**参数:** 3576 3577| 参数名 | 类型 | 必填 | 说明 | 3578| -------- | -------------------- | ---- | -------------------- | 3579| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3580 3581**错误码:** 3582 3583以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3584 3585| 错误码ID | 错误信息 | 3586| --------------- | --------------- | 3587| 7400103 | Session not config. | 3588 3589**示例:** 3590 3591```ts 3592import { BusinessError } from '@ohos.base'; 3593 3594function startPreviewOutput(previewOutput: camera.PreviewOutput): void { 3595 previewOutput.start((err: BusinessError) => { 3596 if (err) { 3597 console.error(`Failed to start the previewOutput. ${err.code}`); 3598 return; 3599 } 3600 console.info('Callback returned with previewOutput started.'); 3601 }); 3602} 3603``` 3604 3605### start 3606 3607start(): Promise\<void\> 3608 3609开始输出预览流,通过Promise获取结果。 3610 3611**系统能力:** SystemCapability.Multimedia.Camera.Core 3612 3613**返回值:** 3614 3615| 类型 | 说明 | 3616| -------------- | ----------------------- | 3617| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)| 3618 3619**错误码:** 3620 3621以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3622 3623| 错误码ID | 错误信息 | 3624| --------------- | --------------- | 3625| 7400103 | Session not config. | 3626 3627**示例:** 3628 3629```ts 3630import { BusinessError } from '@ohos.base'; 3631 3632function startPreviewOutput(previewOutput: camera.PreviewOutput): void { 3633 previewOutput.start().then(() => { 3634 console.info('Promise returned with previewOutput started.'); 3635 }).catch((err: BusinessError) => { 3636 console.error('Failed to previewOutput start '+ err.code); 3637 }); 3638} 3639``` 3640 3641### stop 3642 3643stop(callback: AsyncCallback\<void\>): void 3644 3645停止输出预览流,通过注册回调函数获取结果。 3646 3647**系统能力:** SystemCapability.Multimedia.Camera.Core 3648 3649**参数:** 3650 3651| 参数名 | 类型 | 必填 | 说明 | 3652| -------- | -------------------- | ---- | -------------------- | 3653| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 3654 3655**示例:** 3656 3657```ts 3658import { BusinessError } from '@ohos.base'; 3659 3660function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { 3661 previewOutput.stop((err: BusinessError) => { 3662 if (err) { 3663 console.error(`Failed to stop the previewOutput. ${err.code}`); 3664 return; 3665 } 3666 console.info('Callback returned with previewOutput stopped.'); 3667 }) 3668} 3669``` 3670 3671### stop 3672 3673stop(): Promise\<void\> 3674 3675停止输出预览流,通过Promise获取结果。 3676 3677**系统能力:** SystemCapability.Multimedia.Camera.Core 3678 3679**返回值:** 3680 3681| 类型 | 说明 | 3682| -------------- | ------------------------ | 3683| Promise\<void\> | 使用Promise的方式获取结果。 | 3684 3685**示例:** 3686 3687```ts 3688import { BusinessError } from '@ohos.base'; 3689 3690function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { 3691 previewOutput.stop().then(() => { 3692 console.info('Callback returned with previewOutput stopped.'); 3693 }).catch((err: BusinessError) => { 3694 console.error('Failed to previewOutput stop '+ err.code); 3695 }); 3696} 3697``` 3698 3699### release 3700 3701release(callback: AsyncCallback\<void\>): void 3702 3703释放输出资源,通过注册回调函数获取结果。 3704 3705**系统能力:** SystemCapability.Multimedia.Camera.Core 3706 3707**参数:** 3708 3709| 参数名 | 类型 | 必填 | 说明 | 3710| -------- | -------------------- | ---- | ------------------- | 3711| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3712 3713**错误码:** 3714 3715以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3716 3717| 错误码ID | 错误信息 | 3718| --------------- | --------------- | 3719| 7400201 | Camera service fatal error. | 3720 3721**示例:** 3722 3723```ts 3724import { BusinessError } from '@ohos.base'; 3725 3726function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { 3727 previewOutput.release((err: BusinessError) => { 3728 if (err) { 3729 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 3730 return; 3731 } 3732 console.info('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); 3733 }); 3734} 3735``` 3736 3737### release 3738 3739release(): Promise\<void\> 3740 3741释放输出资源,通过Promise获取结果。 3742 3743**系统能力:** SystemCapability.Multimedia.Camera.Core 3744 3745**返回值:** 3746 3747| 类型 | 说明 | 3748| -------------- | ----------------------- | 3749| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3750 3751**错误码:** 3752 3753以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3754 3755| 错误码ID | 错误信息 | 3756| --------------- | --------------- | 3757| 7400201 | Camera service fatal error. | 3758 3759**示例:** 3760 3761```ts 3762import { BusinessError } from '@ohos.base'; 3763 3764function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { 3765 previewOutput.release().then(() => { 3766 console.info('Promise returned to indicate that the PreviewOutput instance is released successfully.'); 3767 }).catch((err: BusinessError) => { 3768 console.error('Failed to previewOutput release '+ err.code); 3769 }); 3770} 3771``` 3772 3773### on('frameStart') 3774 3775on(type: 'frameStart', callback: AsyncCallback\<void\>): void 3776 3777监听预览帧启动,通过注册回调函数获取结果。 3778 3779**系统能力:** SystemCapability.Multimedia.Camera.Core 3780 3781**参数:** 3782 3783| 参数名 | 类型 | 必填 | 说明 | 3784| -------- | -------------------- | ---- | --------------------------------------- | 3785| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。底层第一次开始曝光时触发该事件并返回 | 3786| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览开始 | 3787 3788**示例:** 3789 3790```ts 3791function registerPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { 3792 previewOutput.on('frameStart', () => { 3793 console.info('Preview frame started'); 3794 }); 3795} 3796``` 3797 3798### off('frameStart') 3799 3800off(type: 'frameStart', callback?: AsyncCallback\<void\>): void 3801 3802注销监听预览帧启动。 3803 3804**系统能力:** SystemCapability.Multimedia.Camera.Core 3805 3806**参数:** 3807 3808| 参数名 | 类型 | 必填 | 说明 | 3809| -------- | -------------------- | ---- | --------------------------------------- | 3810| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。 | 3811| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 3812 3813**示例:** 3814 3815```ts 3816function unregisterPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { 3817 previewOutput.off('frameStart'); 3818} 3819``` 3820 3821### on('frameEnd') 3822 3823on(type: 'frameEnd', callback: AsyncCallback\<void\>): void 3824 3825监听预览帧结束,通过注册回调函数获取结果。 3826 3827**系统能力:** SystemCapability.Multimedia.Camera.Core 3828 3829**参数:** 3830 3831| 参数名 | 类型 | 必填 | 说明 | 3832| -------- | -------------------- | ---- | ------------------------------------- | 3833| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。预览完全结束最后一帧时触发该事件并返回, | 3834| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览结束 | 3835 3836**示例:** 3837 3838```ts 3839function registerPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { 3840 previewOutput.on('frameEnd', () => { 3841 console.info('Preview frame ended'); 3842 }); 3843} 3844``` 3845 3846### off('frameEnd') 3847 3848off(type: 'frameEnd', callback?: AsyncCallback\<void\>): void 3849 3850注销监听预览帧结束。 3851 3852**系统能力:** SystemCapability.Multimedia.Camera.Core 3853 3854**参数:** 3855 3856| 参数名 | 类型 | 必填 | 说明 | 3857| -------- | -------------------- | ---- | ------------------------------------- | 3858| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。 | 3859| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 3860 3861**示例:** 3862 3863```ts 3864function unregisterPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { 3865 previewOutput.off('frameEnd'); 3866} 3867``` 3868 3869### on('error') 3870 3871on(type: 'error', callback: ErrorCallback): void 3872 3873监听预览输出的错误事件,通过注册回调函数获取结果。 3874 3875**系统能力:** SystemCapability.Multimedia.Camera.Core 3876 3877**参数:** 3878 3879| 参数名 | 类型 | 必填 | 说明 | 3880| -------- | --------------| ---- | ------------------------ | 3881| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。预览接口使用错误时触发该事件,比如调用(start(),release())等接口发生错误时返回对应错误信息。| 3882| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3883 3884**示例:** 3885 3886```ts 3887import { BusinessError } from '@ohos.base'; 3888 3889function registerPreviewOutputError(previewOutput: camera.PreviewOutput): void { 3890 previewOutput.on('error', (previewOutputError: BusinessError) => { 3891 console.info(`Preview output error code: ${previewOutputError.code}`); 3892 }) 3893} 3894``` 3895 3896### off('error') 3897 3898off(type: 'error', callback?: ErrorCallback): void 3899 3900注销监听预览输出的错误事件。 3901 3902**系统能力:** SystemCapability.Multimedia.Camera.Core 3903 3904**参数:** 3905 3906| 参数名 | 类型 | 必填 | 说明 | 3907| -------- | --------------| ---- | ------------------------ | 3908| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。| 3909| callback | ErrorCallback | 否 | 回调函数,可选 | 3910 3911**示例:** 3912 3913```ts 3914function unregisterPreviewOutputError(previewOutput: camera.PreviewOutput): void { 3915 previewOutput.off('error'); 3916} 3917``` 3918 3919### addDeferredSurface 3920 3921addDeferredSurface(surfaceId: string): void 3922 3923配置延迟预览的Surface,可以在session.commitConfig()配流和session.start()启流之后运行。 3924 3925**系统接口:** 此接口为系统接口。 3926 3927**系统能力:** SystemCapability.Multimedia.Camera.Core 3928 3929**参数:** 3930 3931| 参数名 | 类型 | 必填 | 说明 | 3932| -------- | --------------| ---- | ------------------------ | 3933| surfaceId | string | 是 | 从[XComponent](../arkui-ts/ts-basic-components-xcomponent.md)组件获取的surfaceId。| 3934 3935**错误码:** 3936 3937以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3938 3939| 错误码ID | 错误信息 | 3940| --------------- | --------------- | 3941| 7400101 | Parameter missing or parameter type incorrect | 3942 3943**示例:** 3944 3945```ts 3946import common from '@ohos.app.ability.common'; 3947 3948async function preview(context: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, photoSurfaceId: string, previewSurfaceId: string): Promise<void> { 3949 const cameraManager: camera.CameraManager = camera.getCameraManager(context); 3950 const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo) 3951 const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 3952 const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, photoSurfaceId); 3953 const session: camera.CaptureSession = cameraManager.createCaptureSession(); 3954 session.beginConfig(); 3955 session.addInput(cameraInput); 3956 session.addOutput(previewOutput); 3957 session.addOutput(photoOutput); 3958 await session.commitConfig(); 3959 await session.start(); 3960 previewOutput.addDeferredSurface(previewSurfaceId); 3961} 3962``` 3963 3964## ImageRotation 3965 3966枚举,图片旋转角度。 3967 3968**系统能力:** SystemCapability.Multimedia.Camera.Core 3969 3970| 名称 | 值 | 说明 | 3971| ------------ | ---- | ------------- | 3972| ROTATION_0 | 0 | 图片旋转0度。 | 3973| ROTATION_90 | 90 | 图片旋转90度。 | 3974| ROTATION_180 | 180 | 图片旋转180度。 | 3975| ROTATION_270 | 270 | 图片旋转270度。 | 3976 3977## Location 3978 3979图片地理位置信息。 3980 3981**系统能力:** SystemCapability.Multimedia.Camera.Core 3982 3983| 名称 | 类型 | 必填 |说明 | 3984| ------------ | ------ | --- |------------ | 3985| latitude | number | 是 |纬度(度)。 | 3986| longitude | number | 是 |经度(度)。 | 3987| altitude | number | 是 |海拔(米)。 | 3988 3989## QualityLevel 3990 3991枚举,图片质量。 3992 3993**系统能力:** SystemCapability.Multimedia.Camera.Core 3994 3995| 名称 | 值 | 说明 | 3996| -------------------- | ---- | ------------ | 3997| QUALITY_LEVEL_HIGH | 0 | 图片质量高。 | 3998| QUALITY_LEVEL_MEDIUM | 1 | 图片质量中等。 | 3999| QUALITY_LEVEL_LOW | 2 | 图片质量差。 | 4000 4001 4002## PhotoCaptureSetting 4003 4004拍摄照片的设置。 4005 4006**系统能力:** SystemCapability.Multimedia.Camera.Core 4007 4008| 名称 | 类型 | 必填 | 说明 | 4009| -------- | ------------------------------- | ---- | -----------------| 4010| quality | [QualityLevel](#qualitylevel) | 否 | 图片质量。 | 4011| rotation | [ImageRotation](#imagerotation) | 否 | 图片旋转角度。 | 4012| location | [Location](#location) | 否 | 图片地理位置信息。 | 4013| mirror | boolean | 否 |镜像使能开关(默认关)。 | 4014 4015## PhotoOutput 4016 4017拍照会话中使用的输出信息,继承[CameraOutput](#cameraoutput) 4018 4019### capture 4020 4021capture(callback: AsyncCallback\<void\>): void 4022 4023以默认设置触发一次拍照,通过注册回调函数获取结果。 4024 4025**系统能力:** SystemCapability.Multimedia.Camera.Core 4026 4027**参数:** 4028 4029| 参数名 | 类型 | 必填 | 说明 | 4030| -------- | -------------------- | ---- | ------------------- | 4031| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4032 4033**错误码:** 4034 4035以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4036 4037| 错误码ID | 错误信息 | 4038| --------------- | --------------- | 4039| 7400104 | Session not running. | 4040| 7400201 | Camera service fatal error. | 4041 4042**示例:** 4043 4044```ts 4045import { BusinessError } from '@ohos.base'; 4046 4047function capture(photoOutput: camera.PhotoOutput): void { 4048 photoOutput.capture((err: BusinessError) => { 4049 if (err) { 4050 console.error(`Failed to capture the photo ${err.code}`); 4051 return; 4052 } 4053 console.info('Callback invoked to indicate the photo capture request success.'); 4054 }); 4055} 4056``` 4057 4058### capture 4059 4060capture(): Promise\<void\> 4061 4062以默认设置触发一次拍照,通过Promise获取结果。 4063 4064**系统能力:** SystemCapability.Multimedia.Camera.Core 4065 4066**返回值:** 4067 4068| 类型 | 说明 | 4069| -------------- | ------------------------ | 4070| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4071 4072**错误码:** 4073 4074以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4075 4076| 错误码ID | 错误信息 | 4077| --------------- | --------------- | 4078| 7400104 | Session not running. | 4079| 7400201 | Camera service fatal error. | 4080 4081**示例:** 4082 4083```ts 4084import { BusinessError } from '@ohos.base'; 4085 4086function capture(photoOutput: camera.PhotoOutput): void { 4087 photoOutput.capture().then(() => { 4088 console.info('Promise returned to indicate that photo capture request success.'); 4089 }).catch((err: BusinessError) => { 4090 console.error('Failed to photoOutput capture '+ err.code); 4091 }); 4092} 4093``` 4094 4095### capture 4096 4097capture(setting: PhotoCaptureSetting, callback: AsyncCallback\<void\>): void 4098 4099以指定参数触发一次拍照,通过注册回调函数获取结果。 4100 4101**系统能力:** SystemCapability.Multimedia.Camera.Core 4102 4103**参数:** 4104 4105| 参数名 | 类型 | 必填 | 说明 | 4106| -------- | ------------------------------------------- | ---- | -------------------- | 4107| setting | [PhotoCaptureSetting](#photocapturesetting) | 是 | 拍照设置。 | 4108| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4109 4110**错误码:** 4111 4112以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4113 4114| 错误码ID | 错误信息 | 4115| --------------- | --------------- | 4116| 7400101 | Parameter missing or parameter type incorrect | 4117| 7400104 | Session not running. | 4118| 7400201 | Camera service fatal error. | 4119 4120**示例:** 4121 4122```ts 4123import { BusinessError } from '@ohos.base'; 4124 4125function capture(photoOutput: camera.PhotoOutput): void { 4126 let captureLocation: camera.Location = { 4127 latitude: 0, 4128 longitude: 0, 4129 altitude: 0, 4130 } 4131 let settings: camera.PhotoCaptureSetting = { 4132 quality: camera.QualityLevel.QUALITY_LEVEL_LOW, 4133 rotation: camera.ImageRotation.ROTATION_0, 4134 location: captureLocation, 4135 mirror: false 4136 } 4137 photoOutput.capture(settings, (err: BusinessError) => { 4138 if (err) { 4139 console.error(`Failed to capture the photo ${err.code}`); 4140 return; 4141 } 4142 console.info('Callback invoked to indicate the photo capture request success.'); 4143 }); 4144} 4145``` 4146 4147### capture 4148 4149capture(setting?: PhotoCaptureSetting): Promise\<void\> 4150 4151以指定参数触发一次拍照,通过Promise获取结果。 4152 4153**系统能力:** SystemCapability.Multimedia.Camera.Core 4154 4155**参数:** 4156 4157| 参数名 | 类型 | 必填 | 说明 | 4158| ------- | ------------------------------------------- | ---- | -------- | 4159| setting | [PhotoCaptureSetting](#photocapturesetting) | 否 | 拍照设置,传入undefined类型数据按默认无参处理。 | 4160 4161**返回值:** 4162 4163| 类型 | 说明 | 4164| -------------- | ------------------------ | 4165| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4166 4167**错误码:** 4168 4169以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4170 4171| 错误码ID | 错误信息 | 4172| --------------- | --------------- | 4173| 7400101 | Parameter missing or parameter type incorrect | 4174| 7400104 | Session not running. | 4175| 7400201 | Camera service fatal error. | 4176 4177**示例:** 4178 4179```ts 4180import { BusinessError } from '@ohos.base'; 4181 4182function capture(photoOutput: camera.PhotoOutput): void { 4183 let captureLocation: camera.Location = { 4184 latitude: 0, 4185 longitude: 0, 4186 altitude: 0, 4187 } 4188 let settings: camera.PhotoCaptureSetting = { 4189 quality: camera.QualityLevel.QUALITY_LEVEL_LOW, 4190 rotation: camera.ImageRotation.ROTATION_0, 4191 location: captureLocation, 4192 mirror: false 4193 } 4194 photoOutput.capture(settings).then(() => { 4195 console.info('Promise returned to indicate that photo capture request success.'); 4196 }).catch((err: BusinessError) => { 4197 console.error('Failed to photoOutput capture '+ err.code); 4198 }); 4199} 4200``` 4201 4202### isMirrorSupported 4203 4204isMirrorSupported(): boolean 4205 4206查询是否支持镜像拍照。 4207 4208**系统能力:** SystemCapability.Multimedia.Camera.Core 4209 4210**返回值:** 4211 4212| 类型 | 说明 | 4213| -------------- | ----------------------- | 4214| boolean | 返回是否支持镜像拍照。 | 4215 4216**示例:** 4217 4218```ts 4219function isMirrorSupported(photoOutput: camera.PhotoOutput): boolean { 4220 let isSupported: boolean = photoOutput.isMirrorSupported(); 4221 return isSupported; 4222} 4223``` 4224 4225### release 4226 4227release(callback: AsyncCallback\<void\>): void 4228 4229释放输出资源,通过注册回调函数获取结果。 4230 4231**系统能力:** SystemCapability.Multimedia.Camera.Core 4232 4233**参数:** 4234 4235| 参数名 | 类型 | 必填 | 说明 | 4236| -------- | -------------------- | ---- | ------------------- | 4237| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4238 4239**错误码:** 4240 4241以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4242 4243| 错误码ID | 错误信息 | 4244| --------------- | --------------- | 4245| 7400201 | Camera service fatal error. | 4246 4247**示例:** 4248 4249```ts 4250import { BusinessError } from '@ohos.base'; 4251 4252function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { 4253 photoOutput.release((err: BusinessError) => { 4254 if (err) { 4255 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 4256 return; 4257 } 4258 console.info('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); 4259 }); 4260} 4261``` 4262 4263### release 4264 4265release(): Promise\<void\> 4266 4267释放输出资源,通过Promise获取结果。 4268 4269**系统能力:** SystemCapability.Multimedia.Camera.Core 4270 4271**返回值:** 4272 4273| 类型 | 说明 | 4274| -------------- | ----------------------- | 4275| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4276 4277**错误码:** 4278 4279以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4280 4281| 错误码ID | 错误信息 | 4282| --------------- | --------------- | 4283| 7400201 | Camera service fatal error. | 4284 4285**示例:** 4286 4287```ts 4288import { BusinessError } from '@ohos.base'; 4289 4290function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { 4291 photoOutput.release().then(() => { 4292 console.info('Promise returned to indicate that the PreviewOutput instance is released successfully.'); 4293 }).catch((err: BusinessError) => { 4294 console.error('Failed to photoOutput release '+ err.code); 4295 }); 4296} 4297``` 4298 4299### on('captureStart') 4300 4301on(type: 'captureStart', callback: AsyncCallback\<number\>): void 4302 4303监听拍照开始,通过注册回调函数获取Capture ID。 4304 4305**系统能力:** SystemCapability.Multimedia.Camera.Core 4306 4307**参数:** 4308 4309| 参数名 | 类型 | 必填 | 说明 | 4310| -------- | ---------------------- | ---- | ------------------------------------------ | 4311| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听。每次拍照,底层开始曝光时触发该事件并返回。 | 4312| callback | AsyncCallback\<number\> | 是 | 使用callback的方式获取Capture ID。 | 4313 4314**示例:** 4315 4316```ts 4317import { BusinessError } from '@ohos.base'; 4318 4319function registerPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 4320 photoOutput.on('captureStart', (err: BusinessError, captureId: number) => { 4321 console.info(`photo capture stated, captureId : ${captureId}`); 4322 }); 4323} 4324``` 4325 4326### off('captureStart') 4327 4328off(type: 'captureStart', callback?: AsyncCallback\<number\>): void 4329 4330注销监听拍照开始。 4331 4332**系统能力:** SystemCapability.Multimedia.Camera.Core 4333 4334**参数:** 4335 4336| 参数名 | 类型 | 必填 | 说明 | 4337| -------- | ---------------------- | ---- | ------------------------------------------ | 4338| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听 | 4339| callback | AsyncCallback\<number\> | 否 | 可选。 | 4340 4341**示例:** 4342 4343```ts 4344function unregisterPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 4345 photoOutput.off('captureStart'); 4346} 4347``` 4348 4349### on('frameShutter') 4350 4351on(type: 'frameShutter', callback: AsyncCallback\<FrameShutterInfo\>): void 4352 4353监听拍照帧输出捕获,通过注册回调函数获取结果。 4354 4355**系统能力:** SystemCapability.Multimedia.Camera.Core 4356 4357**参数:** 4358 4359| 参数名 | 类型 | 必填 | 说明 | 4360| -------- | ---------- | --- | ------------------------------------ | 4361| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 | 4362| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 是 | 回调函数,用于获取相关信息。该回调返回意味着可以再次下发拍照请求。 | 4363 4364**示例:** 4365 4366```ts 4367import { BusinessError } from '@ohos.base'; 4368 4369function registerPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { 4370 photoOutput.on('frameShutter', (err: BusinessError, frameShutterInfo: camera.FrameShutterInfo) => { 4371 console.info(`photo capture end, captureId : ${frameShutterInfo.captureId}`); 4372 console.info(`Timestamp for frame : ${frameShutterInfo.timestamp}`); 4373 }); 4374} 4375``` 4376 4377### off('frameShutter') 4378 4379off(type: 'frameShutter', callback?: AsyncCallback\<FrameShutterInfo\>): void 4380 4381注销监听拍照帧输出捕获。 4382 4383**系统能力:** SystemCapability.Multimedia.Camera.Core 4384 4385**参数:** 4386 4387| 参数名 | 类型 | 必填 | 说明 | 4388| -------- | ---------- | --- | ------------------------------------ | 4389| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 | 4390| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 否 | 回调函数,可选。 | 4391 4392**示例:** 4393 4394```ts 4395function unregisterPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { 4396 photoOutput.off('frameShutter'); 4397} 4398``` 4399 4400### on('captureEnd') 4401 4402on(type: 'captureEnd', callback: AsyncCallback\<CaptureEndInfo\>): void 4403 4404监听拍照结束,通过注册回调函数获取结果。 4405 4406**系统能力:** SystemCapability.Multimedia.Camera.Core 4407 4408**参数:** 4409 4410| 参数名 | 类型 | 必填 | 说明 | 4411| -------- | --------------- | ---- | ---------------------------------------- | 4412| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。拍照完全结束可触发该事件发生并返回相应信息。 | 4413| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 是 | 回调函数,用于获取相关信息。 | 4414 4415**示例:** 4416 4417```ts 4418import { BusinessError } from '@ohos.base'; 4419 4420function registerPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 4421 photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { 4422 console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`); 4423 console.info(`frameCount : ${captureEndInfo.frameCount}`); 4424 }); 4425} 4426``` 4427 4428### off('captureEnd') 4429 4430off(type: 'captureEnd', callback?: AsyncCallback\<CaptureEndInfo\>): void 4431 4432注销监听拍照结束。 4433 4434**系统能力:** SystemCapability.Multimedia.Camera.Core 4435 4436**参数:** 4437 4438| 参数名 | 类型 | 必填 | 说明 | 4439| -------- | --------------- | ---- | ---------------------------------------- | 4440| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。 | 4441| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 否 | 回调函数,可选。 | 4442 4443**示例:** 4444 4445```ts 4446function unregisterPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 4447 photoOutput.off('captureEnd'); 4448} 4449``` 4450 4451### on('error') 4452 4453on(type: 'error', callback: ErrorCallback): void 4454 4455监听拍照输出发生错误,通过注册回调函数获取结果。 4456 4457**系统能力:** SystemCapability.Multimedia.Camera.Core 4458 4459**参数:** 4460 4461| 参数名 | 类型 | 必填 | 说明 | 4462| -------- | ------------- | ---- | ----------------------------------- | 4463| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。拍照接口调用时出现错误触发该事件并返回错误信息。 | 4464| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4465 4466**示例:** 4467 4468```ts 4469import { BusinessError } from '@ohos.base'; 4470 4471function registerPhotoOutputError(photoOutput: camera.PhotoOutput): void { 4472 photoOutput.on('error', (error: BusinessError) => { 4473 console.info(`Photo output error code: ${error.code}`); 4474 }); 4475} 4476``` 4477 4478### off('error') 4479 4480off(type: 'error', callback?: ErrorCallback): void 4481 4482注销监听拍照输出发生错误。 4483 4484**系统能力:** SystemCapability.Multimedia.Camera.Core 4485 4486**参数:** 4487 4488| 参数名 | 类型 | 必填 | 说明 | 4489| -------- | ------------- | ---- | ----------------------------------- | 4490| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。 | 4491| callback | ErrorCallback | 否 | 回调函数,可选 | 4492 4493**示例:** 4494 4495```ts 4496function unregisterPhotoOutputError(photoOutput: camera.PhotoOutput): void { 4497 photoOutput.off('error'); 4498} 4499``` 4500 4501### isQuickThumbnailSupported 4502 4503isQuickThumbnailSupported(): boolean 4504 4505是否支持输出快速缩略图。 4506 4507在CaptureSession.addOutput、CaptureSession.addInput之后,CaptureSession.commitConfig之前生效。 4508 4509**系统接口:** 此接口为系统接口。 4510 4511**系统能力:** SystemCapability.Multimedia.Camera.Core 4512 4513**返回值:** 4514 4515| 类型 | 说明 | 4516| --------- | ------ | 4517| boolean | 返回支持情况,如果返回true表示支持,否则不支持。 | 4518 4519**错误码:** 4520 4521以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4522 4523| 错误码ID | 错误信息 | 4524| --------------- | --------------- | 4525| 7400104 | session is not running | 4526 4527**示例:** 4528 4529```ts 4530import common from '@ohos.app.ability.common'; 4531 4532async function isQuickThumbnailSupported(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<boolean> { 4533 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4534 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4535 // 创建CaptureSession实例 4536 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4537 // 开始配置会话 4538 captureSession.beginConfig(); 4539 // 把CameraInput加入到会话 4540 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4541 await cameraInput.open(); 4542 captureSession.addInput(cameraInput); 4543 // 把photoOutput加入到会话 4544 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4545 captureSession.addOutput(photoOutput); 4546 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4547 return isSupported; 4548} 4549``` 4550 4551### enableQuickThumbnail 4552 4553enableQuickThumbnail(enabled: boolean): void 4554 4555使能/去使能快速缩略图。 4556 4557在CaptureSession.addOutput、CaptureSession.addInput之后,CaptureSession.commitConfig之前生效。 4558 4559**系统接口:** 此接口为系统接口。 4560 4561**系统能力:** SystemCapability.Multimedia.Camera.Core 4562 4563**参数:** 4564 4565| 参数名 | 类型 | 必填 | 说明 | 4566| -------- | ------------- | ---- | ----------------------------------- | 4567| enabled | boolean | 是 | true:使能快速缩略图;false:去使能快速缩略图。 | 4568 4569**错误码:** 4570 4571以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4572 4573| 错误码ID | 错误信息 | 4574| --------------- | --------------- | 4575| 7400104 | session is not running | 4576 4577**示例:** 4578 4579```ts 4580import common from '@ohos.app.ability.common'; 4581import { BusinessError } from '@ohos.base'; 4582 4583async function enableQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> { 4584 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4585 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4586 // 创建CaptureSession实例 4587 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4588 // 开始配置会话 4589 captureSession.beginConfig(); 4590 // 把CameraInput加入到会话 4591 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4592 await cameraInput.open(); 4593 captureSession.addInput(cameraInput); 4594 // 把PhotoOutPut加入到会话 4595 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4596 captureSession.addOutput(photoOutput); 4597 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4598 if (!isSupported) { 4599 console.info('Quick Thumbnail is not supported to be turned on.'); 4600 return; 4601 } 4602 try { 4603 photoOutput.enableQuickThumbnail(true); 4604 } catch (error) { 4605 let err = error as BusinessError; 4606 console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); 4607 } 4608} 4609``` 4610 4611### on('quickThumbnail') 4612 4613on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void 4614 4615监听快速缩略图输出事件。 4616 4617在enableQuickThumbnail(true)使能快速缩略图之后监听生效。 4618 4619**系统接口:** 此接口为系统接口。 4620 4621**系统能力:** SystemCapability.Multimedia.Camera.Core 4622 4623**参数:** 4624 4625| 参数名 | 类型 | 必填 | 说明 | 4626| -------- | ------------- | ---- | ----------------------------------- | 4627| type | string | 是 | 监听事件,固定为'quickThumbnail'。 | 4628| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调返回PixelMap。 | 4629 4630**示例:** 4631 4632```ts 4633import common from '@ohos.app.ability.common'; 4634import { BusinessError } from '@ohos.base'; 4635import image from '@ohos.multimedia.image'; 4636 4637async function registerQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> { 4638 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4639 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4640 // 创建CaptureSession实例 4641 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4642 // 开始配置会话 4643 captureSession.beginConfig(); 4644 // 把CameraInput加入到会话 4645 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4646 await cameraInput.open(); 4647 captureSession.addInput(cameraInput); 4648 // 把PhotoOutPut加入到会话 4649 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4650 captureSession.addOutput(photoOutput); 4651 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4652 if (!isSupported) { 4653 console.info('Quick Thumbnail is not supported to be turned on.'); 4654 return; 4655 } 4656 try { 4657 photoOutput.enableQuickThumbnail(true); 4658 } catch (error) { 4659 let err = error as BusinessError; 4660 console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); 4661 } 4662 4663 photoOutput.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => { 4664 if (err || pixelMap === undefined) { 4665 console.error('photoOutput on thumbnail failed'); 4666 return; 4667 } 4668 // 显示或保存pixelMap 4669 // do something 4670 }) 4671} 4672``` 4673 4674### off('quickThumbnail') 4675 4676off(type: 'quickThumbnail', callback?: AsyncCallback\<image.PixelMap>): void 4677 4678注销监听快速缩略图输出事件。 4679 4680**系统接口:** 此接口为系统接口。 4681 4682**系统能力:** SystemCapability.Multimedia.Camera.Core 4683 4684**参数:** 4685 4686| 参数名 | 类型 | 必填 | 说明 | 4687| -------- | ------------- | ---- | ----------------------------------- | 4688| type | string | 是 | 监听事件,固定为'quickThumbnail'。 | 4689| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | 否 | 可选。 | 4690 4691**示例:** 4692 4693```ts 4694function unregisterQuickThumbnail(photoOutput: camera.PhotoOutput): void { 4695 photoOutput.off('quickThumbnail'); 4696} 4697``` 4698 4699## FrameShutterInfo 4700 4701拍照帧输出信息。 4702 4703**系统能力:** SystemCapability.Multimedia.Camera.Core 4704 4705| 名称 | 类型 | 必填 | 说明 | 4706| --------- | ------ | ---- | ---------- | 4707| captureId | number | 是 | 拍照的ID。 | 4708| timestamp | number | 是 | 快门时间戳。 | 4709 4710## CaptureEndInfo 4711 4712拍照停止信息。 4713 4714**系统能力:** SystemCapability.Multimedia.Camera.Core 4715 4716| 名称 | 类型 | 必填 | 说明 | 4717| ---------- | ------ | ---- | ---------| 4718| captureId | number | 是 | 拍照的ID。 | 4719| frameCount | number | 是 | 帧数。 | 4720 4721## VideoOutput 4722 4723录像会话中使用的输出信息,继承[CameraOutput](#cameraoutput) 4724 4725### start 4726 4727start(callback: AsyncCallback\<void\>): void 4728 4729启动录制,通过注册回调函数获取结果。 4730 4731**系统能力:** SystemCapability.Multimedia.Camera.Core 4732 4733**参数:** 4734 4735| 参数名 | 类型 | 必填 | 说明 | 4736| -------- | -------------------- | ---- | -------------------- | 4737| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4738 4739**错误码:** 4740 4741以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4742 4743| 错误码ID | 错误信息 | 4744| --------------- | --------------- | 4745| 7400103 | Session not config. | 4746| 7400201 | Camera service fatal error. | 4747 4748**示例:** 4749 4750```ts 4751import { BusinessError } from '@ohos.base'; 4752 4753function startVideoOutput(videoOutput: camera.VideoOutput): void { 4754 videoOutput.start((err: BusinessError) => { 4755 if (err) { 4756 console.error(`Failed to start the video output ${err.code}`); 4757 return; 4758 } 4759 console.info('Callback invoked to indicate the video output start success.'); 4760 }); 4761} 4762``` 4763 4764### start 4765 4766start(): Promise\<void\> 4767 4768启动录制,通过Promise获取结果。 4769 4770**系统能力:** SystemCapability.Multimedia.Camera.Core 4771 4772**返回值:** 4773 4774| 类型 | 说明 | 4775| -------------- | ----------------------- | 4776| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4777 4778**错误码:** 4779 4780以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4781 4782| 错误码ID | 错误信息 | 4783| --------------- | --------------- | 4784| 7400103 | Session not config. | 4785| 7400201 | Camera service fatal error. | 4786 4787**示例:** 4788 4789```ts 4790import { BusinessError } from '@ohos.base'; 4791 4792function startVideoOutput(videoOutput: camera.VideoOutput): void { 4793 videoOutput.start().then(() => { 4794 console.info('Promise returned to indicate that start method execution success.'); 4795 }).catch((err: BusinessError) => { 4796 console.error('Failed to videoOutput start '+ err.code); 4797 }); 4798} 4799``` 4800 4801### stop 4802 4803stop(callback: AsyncCallback\<void\>): void 4804 4805结束录制,通过注册回调函数获取结果。 4806 4807**系统能力:** SystemCapability.Multimedia.Camera.Core 4808 4809**参数:** 4810 4811| 参数名 | 类型 | 必填 | 说明 | 4812| -------- | -------------------- | ---- | ------------------------ | 4813| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 4814 4815**示例:** 4816 4817```ts 4818import { BusinessError } from '@ohos.base'; 4819 4820function stopVideoOutput(videoOutput: camera.VideoOutput): void { 4821 videoOutput.stop((err: BusinessError) => { 4822 if (err) { 4823 console.error(`Failed to stop the video output ${err.code}`); 4824 return; 4825 } 4826 console.info('Callback invoked to indicate the video output stop success.'); 4827 }); 4828} 4829``` 4830 4831### stop 4832 4833stop(): Promise\<void\> 4834 4835结束录制,通过Promise获取结果。 4836 4837**系统能力:** SystemCapability.Multimedia.Camera.Core 4838 4839**返回值:** 4840 4841| 类型 | 说明 | 4842| -------------- | ----------------------- | 4843| Promise\<void\> | 使用Promise的方式获取结果。 | 4844 4845**示例:** 4846 4847```ts 4848import { BusinessError } from '@ohos.base'; 4849 4850function stopVideoOutput(videoOutput: camera.VideoOutput): void { 4851 videoOutput.stop().then(() => { 4852 console.info('Promise returned to indicate that stop method execution success.'); 4853 }).catch((err: BusinessError) => { 4854 console.error('Failed to videoOutput stop '+ err.code); 4855 }); 4856} 4857``` 4858 4859### release 4860 4861release(callback: AsyncCallback\<void\>): void 4862 4863释放输出资源,通过注册回调函数获取结果。 4864 4865**系统能力:** SystemCapability.Multimedia.Camera.Core 4866 4867**参数:** 4868 4869| 参数名 | 类型 | 必填 | 说明 | 4870| -------- | -------------------- | ---- | ------------------- | 4871| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4872 4873**错误码:** 4874 4875以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4876 4877| 错误码ID | 错误信息 | 4878| --------------- | --------------- | 4879| 7400201 | Camera service fatal error. | 4880 4881**示例:** 4882 4883```ts 4884import { BusinessError } from '@ohos.base'; 4885 4886function releaseVideoOutput(videoOutput: camera.VideoOutput): void { 4887 videoOutput.release((err: BusinessError) => { 4888 if (err) { 4889 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 4890 return; 4891 } 4892 console.info('Callback invoked to indicate that the videoOutput instance is released successfully.'); 4893 }); 4894} 4895``` 4896 4897### release 4898 4899release(): Promise\<void\> 4900 4901释放输出资源,通过Promise获取结果。 4902 4903**系统能力:** SystemCapability.Multimedia.Camera.Core 4904 4905**返回值:** 4906 4907| 类型 | 说明 | 4908| -------------- | ----------------------- | 4909| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4910 4911**错误码:** 4912 4913以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4914 4915| 错误码ID | 错误信息 | 4916| --------------- | --------------- | 4917| 7400201 | Camera service fatal error. | 4918 4919**示例:** 4920 4921```ts 4922import { BusinessError } from '@ohos.base'; 4923 4924function releaseVideoOutput(videoOutput: camera.VideoOutput): void { 4925 videoOutput.release().then(() => { 4926 console.info('Promise returned to indicate that the videoOutput instance is released successfully.'); 4927 }).catch((err: BusinessError) => { 4928 console.error('Failed to videoOutput release '+ err.code); 4929 }); 4930} 4931``` 4932 4933### on('frameStart') 4934 4935on(type: 'frameStart', callback: AsyncCallback\<void\>): void 4936 4937监听录像开始,通过注册回调函数获取结果。 4938 4939**系统能力:** SystemCapability.Multimedia.Camera.Core 4940 4941**参数:** 4942 4943| 参数名 | 类型 | 必填 | 说明 | 4944| -------- | -------------------- | ---- | ----------------------------------------- | 4945| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。底层第一次曝光时触发该事件并返回。 | 4946| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像开始 | 4947 4948**示例:** 4949 4950```ts 4951function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 4952 videoOutput.on('frameStart', () => { 4953 console.info('Video frame started'); 4954 }); 4955} 4956``` 4957 4958### off('frameStart') 4959 4960off(type: 'frameStart', callback?: AsyncCallback\<void\>): void 4961 4962注销监听录像开始。 4963 4964**系统能力:** SystemCapability.Multimedia.Camera.Core 4965 4966**参数:** 4967 4968| 参数名 | 类型 | 必填 | 说明 | 4969| -------- | -------------------- | ---- | ----------------------------------------- | 4970| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。 | 4971| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 4972 4973**示例:** 4974 4975```ts 4976function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 4977 videoOutput.off('frameStart'); 4978} 4979 4980``` 4981 4982### on('frameEnd') 4983 4984on(type: 'frameEnd', callback: AsyncCallback\<void\>): void 4985 4986监听录像结束,通过注册回调函数获取结果。 4987 4988**系统能力:** SystemCapability.Multimedia.Camera.Core 4989 4990**参数:** 4991 4992| 参数名 | 类型 | 必填 | 说明 | 4993| -------- | -------------------- | ---- | ------------------------------------------ | 4994| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听。录像完全结束最后一帧时触发该事件并返回 。 | 4995| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像结束 | 4996 4997**示例:** 4998 4999```ts 5000function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 5001 videoOutput.on('frameEnd', () => { 5002 console.info('Video frame ended'); 5003 }); 5004} 5005``` 5006 5007### off('frameEnd') 5008 5009off(type: 'frameEnd', callback?: AsyncCallback\<void\>): void 5010 5011注销监听录像结束。 5012 5013**系统能力:** SystemCapability.Multimedia.Camera.Core 5014 5015**参数:** 5016 5017| 参数名 | 类型 | 必填 | 说明 | 5018| -------- | -------------------- | ---- | ------------------------------------------ | 5019| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听 。 | 5020| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 5021 5022**示例:** 5023 5024```ts 5025function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 5026 videoOutput.off('frameEnd'); 5027} 5028``` 5029 5030### on('error') 5031 5032on(type: 'error', callback: ErrorCallback): void 5033 5034监听录像输出发生错误,通过注册回调函数获取结果。 5035 5036**系统能力:** SystemCapability.Multimedia.Camera.Core 5037 5038**参数:** 5039 5040| 参数名 | 类型 | 必填 | 说明 | 5041| -------- | ----------- | ---- | -------------------------------------- | 5042| type | string | 是 | 监听事件,固定为'error',videoOutput创建成功后可监听。录像接口调用出现错误时触发该事件并返回对应错误码,比如调用(start(),release())接口时出现错误返回对应错误信息。 | 5043| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5044 5045**示例:** 5046 5047```ts 5048import { BusinessError } from '@ohos.base'; 5049 5050function registerVideoOutputError(videoOutput: camera.VideoOutput): void { 5051 videoOutput.on('error', (error: BusinessError) => { 5052 console.info(`Video output error code: ${error.code}`); 5053 }); 5054} 5055``` 5056 5057### off('error') 5058 5059off(type: 'error', callback?: ErrorCallback): void 5060 5061注销监听录像输出发生错误。 5062 5063**系统能力:** SystemCapability.Multimedia.Camera.Core 5064 5065**参数:** 5066 5067| 参数名 | 类型 | 必填 | 说明 | 5068| -------- | ----------- | ---- | -------------------------------------- | 5069| type | string | 是 | 监听事件,固定为'error',videoOutput创建成功后可监听。 | 5070| callback | ErrorCallback | 否 | 回调函数,可选 | 5071 5072**示例:** 5073 5074```ts 5075function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void { 5076 videoOutput.off('error'); 5077} 5078``` 5079 5080## MetadataOutput 5081 5082metadata流。继承[CameraOutput](#cameraoutput) 5083 5084### start 5085 5086start(callback: AsyncCallback\<void\>): void 5087 5088开始输出metadata,通过注册回调函数获取结果。 5089 5090**系统能力:** SystemCapability.Multimedia.Camera.Core 5091 5092**参数:** 5093 5094| 参数名 | 类型 | 必填 | 说明 | 5095| -------- | -------------------------- | ---- | ------------------- | 5096| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5097 5098**错误码:** 5099 5100以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 5101 5102| 错误码ID | 错误信息 | 5103| --------------- | --------------- | 5104| 7400103 | Session not config. | 5105| 7400201 | Camera service fatal error. | 5106 5107**示例:** 5108 5109```ts 5110import { BusinessError } from '@ohos.base'; 5111 5112function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5113 metadataOutput.start((err: BusinessError) => { 5114 if (err) { 5115 console.error(`Failed to start metadataOutput. ${err.code}`); 5116 return; 5117 } 5118 console.info('Callback returned with metadataOutput started.'); 5119 }); 5120} 5121``` 5122 5123### start 5124 5125start(): Promise\<void\> 5126 5127开始输出metadata,通过Promise获取结果。 5128 5129**系统能力:** SystemCapability.Multimedia.Camera.Core 5130 5131**返回值:** 5132 5133| 类型 | 说明 | 5134| ---------------------- | ------------------------ | 5135| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5136 5137**错误码:** 5138 5139以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 5140 5141| 错误码ID | 错误信息 | 5142| --------------- | --------------- | 5143| 7400103 | Session not config. | 5144| 7400201 | Camera service fatal error. | 5145 5146**示例:** 5147 5148```ts 5149import { BusinessError } from '@ohos.base'; 5150 5151function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5152 metadataOutput.start().then(() => { 5153 console.info('Callback returned with metadataOutput started.'); 5154 }).catch((err: BusinessError) => { 5155 console.error('Failed to metadataOutput start '+ err.code); 5156 }); 5157} 5158``` 5159 5160### stop 5161 5162stop(callback: AsyncCallback\<void\>): void 5163 5164停止输出metadata,通过注册回调函数获取结果。 5165 5166**系统能力:** SystemCapability.Multimedia.Camera.Core 5167 5168**参数:** 5169 5170| 参数名 | 类型 | 必填 | 说明 | 5171| -------- | -------------------------- | ---- | ------------------- | 5172| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 5173 5174**示例:** 5175 5176```ts 5177import { BusinessError } from '@ohos.base'; 5178 5179function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5180 metadataOutput.stop((err: BusinessError) => { 5181 if (err) { 5182 console.error(`Failed to stop the metadataOutput. ${err.code}`); 5183 return; 5184 } 5185 console.info('Callback returned with metadataOutput stopped.'); 5186 }) 5187} 5188``` 5189 5190### stop 5191 5192stop(): Promise\<void\> 5193 5194停止输出metadata,通过Promise获取结果。 5195 5196**系统能力:** SystemCapability.Multimedia.Camera.Core 5197 5198**返回值:** 5199 5200| 类型 | 说明 | 5201| ---------------------- | --------------------------- | 5202| Promise\<void\> | 使用Promise的方式获取结果。 | 5203 5204**示例:** 5205 5206```ts 5207import { BusinessError } from '@ohos.base'; 5208 5209function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5210 metadataOutput.stop().then(() => { 5211 console.info('Callback returned with metadataOutput stopped.'); 5212 }).catch((err: BusinessError) => { 5213 console.error('Failed to metadataOutput stop '+ err.code); 5214 }); 5215} 5216``` 5217 5218### on('metadataObjectsAvailable') 5219 5220on(type: 'metadataObjectsAvailable', callback: AsyncCallback\<Array\<MetadataObject\>\>): void 5221 5222监听检测到的metadata对象,通过注册回调函数获取结果。 5223 5224**系统能力:** SystemCapability.Multimedia.Camera.Core 5225 5226**参数:** 5227 5228| 参数名 | 类型 | 必填 | 说明 | 5229| -------- | -------------- | ---- | ------------------------------------ | 5230| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。检测到有效的metadata数据时触发该事件发生并返回相应的metadata数据 | 5231| callback | Callback\<Array\<[MetadataObject](#metadataobject)\>\> | 是 | 回调函数,用于获取metadata数据。 | 5232 5233**示例:** 5234 5235```ts 5236import { BusinessError } from '@ohos.base'; 5237 5238function registerMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 5239 metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 5240 console.info(`metadata output metadataObjectsAvailable`); 5241 }); 5242} 5243``` 5244 5245### off('metadataObjectsAvailable') 5246 5247off(type: 'metadataObjectsAvailable', callback?: AsyncCallback\<Array\<MetadataObject\>\>): void 5248 5249注销监听检测到的metadata对象。 5250 5251**系统能力:** SystemCapability.Multimedia.Camera.Core 5252 5253**参数:** 5254 5255| 参数名 | 类型 | 必填 | 说明 | 5256| -------- | -------------- | ---- | ------------------------------------ | 5257| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。 | 5258| callback | Callback\<Array\<[MetadataObject](#metadataobject)\>\> | 否 | 回调函数,可选。 | 5259 5260**示例:** 5261 5262```ts 5263function unregisterMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 5264 metadataOutput.off('metadataObjectsAvailable'); 5265} 5266``` 5267 5268### on('error') 5269 5270on(type: 'error', callback: ErrorCallback): void 5271 5272监听metadata流的错误,通过注册回调函数获取结果。 5273 5274**系统能力:** SystemCapability.Multimedia.Camera.Core 5275 5276**参数:** 5277 5278| 参数名 | 类型 | 必填 | 说明 | 5279| -------- | ------------- | ---- | --------------------------------------- | 5280| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。metadata接口使用错误时触发该事件并返回对应错误码,比如调用(start(),release())接口时发生错误返回对应错误信息。 | 5281| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5282 5283**示例:** 5284 5285```ts 5286import { BusinessError } from '@ohos.base'; 5287 5288function registerMetadataOutputError(metadataOutput: camera.MetadataOutput): void { 5289 metadataOutput.on('error', (metadataOutputError: BusinessError) => { 5290 console.info(`Metadata output error code: ${metadataOutputError.code}`); 5291 }); 5292} 5293``` 5294 5295### off('error') 5296 5297off(type: 'error', callback?: ErrorCallback): void 5298 5299注销监听metadata流的错误。 5300 5301**系统能力:** SystemCapability.Multimedia.Camera.Core 5302 5303**参数:** 5304 5305| 参数名 | 类型 | 必填 | 说明 | 5306| -------- | ------------- | ---- | --------------------------------------- | 5307| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。 | 5308| callback | ErrorCallback | 否 | 回调函数,可选 | 5309 5310**示例:** 5311 5312```ts 5313function unregisterMetadataOutputError(metadataOutput: camera.MetadataOutput): void { 5314 metadataOutput.off('error'); 5315} 5316``` 5317 5318## MetadataObjectType 5319 5320枚举,metadata流。 5321 5322**系统能力:** SystemCapability.Multimedia.Camera.Core 5323 5324| 名称 | 值 | 说明 | 5325| ------------------------- | ---- | ----------------- | 5326| FACE_DETECTION | 0 | metadata对象类型,人脸检测。检测点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。<br> 此坐标系是以设备充电口在右侧时的横向设备方向为基准的,<br> 例如应用的预览界面布局以设备充电口在下侧时的竖向方向为基准,<br> 布局宽高为{w,h}, 返回点为{x,y},则转换后的坐标点为{1-y,x}。 | 5327 5328## Rect 5329 5330矩形定义。 5331 5332**系统能力:** SystemCapability.Multimedia.Camera.Core 5333 5334| 名称 | 类型 | 必填 | 说明 | 5335| -------- | ------ | --------------- | -------------------- | 5336| topLeftX | number | 是 | 矩形区域左上角x坐标。 | 5337| topLeftY | number | 是 | 矩形区域左上角y坐标。 | 5338| width | number | 是 | 矩形宽。 | 5339| height | number | 是 |矩形高。 | 5340 5341## MetadataObject 5342 5343相机元能力信息,[CameraInput](#camerainput)相机信息中的数据来源,通过metadataOutput.on('metadataObjectsAvailable')接口获取 5344 5345**系统能力:** SystemCapability.Multimedia.Camera.Core 5346 5347| 名称 | 类型 | 必填 | 说明 | 5348| -------- | ------------------------------- | ---- | -----------------| 5349| type | [MetadataObjectType](#metadataobjecttype) | 否 | metadata 类型,目前只有人脸识别。 | 5350| timestamp | number | 否 | 当前时间戳(毫秒)。 | 5351| boundingBox | [Rect](#rect) | 否 | metadata 区域框 |